Page 1 of 1

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

Posted: Thu Jan 31, 2013 1:07 pm
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!

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

Posted: Thu Jan 31, 2013 5:51 pm
by Ixnatifual
I could see that coming in handy!

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

Posted: Thu Jan 31, 2013 7:01 pm
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).

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

Posted: Sun Jan 29, 2017 3:17 am
by apendrag0n3
That first code snippet - the "Clone Object Party" one - where does that get put?

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

Posted: Sun Jan 29, 2017 9:44 am
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.