AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   What do global variables do? (https://forums.alliedmods.net/showthread.php?t=132909)

nikhilgupta345 07-20-2010 00:43

What do global variables do?
 
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?

Bugsy 07-20-2010 00:57

Re: What do global variables do?
 
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.

nikhilgupta345 07-20-2010 16:04

Re: What do global variables do?
 
Thanks, now i understand :D

Lulu the hero 07-20-2010 19:51

Re: What do global variables do?
 
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);




All times are GMT -4. The time now is 07:09.

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