Author Topic: A quick Lua beginners tutorial  (Read 3414 times)

0 Members and 1 Guest are viewing this topic.

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
A quick Lua beginners tutorial
« on: November 01, 2006, 06:56:32 PM »
The_fool asked me how I learned lua, So I told him the basics. I didn't work very hard on this, but maybe it'l inspire some of you like it did me.

i learned it by just fooling around. its pretty easy.

for example, you open this:
http://gmwiki.garry.tv/index.php/Lua

it has everything you need.

then you see 2 sections:
Engine Bindings
and
Hooks/Events

Hooks and events are used to tell when a function is run. Ex, "When a balloon is popped" is a hook/event.

Bindings do things like set health.


So click on Engine Bindings and find something that looks cool. For example,
Code: [Select]
_PlayerSetHealth - Changes a player's health. Syntax: <playerid> <newhealth>
Looks good. Lets go back and find a hook to use
Code: [Select]
eventNPCKilled - An NPC is killed. Syntax: <killerid> <killed>
That works well too. So now open notepad and enter this:
Code: [Select]
function eventNPCKilled
CAPS DO MAKE A DIFFERENCE, use correct capitalization

'function' means that the following is an EVENT or FUNCTION

now remember it said this:

eventNPCKilled - An NPC is killed. Syntax: <killerid> <killed>

those mean that when the function is called (ie, an npc dies) it will give you those values. so they must be assigned to variables. Edit what you entered to read this:
Code: [Select]
function eventNPCKilled(killer,killed)
This means that the killer (<killerid>) will be stored in the variable 'killer' and so on.

Now we can use our binding.
Code: [Select]
_PlayerSetHealth - Changes a player's health. Syntax: <playerid> <newhealth>
We add this line below the last one, doing the same thing, except this doesnt give you information, it needs information, so we must suply it with the proper info.
Code: [Select]
_PlayerSetHealth(killer, 150)
This means that the variable stored as 'killer' will equal the player who gets the health. (ie, the player who killed the npc)
150 means that we are setting his health to 150.

Now we add an 'end' to tell the game that our function is now over.

It should look like this:
Code: [Select]
function eventNPCKilled(killer,killed)
_PlayerSetHealth(killer, 150)
end

save it as a .lua file in gmod9/lua

run the game, lua_openscript it, and kill an npc.

« Last Edit: November 01, 2006, 06:59:31 PM by Golden-Death »


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: A quick Lua beginners tutorial
« Reply #1 on: November 02, 2006, 05:02:59 PM »
One thing I learned quickly was to use HookEvent when using event stuff.
(Can't be used in SWEPS)

If you don't, and have multiple scripts already, you're likely not to have the event cause a reaction.
Or, yours will work, but another scripts won't.


"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: A quick Lua beginners tutorial
« Reply #2 on: November 06, 2006, 03:20:32 PM »
Yeah, I just start out with it since its easier to see how it works.