AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Calling a function by string (https://forums.alliedmods.net/showthread.php?t=246001)

PreDominance 08-10-2014 00:32

Calling a function by string
 
Greetings,
In making my warcraft mod, I'm stuck with "how do I call a player's race_attack skill, with abstraction as key?". The easiest way would be to have the player's race_attack skill have a specific format (#RACENUM#_skill_attack_#NUM#), and then call that function via a formatted string.

The only way I know to call a function via a formatted string is by set_task. However I need to execute that function immediately, not 0.1 seconds after (0.1 seconds is fine for spawn-related functions, but I need immediate results when it comes to player_hurt).

So, how do I do something like the following:
PHP Code:

new fncCall[20];
...
format(fncCallcharsmax(fncCall), "%s""myFunc");
callFunc(fncCallargs[]);
...
public 
myFunc(args[]) {} 

My other option is to loop through every race, comparing the user's race id and then executing the correct one, but I'd rather not do that on every player attack.

fysiks 08-10-2014 02:56

Re: Calling a function by string
 
A question like this makes me think that you need to do a redesign of your overall program architecture, I believe this is an "XY Problem". It also seems that this is a continuation of this thread.

If you would like any good advice on what to do instead of what you are doing, you'll need to explain what exactly you are trying to do (without mentioning specific code) i.e. the overall design of your program.

At first glance, I would have to say it will probably be related to multiforwards or something similar.

PreDominance 08-10-2014 03:44

Re: Calling a function by string
 
It actually has nothing to do with my other thread; I was going to have races coded 100% externally (now I'm just parsing through race configuration data, looks pretty though!). This one is referring to executing functions.

Let me clarify my problem for you then (I tried to in my first post), to avoid the XY problem. I'll assume you have a basic understand of a warcraft mod.

Each race in my mod is nothing more than an .inl file with functions and declarations. Each race file needs to have functions that need to be executed based on various events. These events are basic: player_attack, player_spawn, player_jump, etc.
Here's the question: how do you know which event to execute? Each player has nothing more than a number to discern his/her race. I can do something like:
PHP Code:

public event_player_killed(victimattackershouldgib
{
    
nwc_checkChangeRace(victim);
    if (
P_DATA[attacker][PB_HAS_ATTACK]) {
        switch(
P_DATA[attacker][P_RACE]) {
            case 
EVERY_RACE_WITH_ATTACK_SKILL?!
        }
    }


However, if I can simply format a function call, I can (knowing beforehand that I've formatted the function names correctly) merely do this:
PHP Code:

public event_player_killed(victimattackershouldgib
{
    
nwc_checkChangeRace(victim);
    if (
P_DATA[attacker][PB_HAS_ATTACK]) {
        new 
szAttackSkill[50];
        
format(szAttackSkillcharsmax(szAttackSkill), "%i_attack"P_DATA[attacker][P_RACE]);
        
callFunc(szAttackSkill);
    }


*Edit

So after more snooping I found the callfunc native. Why would this not be the perfect solution? Let's find out & read some more.

fysiks 08-10-2014 11:23

Re: Calling a function by string
 
Inline files are not for building API's like you are trying to do. If you use inline files, you are essentially hardcoding everything, they are only useful for organizing your code. If you are actually wanting to make things dynamic, you need to build an API that allows you to "register" your race which sets up multiforwards for your race specific actions.

PreDominance 08-10-2014 11:55

Re: Calling a function by string
 
Hawk552 also told me this as an alternative, and maybe I'll look into it at another time. For now, I have exactly 6 days to get a beta version of this mod out and running because I'll be heading back to school wherein I need time to move back in, see old friends, drink, and actually do school. I hate to sound ignorant but I really don't have time to redesign everything I've done so far.
Can you tell me why the callfunc native wouldn't be ideal?

Kia 08-10-2014 12:43

Re: Calling a function by string
 
Without reading the entire thread, this is how you can call a function.
Code:
if( callfunc_begin("myfunc", "myplugin.amxx") == 1 ) {     callfunc_push_int(id)     callfunc_end() }

PreDominance 08-10-2014 12:48

Re: Calling a function by string
 
Thank you Kia, I did find it but only recently. This opens up a world of opportunities, though.


All times are GMT -4. The time now is 12:58.

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