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

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

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

Post 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.
cfisher2833
Posts: 11
Joined: Sat Oct 18, 2014 9:14 pm

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

Post by cfisher2833 »

You can maybe do a floor trigger (they're unseen).
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

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

Post 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()
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

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

Post by MrChoke »

Thanks! That works.
Post Reply