Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Saturday, February 23, 2013

Why learn how to script?


It is well-known that Roblox offers a neat scripting API that lets users program in the Lua scripting language. But why should you learn it? What purpose does it serve you to learn it? Let me contrast some statements some users have given me.


"You're already really good at scripting. I will never be that good, so I should never learn"

Wrong, very wrong. When I started scripting, users like xLEGOx (now Stravant), Anaminus, and Stickmasterluke were (and still are) fantastic scripters. I looked at them and thought, "I will never be that good. Never." Thankfully I didn't stop there though. Not that I ever became better, but I never let the thought of others being better than me stop me. I used it has positive motivation. Something to strive for.

I came from not knowing anything about computers to knowing quite a lot about them and programming. I am no genius either. I am a B-average student and don't care too much about math or science. However, I still learned how to script and love doing it.


"The wiki doesn't help"

I learned from the wiki. It helps a ton! The main issue you have here is that you aren't willing to read through full articles and then constantly practice what the articles teach you. If you want to learn something that a wiki article teaches you, then you MUST practice it a lot until you get a true understanding of how it all works.

"Scripting has no future value for me"

If I never got into scripting, I would still be chasing a false dream of becoming a fighter pilot, which will never happen due to my poor eyesight. Scripting opened up the doors of computers to me, which has opened up a whole new field of profession that I want to go into. Lua scripting has also introduced me to larger languages, such as C++ and Java, which are languages used in the professional fields today. Scripting may or may not have future value for you, but if it does, why risk passing up the opportunity of learning it?

"Scripting is too hard"

This may be an age issue. I was 13 when I started scripting, and it was pretty difficult at first. If you are younger than 13, I still encourage you to strive to learn, but it WILL be difficult, but challenges are what programmers live for. The goal is to never stop trying. Always push forward in your learning.

"Scripting is useless"

You might as well say computers are useless then. In fact, you might as well throw your computer out a window. But instead, I encourage you to change your mindset and give it a try.

"How do I learn?"

There are a lot of great resources online. Honestly, googling questions is a great way to find help. Sites like Stackoverflow have tons of archived forum posts that I have learned tons from. The ROBLOX Wiki is a great place to learn how to script in Lua on Roblox, and Lua.org provides great documentation for the Lua language in general.

Thursday, February 21, 2013

Difference Between Scripting and Programming

LEFT: Java | RIGHT: Lua

The Debate

I (Crazyman32) have debated many times on the Roblox  forums whether scripting on Roblox  can be considered programming or not. For a long time, I argued that the two are interchangeable  scripting is the same as programming. However, others argued that they are totally different and not related to each other. Other crazy arguments flowed through as well.

Over the few years I have been programming and scripting on Roblox (approx. 5 years), I have learned what the true differences are. To end the debate once-and-for-all, I want to share my knowledge with you, the Roblox players.


The Truth

Have you ever heard the phrase "a square is a rectangle, but a rectangle isn't always a square"? This is the truth about scripting.

By the definition of computer programming, scripting IS programming, however there is a large gap between what scripting languages do and what programming languages do.

The big difference is that Scripting is the process of writing source code that another already-existing program will interpret how it wants to. This allows scripting languages to be very dynamic, for the programmer can tell the program how to interpret the scripts.

Example:
The Roblox game is written primarily in the programming language C++. Lua is then implemented inside the C/C++ code, which us Roblox players then use to write Lua scripts. The C++ developers of Roblox can add/delete/change the functionality of Lua within Roblox so it has different effects on the game.

TLDR

Scripting is used to control a program. Programming is used to control the computer.

Thursday, January 24, 2013

Scripting: Better String Concatenation

Better String Concatenation

Note: This tutorial is for users who already have a basic understanding of Lua in general.

Introduction

First off, what is "string concatenation?"  String concatenation is simply the joining of two strings. For example, local str = "Hello" .. "World" There we combined Hello and World together through concatenation.

The Big Issue

String concatenation is great, however, there is a looming issue that is faced within just about any programming language: Repetitive additions to a string through concatenation is very performance-costly. For example, if you want to add "Hello" 100,000 times to a single string variable, using standard loops with standard concatenation is going to create a performance issue.

