AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Creating and Deleting Variables at run time? (https://forums.alliedmods.net/showthread.php?t=48804)

pRED* 12-20-2006 19:45

Creating and Deleting Variables at run time?
 
Hey

Is there any way to create and delete variable at run time? Similar to malloc in C/C++

The plugin I am working on makes two different query_client_cvar calls and then prints the results to console on the same line.
As far as I can tell this means that I have to store the results from the first call in a global variable until the second one has been done and then print both results at the same time.

I would prefer to find a way to avoid using global variables since they will only be used very rarely and will just waste space the rest of the time.

Thoughts?

The Specialist 12-20-2006 22:48

Re: Creating and Deleting Variables at run time?
 
Global Variables take up about 4 bytes more of memory then local variables. The only reason to use global over local is because global variables are 0'd on load of the plugin , while local's have to be 0'd during running. (correct me someone if im wrong )

ADDED : which meens that using global variables are less CPU exspensive.

XxAvalanchexX 12-20-2006 23:50

Re: Creating and Deleting Variables at run time?
 
pRED* | NZ: you can't remove a global variable from memory once it has been allocated. There's nothing horrible about global variables, they're much more hospitable than the plague even, get used to them.

The Specialist: from experience, I would say that every variable, when created, if not assigned a value, is assumed a value of 0 (or false). You don't need to assign it anything specifically, even if it's local.

pRED* 12-21-2006 02:12

Re: Creating and Deleting Variables at run time?
 
I just thought having about 6 global variables that are only used rarely, permanently taking up memory space was a waste.

But if it's not really an issue i'll just leave it as is. Works fine as it is. :up:

Thanks

The Specialist 12-21-2006 02:13

Re: Creating and Deleting Variables at run time?
 
First of all , HI:) .

2nd , Thats not what i was saying

http://wiki.amxmodx.org/index.php/Optimizing_Plugins

I was correct , global variables are 0'd on loading , and local variables are 0'd dynamical . read the global variable VS local variable part. :wink:

pRED* 12-21-2006 02:23

Re: Creating and Deleting Variables at run time?
 
Quote:

Originally Posted by The Specialist (Post 417967)
First of all , HI:) .

Hi!

Quote:

Originally Posted by The Specialist (Post 417967)
global variables are 0'd on loading , and local variables are 0'd dynamical

Yea that's the exact reason I wanted to use locals. Since they are dynamic, the memory is released back to amxx/server once the function has finished exectuing.
Globals permanently occupy memory space as long as the plugin is running (so 24/7 for most servers) and may only be in use for about 30 seconds total per day..

The Specialist 12-21-2006 02:26

Re: Creating and Deleting Variables at run time?
 
but your missing the point , read the link i just posted here is a qoute from it . Dyamicly is a bad thing for varaibles , its slow an tedious (quote)

Code:


It is important to realize that every variable
in Pawn is automatically zeroed. For global
variables, they are static and permanent,
thus they are zeroed when your plugin is loaded.
Variables in functions, however, must be zeroed
dynamically. This is a slow and tedious operation,
and you should not only avoid relying on it when necessary,
 but you should keep that fact in mind when using arrays.

POINT : Dont use local variables unless you have to , to avoid assignemnt loss etc etc etcc .

pRED* 12-21-2006 02:34

Re: Creating and Deleting Variables at run time?
 
Yes I understand that.

Local Variables = More cpu time when the function is run due to allocating memory, 0'ing it and then releasing it after the function is completed.

Global Variables = More memory permanently taken up when it isn't needed.

I was weighing up the two options and thought that locals would be preferable since the plugin would probably be called once an hour or most likely less.
But don't worry since it isn't possible since query_client_cvar always returns the cvar value to a separate function.

Thanks for the help though. :up:

The Specialist 12-21-2006 02:36

Re: Creating and Deleting Variables at run time?
 
np. :)

pRED* 12-21-2006 02:39

Re: Creating and Deleting Variables at run time?
 
Just to clarify thought in case ive missed something (first time doing pawn)

Code:
public cmdrate_result_func(id,const cvar[],const value[]) {     copy(cmdrate[id],31,value) }

Thats the function that gets called by query_client_cvar and copies that cvar value into a global function.
Theres no way to get that cvar value (const value[]) back into the main function without using a global variable?


All times are GMT -4. The time now is 22:19.

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