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 , 08:35   Re: clear function's global variable
Reply With Quote #1

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 #2

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 #3

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 #4

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
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 02:24.


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