Page 1 of 1

Can callbacks be called within a map script?

Posted: March 21st, 2016, 11:37 pm
by Pphelix
Are callbacks possible within a map script? Reason as to why is because i want something to happen when someone dies within a map. If they're how are they accessed?

Re: Can callbacks be called within a map script?

Posted: March 22nd, 2016, 3:46 pm
by Rezil
I'm not sure, but if you want to do something once a player dies couldn't you just track each player as they connect (and stop tracking them once they disconnect) and check their health every server frame? Once it reaches zero execute whatever code you want to execute. Also take care of players going into spectator mode etc.

Re: Can callbacks be called within a map script?

Posted: March 23rd, 2016, 12:53 am
by Pphelix
I just did a for loop for all players and ran a thread, i don't know why i didn't think of that in the first place XD Problem is solved now!

Re: Can callbacks be called within a map script?

Posted: March 23rd, 2016, 9:04 am
by MoonDog
I know I'm late to the thread, but the answer is yes.

I don't what you are scripting for, but code hits a script callback that is setup in _callbacksetup.gsc.

[[level.callbackPlayerKilled]]


So if you have your own custom function you'd like to setup, some where in your map you need to point that to your function.

Example:


level.callbackPlayerKilled= ::YourFunction;


YourFunction( eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration )
{
}

Re: Can callbacks be called within a map script?

Posted: March 23rd, 2016, 9:06 am
by MoonDog
Can't modify my post while waiting for moderation, but I pasted the wrong callback I believe. Should be [[level.callbackPlayerKilled]]

Re: Can callbacks be called within a map script?

Posted: March 24th, 2016, 5:26 pm
by Pedsdude
MoonDog wrote:Can't modify my post while waiting for moderation, but I pasted the wrong callback I believe. Should be [[level.callbackPlayerKilled]]
Posts approved now :)

Re: Can callbacks be called within a map script?

Posted: April 9th, 2016, 3:31 pm
by IzNoGoD
MoonDog wrote:I know I'm late to the thread, but the answer is yes.

I don't what you are scripting for, but code hits a script callback that is setup in _callbacksetup.gsc.

[[level.callbackPlayerKilled]]


So if you have your own custom function you'd like to setup, some where in your map you need to point that to your function.

Example:


level.callbackPlayerKilled= ::YourFunction;


YourFunction( eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration )
{
}
Should keep the original callback in place. Best way to do that is to overwrite the level.callbackplayerkilled 2 seconds after server has started (because some mods overwrite it 1 second after already...) and call the original callback from within your custom callback.

Code: Select all

main()
{
    thread hijack();
}

hijack()
{
    wait 2;
    level.originalcallbackPlayerKilled = level.callbackPlayerKilled;
    level.callbackPlayerKilled = ::yourfunction;
}

yourfunction(stuff goes here)
{
    self iprintln("You are dead meat");
    self [[level.originalcallbackPlayerKilled]](stuff goes here again);
}

Re: Can callbacks be called within a map script?

Posted: April 12th, 2016, 3:06 am
by MoonDog
IzNoGoD wrote:
Should keep the original callback in place. Best way to do that is to overwrite the level.callbackplayerkilled 2 seconds after server has started (because some mods overwrite it 1 second after already...) and call the original callback from within your custom callback.

Code: Select all

main()
{
    thread hijack();
}

hijack()
{
    wait 2;
    level.originalcallbackPlayerKilled = level.callbackPlayerKilled;
    level.callbackPlayerKilled = ::yourfunction;
}

yourfunction(stuff goes here)
{
    self iprintln("You are dead meat");
    self [[level.originalcallbackPlayerKilled]](stuff goes here again);
}

If you really want to store off the original callback, you can. I don't necessarily like making a call stack larger than it needs to be, just out of best practice. If possible, however, avoid waits. If you are working on a mode that has waits before it defines its callbacks, it is probably a hacky fix because they are attempting to define them out of order and they are getting overridden by the default logic. That or they are improperly threading functions when they do not need to be threaded.

See if you can refactor it, take note of when it is executed and then see to it that your call happens afterwards. At most, waittillframeend will suffice. Again, its a best practices thing. Waits should be avoided in threads and functions during load and setup when it is possible to do so.

Re: Can callbacks be called within a map script?

Posted: May 8th, 2016, 12:20 pm
by IzNoGoD
MoonDog wrote:
IzNoGoD wrote:
Should keep the original callback in place. Best way to do that is to overwrite the level.callbackplayerkilled 2 seconds after server has started (because some mods overwrite it 1 second after already...) and call the original callback from within your custom callback.

Code: Select all

main()
{
    thread hijack();
}

hijack()
{
    wait 2;
    level.originalcallbackPlayerKilled = level.callbackPlayerKilled;
    level.callbackPlayerKilled = ::yourfunction;
}

yourfunction(stuff goes here)
{
    self iprintln("You are dead meat");
    self [[level.originalcallbackPlayerKilled]](stuff goes here again);
}

If you really want to store off the original callback, you can. I don't necessarily like making a call stack larger than it needs to be, just out of best practice. If possible, however, avoid waits. If you are working on a mode that has waits before it defines its callbacks, it is probably a hacky fix because they are attempting to define them out of order and they are getting overridden by the default logic. That or they are improperly threading functions when they do not need to be threaded.

See if you can refactor it, take note of when it is executed and then see to it that your call happens afterwards. At most, waittillframeend will suffice. Again, its a best practices thing. Waits should be avoided in threads and functions during load and setup when it is possible to do so.
I fully agree coding-wise, however, I've seen a few mods (in cod2) that hijack the callbacks without calling the original ones. Hijacking the original ones and re-calling them would break some of that stuff for sure.