probleme with auto increamentation (++, +=) Operators

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
hi am trying make a easy way to auto increase a variable .


in this example, so 


when i showPicture , the var PID and defxy[1] need to increase after show.
Is work good for ++, but not the +=


function ActionKey(WhatToDo) {
var PID = 83; // default PID for actionkey huds
var defxy = [1200,500]; // default x y pos for actionkey
var PH = 125; // picture fixed heigth from Action-Key.png
if (WhatToDo==='show') {
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
}
}






The way clearer.
Why does this return me 5, 6 and not 5, 5  ?


var A = 5;
var B = 5;
console.log((A++)+ ' ' + (B+=1)); // return 5 6




That fµ©£ me, all the easy way, with which I would like make my code.
Is there a way? or is a buggy javascript ?
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
The way clearer.
Why does this return me 5, 6 and not 5, 5  ?



var A = 5;
var B = 5;
console
.log((A++)+ ' ' + (B+=1)); // return 5 6


It returns 5,6 instead of 5,5 because += sets it to 6 before the return call..


++ adds but returns the original value


+= adds and returns the new value


if showing it similar to a code makes more sense

Code:
// var ++ works like this

var A = 5
var A_original = A
A = A + 1
return A_original

// var += value works like this

var A = 5
A = A + value
return A
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
exist somes ways to syntaxe ++ returns the original value , but with a variable ?


example:

Code:
A = 5;
B = 5;

Console.log( (A++B) ); // example return 5
Console.log( (A++B) ); // example return 10
Console.log( (A) ); // example return 15
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
IDK if that's possible, but why not just make it such that your variables are


var PH = 125;
var defxy = [1200,500-PH];



so that your first defxy[1] += line will return 500 which is the first value that you want to use
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
@Engr. Adiktuzmiko


The complete code is a little more laborious.
I had purified it for my question.


But thank you, I think that answers my question.
we cannot
I will do as I usually do.


EDIT: HAHA ok '  why not just make it such that your variables '


ok is true, Thank you I was tired
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I'm not sure if you really cant, and I can't test so I just gave the most plausible answer that I can think of.


You can still test A++B in case it might actually be doable


now if you can't do my suggestion of still using += with your original variable reduced by the first increment (but IDK of a situation where that cannot be done so far), then you could just do


if (WhatToDo==='show') {
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1], 100, 100, 255, 0);
defxy[1]+=PH;
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1], 100, 100, 255, 0);
defxy[1]+=PH;
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1], 100, 100, 255, 0);
defxy[1]+=PH;
}




EDIT : so you can do the 500-PH edit?
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
I


EDIT : so you can do the 500-PH edit?


Yes i test and it worked impeccably it was the way I wanted to build my code.
It suffisait d'y thought, I don't regret my question you helped.
Thank you.


Example: for auto incrementation


function ActionKey(WhatToDo) {
var PID = 83; // default PID for actionkey huds
var PH = 125; // picture fixed heigth from Action-Key.png
var defxy = [1200,500-PH]; // default x y pos for actionkey

if (WhatToDo==='show') {
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
$gameScreen.showPicture(PID++, 'Action-Key', 0, defxy[0], defxy[1]+=PH, 100, 100, 255, 0);
}
}


@  
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,845
Messages
1,016,961
Members
137,561
Latest member
JaCrispy85
Top