View Single Post
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 12-28-2015 , 07:30   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #65

I've noticed that returning an increment variable ( return var++; ) returns garbage values a long time ago. So, right now, I decided to check the generated assembly. It really is a compiler bug! This code:
PHP Code:
public plugin_init() {
    for(new 
05i++) {
        
server_print("(%i) Test1: %i Test2: %i"iTest1(), Test2());
    }
}

Test1() {
    
#emit CONST.pri 1337
    
static var;
    return var++;
}

Test2() {
    static var;
    var++;
    return var;

Produces following output in server console:
Code:
(0) Test1: 1337 Test2: 1
(1) Test1: 1337 Test2: 2
(2) Test1: 1337 Test2: 3
(3) Test1: 1337 Test2: 4
(4) Test1: 1337 Test2: 5
Assembly for function Test1():
Code:
PROC			; stock Test1()
CONST.pri	0x539	; #emit CONST.pri 1337
INC		0xDC	; var
RETN
In PAWN, returned values are stored into the PRI register, and what happens here is that when you return an incremented value, the value never gets copied to the PRI register! As you can see, the value I put into PRI (1337) persists and is returned every time, even though it should get overridden by the value of "var".
So, when you return an incremented value, whatever was left in PRI (which could basically be anything) will get returned.

P.S. I know I am late with this reply several years, but whatever, someone might find this information useful.

Last edited by klippy; 12-28-2015 at 07:31.
klippy is offline