Raised This Month: $ Target: $400
 0% 

functions and vars


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 01-06-2019 , 08:36   functions and vars
Reply With Quote #1

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".
__________________








CrazY. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-06-2019 , 10:27   Re: functions and vars
Reply With Quote #2

No you cant, variable is not a function.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-06-2019 at 10:40.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-06-2019 , 10:30   Re: functions and vars
Reply With Quote #3

get_func_id() + callfunc_begin_i()
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
eat1k
Senior Member
Join Date: Apr 2018
Old 01-06-2019 , 11:39   Re: functions and vars
Reply With Quote #4

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);
__________________
eat1k is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 01-06-2019 , 12:39   Re: functions and vars
Reply With Quote #5

The function is just an example actually. Anyway, I will have to opt for switch case.
__________________








CrazY. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-06-2019 , 13:44   Re: functions and vars
Reply With Quote #6

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).
__________________

Last edited by HamletEagle; 01-06-2019 at 13:45.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2019 , 16:48   Re: functions and vars
Reply With Quote #7

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.
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

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 07:31.


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