Poll:
You do not have permission to vote in this poll.
Total 0 vote(s) 0%
* You voted for this item. [Show Results]

[TUT] Beginning 2DWorlds Lua Scripting
#1
Hello. This is an introduction to 2DWorlds Lua Scripting. In no way is this complete, and suggestions, questions, and feedback is welcome.
Part 1: Beginner
Chapter 1: Identifiers and Properties
Lua is like a tree. At the top is the root object (called 'game' in your scripts) ans then the services, ad then the objects inside the services. In Lua, you navigate this tree with .'s (dots).
For example, this code will kill you.
[lua]
game.World.[YOURNAMEHERE].Health=0
[/lua]
If you replace '[YOURNAMEHERE]' with your in-game name.
For example, I would put
[lua]
game.World.DysLabs.Health=0
[/lua]
In order to run this, you can either put it in a script and run it, or you can type it in the Lua console. Be sure to add your character first or it won't work.

As you can see 'game.World.DysLabs.Health' is the identifier, '=' is the operator, and '0' is the value. It's kind of like a math problem. You can set any property in any object, see the wiki for properties that an object has.

You can also create your own identifiers. In this case, they will be called variables. You assign them the same way. However, they can only contain 0-9, a-Z, and underscores. The scope is also important, but we will get to the later.

Functions
(now you will actually need to write a script, the Lua console won't work here)

Sometimes, you will reuse the same code over and over, and you might want to compress it into single statements. Now you can. Introducing functions.
Here's how you would create a function:
[lua]
function example()
--do stuff
end
[/lua]
(btw, a line that begins with '--' is a comment in Lua and is ignored)
So lets say we wanted to make multiple countdowns.
You could do:
[lua]
start=10
repeat
print(start)
sleep(1)
start-1
until
start==0
-- Next countdown --
[/lua]
(note: this code is untested. I'm not sure if there is a repeat... until loop in 2DWorlds)

But with functions, this could be simplified to:
[lua]
function countdown(start)
repeat
print start
start=start-1
sleep(1)
until start==0
end

countdown(10)
countdown(9)
countdown(8)
-- etc--
[/lua]

Now notice the word 'start' in the parenthesis in the function definition and the call. This can be any number of values, as long as they are valid identifiers. They are called 'parameters.' You define them like so:
[lua]
function newprint(msg)
print("HI U OK: "..msg)
end
[/lua]
(hint: '..' is the string concentration symbol in Lua. It is used to combine strings with other data types.)
and that concludes our section on functions.

Not Really A Chapter (Creating Scripts)
To insert a script, go to Edit > Insert
Click on 'Script' and click Ok.
Now, in the explorer to the left, expand World (if it isn't already) and find 'Script'
Double click it.
You can now edit it.
Due to a bug, you will need to right-click and hut run to test the script after its initial run (right after its created)

Variables


-- End of written tutorial --
[Image: 2eehsib.gif]
Reply
#2
Nice, this is even easier to understand than the Wiki.
Make more tutorials please on 2DWorlds_Lua.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#3
Currently I'm on my iCrap and don't feel line trying to write it.
[Image: 2eehsib.gif]
Reply
#4
iPod or iPad? I'm on my iPad without a problem.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#5
IPod.
It's decided to be really leggy again.
[Image: 2eehsib.gif]
Reply
#6
Updated the identifiers section to introduce variables, will be expanded on later.
Finished the functions chapter.
[Image: 2eehsib.gif]
Reply
#7
I think Jacob_ should consider making you a wiki writer for our reference wiki.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)