How to set up a hot-key to run any script quick!
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!
-----
original thread for discussion/questions:
viewtopic.php?f=14&t=4945