AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [solved] clear function's global variable (https://forums.alliedmods.net/showthread.php?t=90103)

Hunter-Digital 04-14-2009 05:08

[solved] clear function's global variable
 
I have a function and I send it a variable, a global one, and I was wondering how I get what variable name I sent to the function in order to clear it inside the function ?

Code:

new g_iTest[3]
 
//...
 
g_iTest[0] = 982 // just a number
 
//...
 
clearVar(g_iTest[0])
 
//...
 
stock function clearVar(var)
{
      /* ??? = 0 ?? */
}

I don't know the actual variable name I will send, I have many arrays that I use to send through that, that's why I search for an automatic way to get it...

G-Dog 04-14-2009 06:50

Re: clear function's global variable
 
Code:

stock clearVar(&var)
{
    var = 0;
}

edit @ Connor: from what I understood he basically wants to modify whatever variable he is passing to the function in which case all he needs to do is reference the variable passed so that any changes made in the function will alter the global variable. If I misunderstood what he meant then sorry.

ConnorMcLeod 04-14-2009 07:08

Re: clear function's global variable
 
You would have to make something like this :
PHP Code:

enum GLOBALS {
    
time,
    
adminnums,
    
whatever
}

new 
g_Globales[GLOBALS]


ClearGlobalGLOBALS:var )
{
    
g_Globales[var] = 0



Hunter-Digital 04-14-2009 08:35

Re: clear function's global variable
 
Quote:

Originally Posted by G-Dog (Post 805117)
Code:

stock clearVar(&var)
{
    var = 0;
}

edit @ Connor: from what I understood he basically wants to modify whatever variable he is passing to the function in which case all he needs to do is reference the variable passed so that any changes made in the function will alter the global variable. If I misunderstood what he meant then sorry.

Would...
Code:

new g_iVar1 = 0
new g_iVar2 = 0
new g_iVar3 = 0
 
//...
 
g_iVar1 = 1
g_iVar3 = 1
 
clearVar(g_iVar1)
clearVar(g_iVar3)
 
//...
 
stock clearVar(&var)
{
      var = 0
}

...set g_iVar1 and g_iVar3 to 0 ?

I'm using them for entitiy ID reset, so I would not need to write in every function "if is_valid_ent -> remove entity then set var to 0" instead just a function :}

ConnorMcLeod 04-14-2009 09:47

Re: clear function's global variable
 
I don't think so :

clearVar(g_iVar1) doesn't pass a var but it's value

The way i showed you pass something that lets you identify the var, should be other/better ways though.

Arkshine 04-14-2009 09:56

Re: clear function's global variable
 
@Hunting-Digital : G-Dog's method is perfectly fine with your exemple. But depending you want to do, using an enum could be more readable.

Nextra 04-14-2009 09:56

Re: clear function's global variable
 
PHP Code:

#include <amxmodx>

new g_Test;

public 
plugin_init( )
{
    
register_plugin"Bleh""Blah""Bluh" );
    
    
register_clcmd"say /test""test" );
}

public 
test( )
{
    
g_Test random_num1100 );
    
    
client_print0print_chat"g_Test was randomized: %d"g_Test );
    
    
resetg_Test );
    
    
client_print0print_chat"Cleared. g_Test is now: %d"g_Test );
}

reset( &iVar )
    
iVar 0


Following output:

Code:

g_Test was randomized: 34
Cleared. g_Test is now: 0
Nextra :  /test

Resetting globals byref is possible.

ConnorMcLeod 04-14-2009 09:58

Re: clear function's global variable
 
Quote:

Originally Posted by arkshine (Post 805230)
@Hunting-Digital : G-Dog's method is perfectly fine with your exemple. But depending you want to do, using an enum could be more readable.

Ops, exact.

Anyway, why don't you directly do :

g_var = 0

?

Hunter-Digital 04-14-2009 12:05

Re: clear function's global variable
 
Quote:

Originally Posted by Hunter-Digital (Post 805178)
I'm using them for entitiy ID reset, so I would not need to write in every function "if is_valid_ent -> remove entity then set var to 0" instead just a function :}

that means, instead of:
Code:

if(is_valid_ent(g_iAnEnt))
        remove_entity(g_iAnEnt)
g_iAnEnt = 0

on every function that I need to do that... I could just:
Code:

clear(g_iAnEnt)
clear(g_iAnotherEnt)
clear(g_iAndAnotherEnt)
 
//...
 
stock clearEnt(&ent)
{
        if(is_valid_ent(ent))
                remove_entity(ent)
        ent = 0
}

This is solved, thank you guys :P

Arkshine 04-14-2009 12:08

Re: clear function's global variable
 
Yeah, for that, & is fine. No need to use enum and such. :)


All times are GMT -4. The time now is 02:24.

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