AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   functions and vars (https://forums.alliedmods.net/showthread.php?t=313351)

CrazY. 01-06-2019 08:36

functions and vars
 
Hello, I would like to know if I can store function in variable to then call directly from it, something like the following

Code:
new g_iFunction; MyFunction(this) {     // Do Something... } MyFunction2(this) {     new Players[MAX_PLAYERS], iPlayerCount;     g_iFunction = MyFunction;     g_iFunction(Players[random(iPlayerCount)]); }

I need this because I'm working with a large number of functions, so would be more easy to store the function and call it directly instead of use "switch" and/or "if/else if".

Natsheh 01-06-2019 10:27

Re: functions and vars
 
No you cant, variable is not a function.

OciXCrom 01-06-2019 10:30

Re: functions and vars
 
get_func_id() + callfunc_begin_i()

eat1k 01-06-2019 11:39

Re: functions and vars
 
I think I understood correctly, here is an example.

Code:
func_GetPlayersNum(&iTs, &iCTs) {     new iPlayers[MAX_PLAYERS], iPlayerCount;     get_players_ex(iPlayers, iPlayerCount, GetPlayers_ExcludeDead);     for(new i; i < iPlayerCount; i++)     {         switch(get_member(iPlayers[i], m_iTeam))         {             case TEAM_TERRORIST: iTs++;             case TEAM_CT: iCTs++;         }     } }

Then:

Code:
new iCTs, iTs; func_GetPlayersNum(iCTs, iTs);

CrazY. 01-06-2019 12:39

Re: functions and vars
 
The function is just an example actually. Anyway, I will have to opt for switch case.

HamletEagle 01-06-2019 13:44

Re: functions and vars
 
There are no pointers in pawn, so you can't do that directly. However, you can do as OciXCrom said and use callfuncs.
If the syntax is ugly or you want to keep it short, you can always create a macro or wrapper function that will do all the callfunc logic: callfunc_begin + push + end. Such function should take as the first argument an enum which will determine what function to call(that is assuming all your functions that need to be called have the same header).

fysiks 01-06-2019 16:48

Re: functions and vars
 
It sounds like you are just trying to make it so that you just write less code. If this is the case, you should really rethink this approach. You shouldn't try to do something like this just because you want to write less code. You need to have some other significant reason.

I bet that you could architect your code better so that it's not as bad as you think it is going to be. However, we can't help you with that since you've not given any information about what you are actually trying to do.


All times are GMT -4. The time now is 07:31.

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