Raised This Month: $ Target: $400
 0% 

What do global variables do?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 07-20-2010 , 00:43   What do global variables do?
Reply With Quote #1

I've been looking through the source of different plugins, and many of them have variables declared like "new g_varname". These are global variables right? What do they do, and how are they different from regular variables?
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-20-2010 , 00:57   Re: What do global variables do?
Reply With Quote #2

http://en.wikipedia.org/wiki/Global_variable

In a nutshell, they are accessible throughout the plugin and retain their value similar to a static variable. Global variables are not limited to the Pawn language or AMX-X. You can search the net for "global variables" to find more info.
__________________

Last edited by Bugsy; 07-20-2010 at 01:17.
Bugsy is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 07-20-2010 , 16:04   Re: What do global variables do?
Reply With Quote #3

Thanks, now i understand
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Lulu the hero
Senior Member
Join Date: Oct 2009
Location: Budapest, Hungary
Old 07-20-2010 , 19:51   Re: What do global variables do?
Reply With Quote #4

There are two types of varibles in programming languages:
1) global
2) local

Global can be accessed from the whole plugin, while local variables can only be accessed in the function where you declared it.
PHP Code:
new global = 17;

public 
something(){
  new 
local=32;

  
//this will write out 17
  
console_print(0,"%i",global);
  
  
//this will print out 32
  
console_print(0,"%i",local);
}

public 
other(){
   
//this will write out 17
  
console_print(0,"%i",global);
  
  
//this will cause an error, because the "local" can only be accessed in the something function
  
console_print(0,"%i",local);

Lulu the hero 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 07:09.


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