Example of the above scenario using default string concatenation in Lua:

local str = ""

for i = 1,100000 do
   str = str .. "Hello"
end

The above code took 4700 milliseconds (4.7 seconds) to execute completely.


The Better Method

Thankfully, there is a much better way to accomplish the same task in much less time. In some languages, such as Java, you have things like the java StringBuilder object, which fixes this whole mess of performance issues by appending strings and then shoving them all together when you need them.

In Lua, we can devise our own method by using tables and table.concat. In fact, the method of going about doing this is quite simple too. Instead of constantly concatenating our 'str' variable with "Hello," we are going to add "Hello" to a table. At the end of the loop, we will concatenate the table into a readable string variable. This is much quicker.

Example of the better method using tables and table concatenation:


local str = ""
local strHold = {} -- Table to hold string data

for i = 1,100000 do
   table.insert(strHold, "Hello")
end

str = table.concat(strHold)

The above code took 60 milliseconds (0.06 seconds) to execute completely. As you can see, there was a tremendous increase in performance speed.


A Final Note

As a final note, I would like to tell you that standard concatenation is NOT BAD. The method shown in this tutorial should only be used when needing to concatenate a large string object multiple (thousands of) times as quickly as possible. On Roblox, there will be few times when you will need to use this method, however it is an extremely valuable skill to know in the real world of programming.

Saturday, January 19, 2013

Lua Editor

Crazyman32's Lua Editor

Crazyman32's Lua Editor is a text editor designed specifically to write and run pure Lua code. It is currently in its early alpha stages, meaning that it is not yet fully complete. The editor is a great tool to use when wanting to practice Lua quickly and offline. The editor is also a great resource to use when learning Lua, as you can swiftly write in code and run it.


The editor comes with live build/compilation capabilities, which means that the editor will tell you if there are any syntax errors, while you write your code. You can then run the code as well. Handy shortcuts are built in as well. F5 will run whatever is in the editor right away.

Another fun inclusion with the editor is auto-indentation and auto-completion. Therefore, when you write a new 'if then' or other block creation line, the editor will put the 'end' necessary into the code when you press Enter.

The Lua Editor is built 100% with Java. Lua is then interpreted with an API called LuaJ, which is the C version of Lua written in Java and still fully open-source.

You can keep up with the latest updates with the Editor by visiting the project's main page on SourceForge:


Friday, January 18, 2013

Learning the Data Persistence API

Data Persistence Tutorial


Introduction

Data Persistence. A scary phrase that makes most people afraid to even approach it. However, I have good news: It's not actually as complicated as it sounds! In this tutorial, I will guide you through the basics of the data persistence API[1] on ROBLOX.

The Data Persistence API allows you to save and load data for a player in one specific level. For an example of how this is helpful, consider the following: You have a player in your game. The player earns 1,000 points after playing for a good hour. However, you want the player to still have the 1,000 points after he leaves and comes back to your game, no matter what server he joins. The Data API allows this to happen, as long as the player comes to that same game again (it can still be a different server though, but must be the same game).

NOTE: This tutorial is for people who already have a basic understanding of scripting in Lua[2] on ROBLOX. If you don't know scripting very well yet, I highly encourage you to continue on learning more about it before diving into this tutorial. There are great ROBLOX Lua tutorials on the main Wiki site.



Getting Started

First of all, let's learn the methods used in the API. The whole API is built into the Player object. The methods that will be used to control the API are as following:

In that list, we have three categories of things. Loading, saving, and waiting. Before we do ANY saving or loading, we must make sure that the player object is ready to save and load data. That's where the WaitForDataReady method comes into play.

Quick examples:

  • player:SaveBoolean("myBoolean", true)
  • player:SaveInstance("myInstance", game.Workspace.MyModel)
  • player:SaveString("myString", "Hello world!")
  • player:SaveNumber("myNumber", 32)
  • local b = player:LoadBoolean("myBoolean")
  • local object = player:LoadInstance("myInstance")
  • local str = player:LoadString("myString")
  • local num = player:LoadNumber("myNumber")


