![]() |
[TUT] Beginning 2DWorlds Lua Scripting - Printable Version +- 2DWorlds Forums (http://2dworlds.buildism.net/forum) +-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4) +--- Forum: Help (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=21) +--- Thread: [TUT] Beginning 2DWorlds Lua Scripting (/showthread.php?tid=744) |
[TUT] Beginning 2DWorlds Lua Scripting - DysLabs - 03-19-2013 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 -- RE: [TUT] Beginning 2DWorlds Lua Scripting - Oak - 03-19-2013 Nice, this is even easier to understand than the Wiki. Make more tutorials please on 2DWorlds_Lua. RE: [TUT] Beginning 2DWorlds Lua Scripting - DysLabs - 03-19-2013 Currently I'm on my iCrap and don't feel line trying to write it. RE: [TUT] Beginning 2DWorlds Lua Scripting - Oak - 03-19-2013 iPod or iPad? I'm on my iPad without a problem. RE: [TUT] Beginning 2DWorlds Lua Scripting - DysLabs - 03-19-2013 IPod. It's decided to be really leggy again. RE: [TUT] Beginning 2DWorlds Lua Scripting - DysLabs - 03-19-2013 Updated the identifiers section to introduce variables, will be expanded on later. Finished the functions chapter. RE: [TUT] Beginning 2DWorlds Lua Scripting - Oak - 03-20-2013 I think Jacob_ should consider making you a wiki writer for our reference wiki. |