How to Decrement an int variable. I have tried many approaches but nothing works

Hello, I am going crazy :slight_smile:
I have an int and I am using the Decrement to substract 1 from it every time an event occurs.
The decrement works only one time then the value is not substracted no matter how many times the pin is executed.

I have tried Decrement, I have tried setting the float subtracting -1 and setting it again, I have tried with functions and also macros with the same result.
1

In this image attached:

  1. The first Print String is executed,
  2. The int is decremented by 1
  3. Second Print String results correct as -1
  4. Second time, the print string is executed but this time the decrement does not work and the result is every time the same.

The odd thing is that if I’m triggering the this from a keyboard event the decrement works as expected.

Help please!

It’s not the decrement, it’s the rest of the code it’s connected to :wink:

That’s why it works when you try triggering from the KB.

1 Like

But in theory if the print string is triggered it should execute the code right?
I even added a timeline with an event track that should trigger the Execution of the substraction. That still didn’t work :frowning:

Decrement is working properly.
It’s likely your code is setup wrong.

  • Are you changing the int anywhere else (which would change the value back)?
  • Is this int a local function variable (which are reset every execution)?
  • Or is it a parameter (which is reset every call)?
1 Like

The variable lives only here. If it was changed from another place the keyboard decrement should not work also.

try printing the int before the decrement.
and when you print it again afterwards. get the value from the variable, instead of the decrement node just in-case.

OK. So, the variable is restarted every time from it’s initial value.
Something is resetting it but I have no idea what.

Just to make this clear, If you execute the print string it does not execute the decrement just because it’s integer pin is hooked to the print string. the decrement node only executes when it’s IN execute pin is called.

The variable starts from the initial value every time so the decrement works but the value returns to initial.
The workaround is by saving the variable in the GameInstance outside this actor.
Thank you guys for taking time to respond.
Happy coding!
Cheers!

1 Like