AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Code syntax to reset variables to zero? (https://forums.alliedmods.net/showthread.php?t=107085)

TinCan 10-22-2009 14:28

Code syntax to reset variables to zero?
 
Trying to solve a bug in a plugin someone else made and wonder, does this one line of code set g_votes[0], g_votes[1] and g_votes[2] to zero?
g_votes[0] = g_votes[1] = g_votes[2] = 0;

Or do you have to do it like:
g_votes[0] = 0;
g_votes[1] = 0;
g_votes[2] = 0;

Exolent[jNr] 10-22-2009 14:30

Re: Code syntax to reset variables to zero?
 
They both do the same operation.

Hawk552 10-22-2009 14:32

Re: Code syntax to reset variables to zero?
 
Yes, the first statement works. What it does is this:

PHP Code:

g_votes[0] = g_votes[1] = g_votes[2] = 0

  • Step 1 -
    PHP Code:

    g_votes[2] = // let's call this statement "VOTES_2" 

  • Step 2 -
    PHP Code:

    g_votes[1] = VOTES_2 // let's call this statement "VOTES_1" 

  • Step 3 -
    PHP Code:

    g_votes[0] = VOTES_1 

At each step, the previous statement evaluates to whatever you're assigning to.

Hawk552 10-22-2009 14:41

Re: Code syntax to reset variables to zero?
 
Quote:

Originally Posted by Exolent[jNr] (Post 969322)
They both do the same operation.

Actually, this is not quite true. The second statement stores 0 in the DAT section and pushes it onto the stack three times, whereas the first only stores it once. So the operation in assembly will be, in pseudocode (cus I'm too lazy to write it up in proper ASM):

PHP Code:

DAT
adr001 0
adr002 0
adr003 0

CODE
array=adr001
array+1=adr002
array+2=adr003 

Whereas the other will look like:

PHP Code:

DAT
adr001 0

CODE
array+2=adr001
array+1=array+2
array=array+


Exolent[jNr] 10-22-2009 14:46

Re: Code syntax to reset variables to zero?
 
I was actually referring to the result, not the actual operation.
I guess I should start choosing my words carefully. :bacon:


All times are GMT -4. The time now is 17:40.

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