AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Question about Natives (https://forums.alliedmods.net/showthread.php?t=283274)

Whitez 05-29-2016 13:27

Question about Natives
 
Is it possible to create a forward and a native with more params?
Will something like this work?

PHP Code:

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
g_Forwards[FW_USER_INFECT_PRE] = CreateMultiForward("zp_fw_core_infect_pre"ET_CONTINUEFP_CELLFP_CELLFP_CELL)
    
g_Forwards[FW_USER_INFECT] = CreateMultiForward("zp_fw_core_infect"ET_IGNOREFP_CELLFP_CELL)
    
g_Forwards[FW_USER_INFECT_POST] = CreateMultiForward("zp_fw_core_infect_post"ET_IGNOREFP_CELLFP_CELLFP_CELL)
}
InfectPlayer(idattacker 0value 0)
{

}


public 
native_core_infect(plugin_idnum_paramsparam2)
{



Black Rose 05-29-2016 13:32

Re: Question about Natives
 
Not exactly sure what you need but you can add more or less as many parameters as you want/need.

Whitez 05-29-2016 13:51

Re: Question about Natives
 
The original native has two parameters, id and attacker
PHP Code:

native_my_native(plugin_id num_params

I want to add an optional third parameter to send it in case of other special situations ( like below )
PHP Code:

native_my_native(plugin_idnum_param1num_param2

I'm just wondering if it will actually work properly, is the first time i work with natives and forwards and i never seen a native with more than two params (plugin_id, num_params)

Black Rose 05-29-2016 13:59

Re: Question about Natives
 
No, you misunderstand. To get the id and attacker parameter values inside a style 0 dynamic native you have to use
Code:
new id = get_param(1); new attacker = get_param(2);
Just continue like this...
Code:
if ( num_params == 3 ) {     new whatever = get_param(3); }

Declaring them might be a problem. You're supposed to use ... and the compiler will ignore how many arguments you use, but when I did this the values I got where completely off. I think I got the right values by using get_param_byref() but I can't tell for sure and I'm not suggesting it.
I would instead use a default value which is obscure and checking if that is in use or if the user entered a value.
Code:
native my_native(id, attacker, param3 = -911)

Forwards are more complicated. But you can send whatever you want, it's up to the receiver to decide if they want that information.

Whitez 05-29-2016 14:16

Re: Question about Natives
 
Is this right?

Condition is just an optional parameter, which i want to send only in case of a special event, so i can use exceptions for special events in other plugins with the forwards created

PHP Code:

Custom Forwards
enum _
:TOTAL_FORWARDS
{
    
FW_USER_INFECT_PRE 0,
    
FW_USER_INFECT,
    
FW_USER_INFECT_POST
}

new 
g_ForwardResult
new g_Forwards[TOTAL_FORWARDS]

public 
plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
g_Forwards[FW_USER_INFECT_PRE] = CreateMultiForward("zp_fw_core_infect_pre"ET_CONTINUEFP_CELLFP_CELLFP_CELL)
    
g_Forwards[FW_USER_INFECT] = CreateMultiForward("zp_fw_core_infect"ET_IGNOREFP_CELLFP_CELL)
    
g_Forwards[FW_USER_INFECT_POST] = CreateMultiForward("zp_fw_core_infect_post"ET_IGNOREFP_CELLFP_CELLFP_CELL)
}

InfectPlayer(idattacker 0condition 0)
{
    
ExecuteForward(g_Forwards[FW_USER_INFECT_PRE], g_ForwardResultidattackercondition)
    
    
// One or more plugins blocked infection
    
if (g_ForwardResult >= PLUGIN_HANDLED)
        return;
    
    
ExecuteForward(g_Forwards[FW_USER_INFECT], g_ForwardResultidattackercondition)
    
    
flag_set(g_IsZombieid)
    
    if (
GetZombieCount() == 1)
        
flag_set(g_IsFirstZombieid)
    else
        
flag_unset(g_IsFirstZombieid)
    
    
ExecuteForward(g_Forwards[FW_USER_INFECT_POST], g_ForwardResultidattackercondition)
    
    
CheckLastZombieHuman()
}

public 
native_core_infect(plugin_idnum_paramsnum_condition)
{
    new 
id get_param(1)
    
    if (!
is_user_alive(id))
    {
        
log_error(AMX_ERR_NATIVE"[ZP] Invalid Player (%d)"id)
        return 
false;
    }
    
    if (
flag_get(g_IsZombieid))
    {
        
log_error(AMX_ERR_NATIVE"[ZP] Player already infected (%d)"id)
        return 
false;
    }
    
    new 
attacker get_param(2)
    
    if (
attacker && !is_user_connected(attacker))
    {
        
log_error(AMX_ERR_NATIVE"[ZP] Invalid Player (%d)"attacker)
        return 
false;
    }
    
    new 
condition get_param(3)
    
    
InfectPlayer(idattackercondition)
    return 
true;


PHP Code:


// In case of a special situation
zp_core_infect(idattacker3

PHP Code:


public fw_zp_core_infect_post(victimattackercondition)
{
            if(
condition 3)
              {
                      
code here
              
}



Black Rose 05-29-2016 15:17

Re: Question about Natives
 
I really don't fully understand your code.
You have to check how many params were used in the native. That's what num_params is for.
num_condition should not be there at all.
There's also no way of determining how many cells were passed into the forward. So the receiver doesn't know. Perhaps making the condition into another native? If you would tell what the third parameter is I could give you a direct answer.

I also want to point out that the middle forward is kind of useless. You either want to prevent it on pre or react on post. The middle forward is basically post.

Whitez 05-29-2016 15:27

Re: Question about Natives
 
So we have the following

Spoiler


I want to add an optional, not mandatory third parameter to the forwards and the native, so i can use the third parameter in the infection bomb to tell in the rewards plugin that the infection was done with the bomb, and to not display the message

Example

Infection Grenade Plugin
PHP Code:



public grenade_explode(ent)
{
// Get attacker
    
new attacker pev(entpev_owner)
    
    
// Infection bomb owner disconnected or not zombie anymore?
    
if (!is_user_connected(attacker) || !zp_core_is_zombie(attacker))
    {
        
// Get rid of the grenade
        
engfunc(EngFunc_RemoveEntityent)
        return;
    }
    
    
// Collisions
    
new victim = -1
    
    
while ((victim engfunc(EngFunc_FindEntityInSpherevictimoriginNADE_EXPLOSION_RADIUS)) != 0)
    {
        
// Only effect alive humans
        
if (!is_user_alive(victim) || zp_core_is_zombie(victim))
            continue;
        
        
// Last human is killed
        
if (zp_core_get_human_count() == 1)
        {
            
ExecuteHamB(Ham_Killedvictimattacker0)
            continue;
        }
        
        
// Turn into zombie
        
zp_core_infect(victimattacker1)
    }


Rewards plugin
PHP Code:


public zp_fw_core_infect_post(victimattackercondition)
{
          if(
condition 1) return

         
// display a message to attacker


Along with this, i want to be able to call the native and like this, in case i don't want to send the extra param -> zp_core_infect(victim, attacker)

Whitez 05-29-2016 21:17

Re: Question about Natives
 
I am sorry blackrose, I misunderstood what num_params is for, i got how it works now thanks to you

I have found the answer to my question here
https://forums.alliedmods.net/showthread.php?t=245176

I don't know how i found it, i did over 20 searches on google with different keywords checking 5 pages for each search until that popped out

HamletEagle 05-30-2016 11:43

Re: Question about Natives
 
get_param_ byref should be used with "...".

klippy 05-30-2016 12:56

Re: Question about Natives
 
Quote:

Originally Posted by HamletEagle (Post 2423255)
get_param_ byref should be used with "...".

Also used when you actually pass arguments by reference, like so:
PHP Code:

native my_native(argPassedByValue, &argPassedByRef); 

you would then get the second one by using get_param_byref().


All times are GMT -4. The time now is 18:36.

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