You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running lua code in a match is useful for being able to gather character info, get the status of a match, set certain data to characters (maps, state numbers, life, etc), change the music and/or draw things on the screen (GUI mostly), among other things.
This kind of code can be thought as "listening" in Java/JavaScript, where you "listen" for events to happen in the match and perform actions accordingly. In this case, we'll use character debug triggers to do this kind of match listening.
Start by creating a new lua module file in external/mods, you usually want to have all your in-dev mods in this folder.
Inside this module, we'll start by writing our inital listening function statement:
localfunctionf_matchListener()
-- Do stuff hereend
We want to make this function local, as to avoid overwriting other functions that might use the same identifier.
This function will contain all the code that will be run while the match is going on. In this case, we wanna do something simple like getting the name of the character who loses more than 700 HP in the first round, then kill it.
To get this kind of information, we have to first loop through all possible characters in a match and examine them. For that, we can use "trigger redirector" functions, which are just funcs to select the player that will be affected by the trigger debug functions.
localfirstToLoseHP=""localfunctionf_matchListener()
-- Get character dataifroundno() ==1thenfori=1, 8doplayer(i) -- Select player to examineiflife() <300andfirstToLoseHP=="" thenfirstToLoseHP=name()
setLife(0)
endendendend
Generally, looping through all 8 possible characters is okay performance-wise, even if the match has less than 8 chars. Having said that, you can still optimize the for loop by first getting the total number of existing characters in the match.
localfirstToLoseHP=""localtotalPlayers=0localfunctionf_matchListener()
-- Get total players in this matchiftotalPlayers==0thenfori=1, 2do-- Only check initial characters from both teamsplayer(i)
totalPlayers=totalPlayers+numpartner() +1endend-- Get character dataifroundno() ==1thenfori=1, totalPlayersdoplayer(i) -- Select player to examineiflife() <300andfirstToLoseHP=="" thenfirstToLoseHP=name()
setLife(0)
endendendend
Since we declared some local variables outside of the scope of the function, we need a way to reset them, so we can ensure they'll work in the next match. We'll add a new function that will be in charge of this:
At this point, we have everything we want for our event listening. The next step is to use hook functions available in base scripts (external/script) to hook this code into the main loop() lua function (that runs once per frame in a match). This function can be found at external/script/global.lua.
We'll start by hooking f_matchListener() using hook.add():
-- Hook list, hook name, function (as value)hook.add("loop", "getCharLife", f_matchListener)
We also want to hook f_resetData() somewhere where it'll only be executed once. launchFight() hook, which is run just once before any match starts, seems like a good choice:
But actually, this could be troublesome, since launchFight() isn't called again when a match is reset using F4. To circumvent this, we'll instead call f_resetData at the start of f_matchListener using debug triggers.
More details about hook functions can be found here.
With this, our code is finished. Here's the final result:
-- VariableslocalfirstToLoseHP=""localtotalPlayers=0-- Functionslocalfunctionf_resetData()
firstToLoseHP=""totalPlayers=0endlocalfunctionf_matchListener()
-- Reset data at the start of the matchifroundno() ==1androundstate() ==0thenf_resetData()
end-- Get total players in this matchiftotalPlayers==0thenfori=1, 2do-- Only check initial characters from both teamsplayer(i)
totalPlayers=totalPlayers+numpartner() +1endend-- Get character dataifroundno() ==1thenfori=1, totalPlayersdoplayer(i) -- Select player to examineiflife() <300andfirstToLoseHP=="" thenfirstToLoseHP=name()
setLife(0)
endendendend-- Hookshook.add("loop", "getCharLife", f_matchListener)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Running lua code in a match is useful for being able to gather character info, get the status of a match, set certain data to characters (maps, state numbers, life, etc), change the music and/or draw things on the screen (GUI mostly), among other things.
This kind of code can be thought as "listening" in Java/JavaScript, where you "listen" for events to happen in the match and perform actions accordingly. In this case, we'll use character debug triggers to do this kind of match listening.
Start by creating a new lua module file in
external/mods, you usually want to have all your in-dev mods in this folder.Inside this module, we'll start by writing our inital listening function statement:
We want to make this function local, as to avoid overwriting other functions that might use the same identifier.
This function will contain all the code that will be run while the match is going on. In this case, we wanna do something simple like getting the name of the character who loses more than 700 HP in the first round, then kill it.
To get this kind of information, we have to first loop through all possible characters in a match and examine them. For that, we can use "trigger redirector" functions, which are just funcs to select the player that will be affected by the trigger debug functions.
Generally, looping through all 8 possible characters is okay performance-wise, even if the match has less than 8 chars. Having said that, you can still optimize the for loop by first getting the total number of existing characters in the match.
Since we declared some local variables outside of the scope of the function, we need a way to reset them, so we can ensure they'll work in the next match. We'll add a new function that will be in charge of this:
At this point, we have everything we want for our event listening. The next step is to use hook functions available in base scripts (
external/script) to hook this code into the mainloop()lua function (that runs once per frame in a match). This function can be found atexternal/script/global.lua.We'll start by hooking
f_matchListener()usinghook.add():We also want to hook
f_resetData()somewhere where it'll only be executed once.launchFight()hook, which is run just once before any match starts, seems like a good choice:But actually, this could be troublesome, since
launchFight()isn't called again when a match is reset using F4. To circumvent this, we'll instead callf_resetDataat the start off_matchListenerusing debug triggers.More details about hook functions can be found here.
With this, our code is finished. Here's the final result:
All reactions