Raised This Month: $51 Target: $400
 12% 

Multiple Plugins, Same Function, Execution Order


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-27-2019 , 18:53   Multiple Plugins, Same Function, Execution Order
Reply With Quote #1

If there are multiple plugins that are using the same function, what determines the order of operation? In this example, let's say 2 plugins are using this same exact code below...

Is there a way to know or even define which one will execute first?

PHP Code:
#include <sourcemod>
#include <sdkhooks>

public void OnClientPutInServer(int client) {
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetype) {

    
// ...Do something

Thanks for any info, this would be useful to know how SourceMod determines this.
__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT
scorpius2k1 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-28-2019 , 05:43   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #2

It is unspecified.

In general, it'll be based on hook order for private forwards and load order for global forwards, and load order will generally be filesystem iteration order for auto-loaded plugins without dependencies, which will generally be alphabetical. But it isn't safe to rely on any of that - I mention it only because people will incorrectly state that it is guaranteed to work like that if I don't.

What is defined is how different Action return values affect the end result passed to the game, higher values will always win over lower values, so taking OnTakeDamage, if any plugin blocks the damage by returning Plugin_Handled, even though they're all called, that result will win over a plugin changing the damage with Plugin_Changed, or one just using it as a notification and returning Plugin_Continue.
__________________

Last edited by asherkin; 04-28-2019 at 05:45.
asherkin is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 04-28-2019 , 06:43   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #3

I'm curious in what situation would you need to know this information?
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-28-2019 , 09:32   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #4

Quote:
Originally Posted by asherkin View Post
It is unspecified.

In general, it'll be based on hook order for private forwards and load order for global forwards, and load order will generally be filesystem iteration order for auto-loaded plugins without dependencies, which will generally be alphabetical. But it isn't safe to rely on any of that - I mention it only because people will incorrectly state that it is guaranteed to work like that if I don't.

What is defined is how different Action return values affect the end result passed to the game, higher values will always win over lower values, so taking OnTakeDamage, if any plugin blocks the damage by returning Plugin_Handled, even though they're all called, that result will win over a plugin changing the damage with Plugin_Changed, or one just using it as a notification and returning Plugin_Continue.
Excellent info asherkin, thank you.
__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT
scorpius2k1 is offline
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-28-2019 , 09:43   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #5

Quote:
Originally Posted by 1337norway View Post
I'm curious in what situation would you need to know this information?
I am trying to return a value from one plugin "Plugin A" via a native to another "Plugin B"...from the same function. Problem is, "B" fires before "A" does and the native doesn't return how I would like it to. Taking into account what asherkin said, it makes sense why B is firing first. Even setting 'Plugin_Changed' on "A", it still fires last. I cannot use 'Plugin_Handled' to "force" it first for obvious reasons. I am simply wanting to have one plugin modify another in the same function via a native call, I am just not sure how to go about doing it the proper way. Is there a way to do a Pre hook for "A" maybe?

Here's a simplified version of of my issue:

PHP Code:
// PLUGIN A *NEEDS TO FIRE FIRST*
/////////////////////////////////

#include <sourcemod>
#include <sdkhooks>

int g_iDamage;

public 
void OnClientPutInServer(int client) {
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetype) {

    
g_iDamage 50;

    
PrintToChatAll("Plugin A, Native Damage Set: %i"g_iDamage);

    return 
Plugin_Continue
}  

public 
APLRes AskPluginLoad2(Handle myselfbool latechar [] errorint err_max) {

    
CreateNative("native_call"Native_call);

    return 
APLRes_Success;
}

public 
int Native_call(Handle pluginint numParams) {
     
     
int client GetNativeCell(1);
     
int index GetNativeCell(2);

     if(
IsValidClient(client)) {
         if(
index == 0) { return g_iDamage; }
    }

    return -
1;
}



// PLUGIN B
///////////

#include <sourcemod>
#include <sdkhooks>

native int native_call(int indexint client);

public 
void OnClientPutInServer(int client) {
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetype) {

    
float fDamageChange float(native_call(attacker1));

    
PrintToChatAll("Plugin B, Native Damage Change: %f"fDamageChange);

    
damage fDamageChange;

    return 
Plugin_Changed;

__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT
scorpius2k1 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-28-2019 , 10:11   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #6

If they're meant to be cooperative, just fire your own OnTakeDamage forward.
__________________
asherkin is offline
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-28-2019 , 10:23   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #7

Quote:
Originally Posted by asherkin View Post
If they're meant to be cooperative, just fire your own OnTakeDamage forward.
That sounds just like what I would need in this situation. After reading your reply, I had a look at forwards here. I have not used them yet personally, so new territory here... I see Private & Global forwards, which would be proper for what I am trying to do?

I'll have to do some playing around with it so I can learn these a bit better and hopefully implement in my plugin, quite useful.
__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT
scorpius2k1 is offline
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-28-2019 , 10:59   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #8

Is there a place to reference matching parameter types for 'CreateGlobalForward' (ET_Event, Param_Cell, etc) for my function? I see a section on the wiki for 'Call_PushCell', 'Call_PushString', etc for use inside the function, but not the creation parameters. I did a search, maybe not looking in the right spot, but https://sm.alliedmods.net/new-api/fu...eGlobalForward has no further info for "ParamType". I am getting a tag mismatch at the moment.

Thanks again.

PHP Code:
#include <sdkhooks>

Handle g_OnTakeDamageModForward;
 
public 
void OnPluginStart()
{
   
// WARNING 213: TAG MISMATCH
   
g_OnTakeDamageModForward CreateGlobalForward("OnTakeDamageMod"Param_CellParam_CellParam_CellParam_FloatParam_Cell);
}
 
public 
APLRes AskPluginLoad2(Handle pluginbool latechar[] errorint err_max)
{
   
RegPluginLibrary("OnTakeDamageMod_Plugin");
}

public 
void OnClientPutInServer(int client) {
   
SDKHook(clientSDKHook_OnTakeDamageEventHandler_OnTakeDamageMod);
}
 
public 
Action EventHandler_OnTakeDamageMod(int iVictimint &iAttackerint &inflictorfloat &damageint &damagetype)
{
   
Action result;
 

   
/* Start function call */
   
Call_StartForward(g_OnTakeDamageModForward);
 
   
/* Push parameters one at a time */
   
Call_PushCell(iVictim);
   
Call_PushCellRef(iAttacker);
   
Call_PushCellRef(inflictor);
   
Call_PushFloatRef(damage);
   
Call_PushCellRef(damagetype);


   
/* Finish the call, get the result */
   
Call_Finish(result);
 
   return 
result;


EDIT: Found a fix but would like to find a reference for the parameter calls

PHP Code:
g_OnTakeDamageModForward CreateGlobalForward("OnTakeDamageMod"ET_EventParam_CellParam_CellByRefParam_CellByRefParam_FloatByRefParam_CellByRef); 
__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT

Last edited by scorpius2k1; 04-28-2019 at 11:09.
scorpius2k1 is offline
scorpius2k1
Senior Member
Join Date: Feb 2016
Old 04-28-2019 , 13:51   Re: Multiple Plugins, Same Function, Execution Order
Reply With Quote #10

Quote:
Originally Posted by asherkin View Post
Perfect, thanks.

I was able to get a public forward setup and working nicely (plugin A has the forward, plugin B simply calls it via the forwarded function), everything is working as I had hoped now and learned how to do forwards.

Thanks a million for the help, really appreciate it.
__________________
{__ PIRATES COVE __} ● HIGH-KILL Community | Stats ●
Half-Life 2: Deathmatch
66.151.244.149:27016 => CONNECT
scorpius2k1 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode