Raised This Month: $ Target: $400
 0% 

[solved] clear function's global variable


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 04-14-2009 , 05:08   [solved] clear function's global variable
Reply With Quote #1

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...
__________________
Hunter-Digital is offline
G-Dog
Senior Member
Join Date: Dec 2005
Location: Thunderstorm Central
Old 04-14-2009 , 06:50   Re: clear function's global variable
Reply With Quote #2

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.
__________________
If at first you don't succeed, then skydiving isn't for you.

Last edited by G-Dog; 04-14-2009 at 07:37.
G-Dog is offline
Send a message via AIM to G-Dog
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-14-2009 , 07:08   Re: clear function's global variable
Reply With Quote #3

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

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 04-14-2009 , 08:35   Re: clear function's global variable
Reply With Quote #4

Quote:
Originally Posted by G-Dog View Post
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 :}
__________________
Hunter-Digital is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-14-2009 , 09:47   Re: clear function's global variable
Reply With Quote #5

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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 04-14-2009 , 09:56   Re: clear function's global variable
Reply With Quote #6

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.
__________________
In Flames we trust!
Nextra is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 04-14-2009 , 12:05   Re: clear function's global variable
Reply With Quote #7

Quote:
Originally Posted by Hunter-Digital View Post
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
__________________
Hunter-Digital is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-14-2009 , 09:56   Re: clear function's global variable
Reply With Quote #8

@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.
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-14-2009 , 09:58   Re: clear function's global variable
Reply With Quote #9

Quote:
Originally Posted by arkshine View Post
@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

?
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-14-2009 , 12:08   Re: clear function's global variable
Reply With Quote #10

Yeah, for that, & is fine. No need to use enum and such.
Arkshine 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 02:24.


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