static and new
#1

what make static and new different? can you explain it to me?


Sorry for my bad english
Reply
#2

Hey Raefal,

thats a pretty easy question and also the answer is pretty easy (also sorry for my english).

Static Variables will just get instanced once, so everytime it is called again, it is the same reference. If you are using new, the engine will create a new instance, everytime it is called again.
Static variables can just be used when you do not have any variable variables for instancing it.

I hope I helped you.

Regards,
Max
Reply
#3

View mode:

new:
Код:
for (new i = 0; i < 3; i++)
{
    new
        j = 1;
    printf("%d", j);
    j++;
}
Код:
1
1
1
static:
Код:
for (new i = 0; i < 3; i++)
{
    static
        j = 1;
    printf("%d", j);
    j++;
}
Код:
1
2
3
Read more here https://sampwiki.blast.hk/wiki/Scripting_Basics
Reply
#4

pawn lang says

Quote:
Originally Posted by pawn-lang
When a local variable is declared with the keyword static
rather than new, the variable remains in existence after
the end of a function. This means that static local variables
provide private, permanent storage that is accessible only from a singl
e function (or compound block). Like global variables, static local varia
bles can only be initialized with constant expressions.
meaning your variables are still there after execution of some function
pawn Код:
myfunction()
{
    static x=5;
}
after execution of "myfunction" the variable "x" is still somewhere in the memory.

simple test script to prove that it's still somewhere in the memory:
pawn Код:
#include <a_samp>

main(){}

public OnFilterScriptInit()
{
    printf("static abc: %d",ReadAmxMemory(myfunc()));
    return 1;
}

myfunc()
{
    static abc=200;
    return ref(abc);
}

ref(...)
{
    assert(numargs() == 1);
    #emit load.s.pri 12 // first argument
    #emit retn
    return 0; // make compiler happy
}

ReadAmxMemory(address)
{
    #emit lref.s.pri address
    #emit retn
    return 0; // make compiler happy
}
it'll print

static abc: 200
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)