Typically, you would do most of the saving and loading when the player first enters the game. Here is some pseudocode for that:


function PlayerEntered(player)
   player:WaitForDataReady() -- Await data ready
   -- Add loading/saving methods here
end

game.Players.PlayerAdded:connect(PlayerEntered)

That snippet of coding is the bare-bones of working with the data API. Thankfully, the rest of the process isn't very difficult either! Let us look at an example of using both saving and loading. In this example, we can keep track of the number of times the player has visited the game:


-- All under the PlayerEntered block:

player:WaitForDataReady()

-- Load the number of visits currently
-- If nothing has been saved, it will return 0
local numberVisits = player:LoadNumber("visits")

-- Increase the number by one:
numberVisits = (numberVisits + 1)

-- Save the new number of visits:
player:SaveNumber("visits"numberVisits)

Advanced: Auto-Saving

A lot of times, you want to be able to allow stats to save automatically, that way the player never has to worry about pressing any Save button all the time. A lot of users like to use the PlayerRemoving method to trigger saving, however that method can sometimes run into issues and not properly save all the data. Therefore, I prefer to tell people to take advantage of the Changed event in different value holder objects, like the IntValue object.

In the scenario below, we have a snippet of a script in the PlayerEntered block where we create a stat holder and then set a Changed event in order to save the value every time it changes:


-- All under the PlayerEntered block:

player:WaitForDataReady()

-- Create the value holder:
local points = Instance.new("IntValue", player)
points.Name = "Points"

-- Load points:
points.Value = player:LoadNumber("points")

-- Auto-save technique using the Changed event:
points.Changed:connect(function(newValue)
   player:SaveNumber("points", newValue)
end)


--------------------------------------------
-- How to avoid saving too often:

local lastValue = points.Value
points.Changed:connect(function(newValue)
   lastValue = newValue
   Delay(1, function()
      if (lastValue == newValue) then
         player:SaveNumber("points", newValue)
      end
   end)
end)

[1] API: Application Programming Interface. Usually a library of program code that lets you program off of.
[2] Lua: A programming language commonly implemented for scripting on programs

Monday, January 14, 2013

Game Passes - Getting Them To Work

Hello. If you make games, one important aspect for your game are Game Passes.

There are many ways to use Game Passes, and the most used way is allowing people to enter a VIP room. But how does one make a Game Pass VIP door? Well, today, I am going to show you how! Let's get started!



Building The Door*


Building the door should not be a problem, but I am still going to describe it. You will need to use ROBLOX Studio. First, you will need to create the door. Usually, you will want it to be of the size of a character, so players can get through. You will probably want the door to be anchored too, unless you expect it to move whenever a player touches it...
The size of a player's character is 4 x 5 x 1, so you will probably want your door to have that size. However, you can't get exactly that size with the Brick FormFactor. Therefore, you will probably want to change the FormFactor to Symmetric, so you can get the exact size. Another solution would be to simply make the door slightly bigger than the exact size of a character.

Making The Door Work

You should now have a door built. To make the door only allow people who have the Game Pass to enter, you need to insert a Script into the door. To insert a script, click Insert>Object>Script.

Once you insert it, open it. If you are using ROBLOX Studio 2.0, it will open when you insert the script. Copy the following code and paste it into the script.



local door = script.Parent
local passID = 1337 -- Change that to the passes ID

function pass(p, id)
return game:GetService("GamePassService"):PlayerHasPass(p, id)
end

script.Parent.Touched:connect(function(p)
if p then -- make sure the touching object is still there
plr = game.Players:GetPlayerFromCharacter(p.Parent)
if plr then -- if the toucher is a player, this will return the player
if pass(plr, passID) then
script.Parent.CanCollide = false
wait(1)
script.Parent.CanCollide = true
else
p.Parent:BreakJoints()
end
end
end
end
)

And you're done! Simply place the door where the VIP room is, and add stuff to the room!

I hope you enjoyed this tutorial by PlusJon!