Page 1 of 1

Is there an "init" or "start" function that the game calls?

Posted: Tue Oct 28, 2014 5:26 am
by MrChoke
I am still new to how scripting in this game works. The question I have is whether there is a starting script that runs where I can add any up-front settings for the game. It seems all scripts must be tied to a connector, which means they are basically triggered. I want one that executes one time when the game starts.

I do see a script called init.lua. It does seem to execute when the map loads into the editor. I know this only because if I type syntax errors the map won't load. However, any global variables I set or anything else I do in there doesn't seem to be accessible inside the script entities you define in the map. I guess they don't seem to be global even though I do declare them outside any function in init.lua.

Re: Is there an "init" or "start" function that the game cal

Posted: Tue Oct 28, 2014 5:50 am
by cfisher2833
You can maybe do a floor trigger (they're unseen).

Re: Is there an "init" or "start" function that the game cal

Posted: Tue Oct 28, 2014 5:59 am
by minmay
Script entity code is run immediately when the game starts. But if you put your code in a function definition it won't execute until something actually calls the function. So something like:

Code: Select all

function stuff()
  print("stuff")
end
won't print "stuff" until something calls stuff(). But if you have a script entity that only contains:

Code: Select all

print("stuff")
outside of any functions, it should print "stuff" immediately. Or if you want to keep your code in a function (you almost certainly do) just do this:

Code: Select all

function stuff()
  print("stuff")
end
stuff()

Re: Is there an "init" or "start" function that the game cal

Posted: Tue Oct 28, 2014 8:56 pm
by MrChoke
Thanks! That works.