Raised This Month: $ Target: $400
 0% 

Load-Once Function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-04-2009 , 07:04   Load-Once Function
Reply With Quote #1

Hello!
I can't test code right now so, I would like to know, how can I make so a function is only called ONCE (it's a load function that registers stuff, therefore calling it twice registers stuff twice).

Edit: I think it's worth mentioning that the function is called by multiple plugins.

Thanks in advance,
Vitor.
vitorrd is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-04-2009 , 09:29   Re: Load-Once Function
Reply With Quote #2

PHP Code:
public YourFunction( )
{
    static 
bool:bWasCalled;
    
    if ( 
bWasCalled )
        return 
PLUGIN_HANDLED;

    
bWasCalled true;

    
//your code

    
return PLUGIN_HANDLED;

__________________
Bugsy is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-06-2009 , 10:37   Re: Load-Once Function
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
PHP Code:
public YourFunction( )
{
    static 
bool:bWasCalled;
 
    if ( 
bWasCalled )
        return 
PLUGIN_HANDLED;
 
    
bWasCalled true;
 
    
//your code
 
    
return PLUGIN_HANDLED;

The question is: Are static variables shared within the context of all plugins or within the context of a single plugin?
vitorrd is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 10-06-2009 , 11:19   Re: Load-Once Function
Reply With Quote #4

Quote:
Originally Posted by vitorrd View Post
The question is: Are static variables shared within the context of all plugins or within the context of a single plugin?
Static doesn't take a part in that. Static is a same kind of memory keeping variable that global is but only usable in the function it's created. Obviously Bugsy's function would work, because it doesn't matter from where the function is called, the static var is only in the use of the func.
Somehow if you would add a public tag the static var would be usable not only in the whole plugin but also in other plugins. Somehow it would never change the static property, so doing a different variable for every plugin you'd need some other way.
SnoW is offline
Send a message via MSN to SnoW
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-06-2009 , 11:23   Re: Load-Once Function
Reply With Quote #5

Thanks for the clearing up, Snow. I wasn't sure about the environment plugins were executed in, it's nice to know they're all in the very same one.
vitorrd is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-06-2009 , 19:37   Re: Load-Once Function
Reply With Quote #6

Declaring a variable as static within a function will make that variable retain the value even when the function exits. The next time the function is called, the value (if any) from a previous call will remain. The variable is only accessible from within the function where it is declared unless it is passed byref to another function and is changed there; see below.

PHP Code:
register_concmd"test" "Test1" );

public 
Test1()
{
    static 
iVar;
    
    
Test2iVar );
    
server_print"Value = %d" iVar );
}

Test2( &iNum )
{
    
iNum++;

__________________

Last edited by Bugsy; 10-06-2009 at 19:46.
Bugsy is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-07-2009 , 02:33   Re: Load-Once Function
Reply With Quote #7

Thanks for the explanation, Bugsy, but that was not quite the point. The scripts must be executed by AMXX inside an environment, and I wasn't sure whether they were all executed in the same environment (making public globals accessible by other scripts, for instance, and static variables unically static at all scripts) or in separate environments (computationally exepensive anyway).

Thanks anyway
vitorrd is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-12-2009 , 10:50   Re: Load-Once Function
Reply With Quote #8

Turns out you guys were wrong about it. Making it static didn't help in any way, it is only static inside THAT environment. I need a way that will let a function to be called only once (it is an include file, therefore it is compiled twice, once in each of the two plugins using it).
vitorrd is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-12-2009 , 10:56   Re: Load-Once Function
Reply With Quote #9

Oh yea? Test this plugin and see if it works.
Code:
#include < amxmodx > public plugin_init( ) {     register_plugin( "Once-Used Function", "0.0.1", "Exolent" );     register_clcmd( "say /test", "CmdTest" ); } public CmdTest( client ) {     static bool:bUsed;     if( bUsed ) {         client_print( client, print_chat, "/test was already used!" );     } else {         bUsed = true;         client_print( client, print_chat, "First time using /test" );     } }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-12-2009 , 11:02   Re: Load-Once Function
Reply With Quote #10

I've already done that, Exolent.
vitorrd 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 22:41.


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