#how do the += work

6 messages · Page 1 of 1 (latest)

ebon pier
#

i was working on a project that accelerating in this state : velocity.y = (velocity.y + gravity) * delta
but accelerated when in this state : velocity.y += gravity * delta
what's the difference and are there any other versons of += (also sorry I'm just starting to leran gdscript)

midnight loom
#

velocity.y = (velocity.y + gravity) * delta is different from velocity.y += gravity * delta.

delta is the time thats passed since the last frame of _process or _physics_process which is used to keep movement consistent even if the game lags or anything, and is very small.
velocity.y = (velocity.y + gravity) * delta is taking the entire sum of the current y velocity plus the gravity amount, and multiplying that entire sum by delta, and assigning that value.
velocity.y += gravity * delta is taking *just *the gravity amount and multiplying it by delta, and adding that value to velocity.y.

#

in other words, += is just a shorthand for saying "take the current value, add some amount to it, and reassign that new value to the variable.

#

there's also other versions that are similar for all the other typical math operators:
-= for subtraction
*= for multiplication
/= for division

#

hope that helps!

sleek meteor
#

basically
velocity.y += gravity * delta
is the same as
velocity.y = velocity.y + (gravity * delta)