This is not written by me !!!
Credits: hypersniper
Refresher on Tables
I'm sure you remember what tables are, right? If you don't, give [Only registered and activated users can see links. ], and [Only registered and activated users can see links. ] a read to refresh your mind. Basically tables are used to organise data, and improve efficiency. They are like a queue of people, each is numbered 1 to 10 (keys) and at each position in the queue is a person (value). But, in Lua, we can have very fancy queues and values.
Custom functions
This is a very closely related concept that not many people understand. In your scripting life, you've just made functions after RegisterEventing them, with (pUnit, event) and whatnot. I'm going to flip it all upside down so you actually understand what a function is.
Firstly, functions can be anything. They don't need pUnit or event. For example, in a Lua script you can save yourself a lot of time if you're doing something repetitive.
This may confuse you. Why is pUnit in the brackets? What is this 'person'? I'll explain.function Say(person, message)
person:SendChatMessage(12, 0, message)
end
function unit_onspawn(pUnit, event)
Say(pUnit, "I have now spawned")
end
function unit_oncombat(pUnit, event)
Say(pUnit, "I attack!")
end
RegisterUnitEvent(1337, 18, "unit_onspawn")
RegisterUnitEvent(1337, 1, "unit_combat")
- When creating a custom function, you can ask for whatever you want in the brackets. In my case, I need a person to send the message, and the message to send.
- These "things in the brackets" are called arguments or parameters. Custom functions allow for custom arguments. Very handy.
- Later, you can simply use that function whenever you want. Using a function is "calling" it. So in my OnSpawn event, I call the Say function with arguments pUnit and "I have now spawned". This will make pUnit say "I am now spawned", and by using the Say function, I have saved about 30 characters of typing.
But this leads me to another question. How come unit_onspawn and unit_combat come with free complimentary arguments, when no one said what arguments they should take? This magic happens inside the Lua engine thanks to the special RegisterUnitEvent, so you really don't have to worry about it.
Back to tables
In other news, time to learn more about tables. We learnt in the advanced tables tutorial that you can use strings as keys.
There is another way you can do this. This is just an easier way to set table1's "hello" key:Code:table1["hello"] = 5
Combining our two new pieces of knowledge to formCode:table1.hello = 5
Functions in tables! Take a look at this example:
So what this does is make unit #1337 say "Hello" every second, after saying "Hello I'm here" when he spawns.table1 = {} --set up an empty table (MAKE SURE YOU DO NOT LOCAL THESE TABLES)
function table1.sayhello(pUnit, event)
pUnit:SendChatMessage(12, 0, "Hello")
end
function table1.sayhelloimhere(pUnit)
pUnit:SendChatMessage(12, 0, "Hello I'm here!")
end
function unit_onspawn(pUnit, event)
table1.sayhelloimhere(pUnit)
pUnit:RegisterEvent("table1.sayhello", 1000, 0)
end
RegisterUnitEvent(1337, 18, "unit_onspawn")
That ends this tutorial, laying the foundations for the next tutorial that will explain how these become useful in emulation Lua.
While I have striven to make this guide as clear, concise and understandable as possible, there may be something that just isn't. If that happens, please post



LinkBack URL
About LinkBacks




Reply With Quote


Bookmarks