How to set up a hot-key to run any script quick!

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

How to set up a hot-key to run any script quick!

Post by Komag »

The gui hooks are truly awesome, and one nice little trick is to set up some simple key press code to run whatever script you want at the touch of a key.

First, you need to add the main gui hook to the party definition like this:

Code: Select all

cloneObject{
  name = "party",
  baseObject = "party",
  onDrawGui = function(g)
    guiScript.doGui(g)
  end,
}
Next, add a script entity to your dungeon called guiScript and put in this code:

Code: Select all

function doGui(g) -- triggers EVERY frame by render gui hook
  --code
end
Now you're ready to put whatever you want in place of --code

For this example, let's just do a simple print message when you press the 'm' key. Change your guiScript code to:

Code: Select all

mFree = true

function doGui(g) -- triggers EVERY frame by render gui hook
  if g.keyDown("m") then
     if mFree then
        print("m key was pressed")
        --other code
     end
     mFree = false
    else -- key no longer being held down
     mFree = true
  end
end
And that's it! Now, in game, if you press the m key it will print the message "m key was pressed". And it will only print one time, even if you hold the key down for a few seconds, until you let go and press it again.

You'll notice I added a "global" variable called mFree just before the function. This acts as a flag so that holding the key down won't trigger the code to be run multiple times in a row. The first frame rendered after you press the key checks to see if mFree is true, and only if it's true does it run the code, and it also sets it to false, preventing the code from running again until you first let go of the key (to set the flag true) to make it available again.

If you want to test some feature over and over, you can temporarily put in a link in this code, such as:

Code: Select all

mFree = true

function doGui(g) -- triggers EVERY frame by render gui hook
  if g.keyDown("m") then
     if mFree then
        --print("m key was pressed")
        myOtherScript.myOtherTestFunction()
     end
     mFree = false
    else
     mFree = true
  end
end
Now, every time you press m it will run myOtherTestFunction() for whatever you have set up there.

You can use this for testing things out when designing complex puzzles, or you could add this as an actual gameplay feature for players to use in your dungeon, making 'm' be a new interface key which casts a free missile spell or something, whatever you want, anything is possible!
Finished Dungeons - complete mods to play
Ixnatifual

Re: How to set up a hot-key to run any script quick!

Post by Ixnatifual »

I could see that coming in handy!
User avatar
thomson
Posts: 337
Joined: Thu Sep 13, 2012 9:55 pm
Location: R'lyeh
Contact:

Re: How to set up a hot-key to run any script quick!

Post by thomson »

Or you can look at grimwidgets grimwidgets if you need more powerful things. You may want to look at the the code and the compass example (press c to show or hide it).
[MOD] Eye of the Beholder: Waterdeep sewers forum sources; Grimtools (LoG1 -> LoG2 converter) sources
apendrag0n3
Posts: 6
Joined: Wed Jan 25, 2017 5:48 am

Re: How to set up a hot-key to run any script quick!

Post by apendrag0n3 »

That first code snippet - the "Clone Object Party" one - where does that get put?
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: How to set up a hot-key to run any script quick!

Post by Isaac »

apendrag0n3 wrote:That first code snippet - the "Clone Object Party" one - where does that get put?
Object definitions need to be placed in one of the lua files found in the mod's 'mod_assets/scripts' folder. Generally (by convention), objects go in the objects.lua file.
Post Reply