AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Load-Once Function (https://forums.alliedmods.net/showthread.php?t=105392)

vitorrd 10-04-2009 07:04

Load-Once Function
 
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.

Bugsy 10-04-2009 09:29

Re: Load-Once Function
 
PHP Code:

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

    
bWasCalled true;

    
//your code

    
return PLUGIN_HANDLED;



vitorrd 10-06-2009 10:37

Re: Load-Once Function
 
Quote:

Originally Posted by Bugsy (Post 952020)
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?

SnoW 10-06-2009 11:19

Re: Load-Once Function
 
Quote:

Originally Posted by vitorrd (Post 953966)
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.

vitorrd 10-06-2009 11:23

Re: Load-Once Function
 
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.

Bugsy 10-06-2009 19:37

Re: Load-Once Function
 
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++;



vitorrd 10-07-2009 02:33

Re: Load-Once Function
 
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 10-12-2009 10:50

Re: Load-Once Function
 
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).

Exolent[jNr] 10-12-2009 10:56

Re: Load-Once Function
 
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" );     } }

vitorrd 10-12-2009 11:02

Re: Load-Once Function
 
I've already done that, Exolent.


All times are GMT -4. The time now is 22:41.

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