A little confusion about Lua.

I understand the basics of Lua and can canibalise previous code to make it do what I want it to but I’m just confused about how the Lua file is originally run.

The only thing I can liken this question to is Expression 2.

In that Expression 2 has runOnTick(1) and any “functions” and if-thens are run on the game tick.

But i’m confused about how it works with Lua.
Is there a master script that runs on server start that makes the subscripts run or are specific scripts automatically run continually whilst the gamemode is running.

When you run a gamemode. Gamemode stuff is run, along with what you’ve hooked for it.
e2 isn’t really Lua.
You can make a lot of functions. They wont be run unless you run them.
Files in autorun folder will be run. You can have functions in them they won’t be run unless you have something to run them
let’s say. In autorun you have
function hi()
print(“hi”)
end

But it won’t be run without using hi()

Is it only run once or continually?
Since I’m canabalising for now until I learn more I’m learning from the Perp 2.5 files (I know I shouldn’t)

There isn’t an autorun folder but what is confusing me is what is making specific files run whilst the server is up.

I’ve tried understanding the Lua tutorials online on the Wiki etc. but it’s this issue that is stopping me.

the whole:
“if player does this in game then run this script”
thing

Oh.
Example. Console commands?
**[Concommand.Add

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Concommand.Add)**
concommand.Add(“isaythisinconsole”, functiontorun)

When typing isaythisinconsole into console. (Like you could do connect)
It’d run functiontorun() with 3 args.
functiontorun(player, command, arguments)
Player, is an entity of the player who’s running the command.
Command, will in that case be “isaythisinconsole” A string. and arguments is a table of what you ran afterwards.
isaythisinconsole hi i am a lot of args
It’ll have a table, one word per value.
for k, v in pairs(arg) do
print(v)
end
would print
hi
i
am
a
lot
of
args

Ok. That makes sense for console commands.

I don’t think I’m explaining it correctly.

Imagine a situation where I want a function run every time a player presses the use key.

The codes all fine its just is it a specific folder that I put the finished if then statement so that the server is constantly checking to see
“if use = down”
repeatedly

or are ALL scripts in the gamemode area constantly checked for if thens?

Depends if you want it run, every framerate, when they key is down. OR when you click it. OR even when you release it.
**[Gamemode.KeyPress

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Gamemode.KeyPress)**
**[Gamemode.KeyRelease

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Gamemode.KeyRelease)**
**[Gamemode.Think

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Gamemode.Think)**

For the think. You need **[Player.KeyDown

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.KeyDown)** too.

I still don’t think I’m explaining it well enough.

Are all If - then - Run functions checked every framerate?

or are there specific folders that are checked every framerate?

if i did

if gamemode.keypress then function run
in any lua file and save it anywhere in the gamemode directory will it be checked for valid every framerate?

Look at the examples in the links.

I don’t think you’re getting it, this example prints “Hello world!” once when the script is loaded:

[lua]print(“Hello World!”)[/lua]

It is only ran once.

If you want something to run every frame then you use hooks. A hook calls a function when a certain condition is met, or a certain event is fired.

This will print “Hello World!” every frame:

[lua]
function HelloWorld()
print(“Hello World!”)
end
hook.Add(“Tick”, “printhelloworld”, HelloWorld)
[/lua]

The syntax for hook.Add is: EventName, UniqueID, Function

In case you don’t know what a function is, it is some code that does a predefined task, EG: this outputs “Hello World!” 5 times:

[lua]
function HelloWorld()
print(“HelloWorld!”)
end
HelloWorld()
HelloWorld()
HelloWorld()
HelloWorld()
HelloWorld()
[/lua]

No, all if then statements are not run every frame.
To get them to do this you have to put them in functions which you then hook to the appropriate game event (List of hook)

Think is the hook which gets run every frame, so to get the equivalent of runOnTick(1) you’d do
[lua]
hook.Add(“Think”,“unique_name”,function()
–code here will be run every frame
end)
[/lua]

[editline]22nd May 2011[/editline]

:ninja:'d

Dont compare E2 to lua.

Dont learn from Perp2.5, its way to complicated and terribly coded for you to understand.

That answers my questions.
So hooks are checked every gametick and once a hook is true then a function is run from it.

Does it matter where these hooks are placed in the filestructure.

Only if you hook it to Think.

Think… Not Tick.

Only the Think & HUDPaint hook are called every frame. but both have different uses.

Hooks are called on certain events, that is their use.

So I can use a hook anywhere in the file structure and in theory it should work on that hook.

Not any, some hooks are serverside only and some are clientside only