local or global variable in a timer/Using enum as integer
#1

Please, If you don't know what are you talking about don't reply, Or at-least try it before posting

First question, where does the local variables get saved and the global one, I already know the local get saved on the "heap/stack" which I think is the "memory" or "Ram", but what about the global ?

an example
pawn Code:
new Float:vehiclehealth;//this
forward VehicleTimer();
public VehicleTimer()
{
    new Float:vehiclehealth;//or this
    GetVehicleHealth(randomvehicleid, vehiclehealth);
}
from my perspective, the timer makes a new variable every second, and sets it to zero, then pass the health to it, so the global would be better, but I'm not sure

Second question: I've been trying to use an enum for sorting stuff, like dialog ids, without having to track them manually, but it gave me an error while using it inside another enum, like this
pawn Code:
enum
{
    BLABLA1 = 0,
    BLABLA2,
    MAX_BLABLAS
}
/*
#define BLABLA1         0
#define BLABLA2         1
#define MAX_BLABLAS     2
*/

enum blabla
{
    PlayerText:bbla[MAX_BLABLAS],
}

new Bla[MAX_PLAYERS][blabla];

public OnPlayerConnect(playerid)
{
    PlayerTextDrawShow(playerid, Bla[playerid][bbla][BLABLA1]);//warning 213: tag mismatch
    return 1;
}
When I use a define it works, but when I use an enum it doesn't, should this enum be like an integer, all it does is replacing "BLABLA1" to a number ?

thanks for your feedback
Reply
#2

Globals gets saved in the data section, locals in the stack
Creating normal local variables is only one opcode, also the speed should be the same as they are both loaded into the memory

For enums you need to define the tags for each "enum variable/constant" separately not for the array
pawn Code:
enum
{
    PlayerText: BLABLA1 = 0,
    BLABLA2,
    MAX_BLABLAS
}

enum blabla
{
    bbla[MAX_BLABLAS],
}

new Bla[MAX_PLAYERS][blabla];

public OnPlayerConnect(playerid)
{
    PlayerTextDrawShow(playerid, Bla[playerid][bbla][BLABLA1]);
    return 1;
}
Reply
#3

Quote:
Originally Posted by Nero_3D
View Post
Globals gets saved in the data section, locals in the stack
Creating normal local variables is only one opcode, also the speed should be the same as they are both loaded into the memory
Globals gets saved in the data section, what does that mean ? it consumes RAM too ?
if yes then whats the deference then, just that the local gets deleted after the function ends ?

About the enums it works, I didn't quite get why, Does the parameter take the sorting enum or the one in the other enum(blabla)

the sorting enum(as I get it) is just a number, if I use 0 instead of it it'll do the same job, then why do I have to put a tag before it and not the actual one.

Thanks.
Reply
#4

Yes, if you load a gamemode of filterscripts it gets loaded into the ram, where else
The difference is simple the data section is predefined while the heap/stack section is just a memory block which can be accessed dynamicly

Memory Image (see Implementer Guide): 0 =< Prefix < Code < Data < [Heap < Stack] <= MAX

Local variable get pushed on the stack and at the end of the statement they are freed

For enums the tag of the "enum variable" in the last bracket counts
Reply
#5

Thanks, One last question though, in terms of memory usage, they both use the same usage..
what about performance, speed..
Is
pawn Code:
function()
{
    new id, id2, id3;//lots of variables
    for(new i; i<100; i++
    {
        //Lots of codes settings and using id(s).
    }
}
exactly as fast as this
pawn Code:
function()
{
    for(new i; i<100; i++
    {
    new id, id2, id3;//lots of variables
        //Lots of codes settings and using id(s).
    }
}
Reply
#6

In my opinion, declare variables at the lowest possible level. Avoid globals.
Reply
#7

Vince is right, use globals only if you need them otherwise use locals

To your example the first is faster because the variables are only created / deleted once and inside the loop they would be created 100 times

Thats the reason why there is the first space to create the loop variable for the "for loop"
You could also rewrite a for loop to something like that
pawn Code:
function()
{
    // for(statement 1; statement 2; statement 3)
    { // limit the scoop of the variables to the loop
        new i, id, id2, id3; // statement 1

        while(i < 100) // statement 2
        {
            // CODE

            i++; // statement 3
        }
    }
}
But in OnPlayerUpdate I would use static variables (these are global variables limited to the function)
Because you shouldn't create variables inside a fast repeating code like OnPlayerUpdate or loops
Reply
#8

Thanks alot Nero and Vince, I'll try to check the speed difference though, "tickcounts" will do the job right ?

for now, I think doing it like this is both outside the loop and looks better.
pawn Code:
for(new var1 = something, var2 = somethingelse, i; i<blabla; i++)
{
    //Codes...
}
For some reason I don't like using "While" loops, does it have any advantages over "for" loop ?
Reply
#9

Only if you don't know how many times the loop should run. For example reading lines from a file. If you can count it, use a for-loop otherwise use a while-loop.
Reply
#10

I can't rep you enough, actually I can't rep you at all cause you're probably the last one I rep-ed, Thank you.

btw, is this your server in your signature ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)