(Solved) Play sound every time the variable becomes smaller by 1

Hi,
maybe there is a simple solution, but I can’t get a working result.

I have a variable that slowly goes from 20 to zero. The scene variable(SurpriseDownTic)

The variable(PriceDelta) gives me whole numbers back. These numbers go slower and slower from 20, 19, 18,… towards zero.
How can I play a sound every time the variable(PriceDelta) decreases by 1 number?
From 20 to 19 to 18 to 17 …

Thank you.

Initialize a variable named NextPriceDelta to PriceDelta - 1

Then use:

PriceDelta <= NextPriceDelta
    Play sound
    NextPriceDelta subtract 1

To play a sound every time the PriceDelta variable decreases by 1 number, you can use a simple conditional statement in your code. Here’s an example of how you might do this using GDevelop:

// Declare a global variable to keep track of the previous value of PriceDelta
var prevPriceDelta = 0;

// In your event that updates the value of PriceDelta

// First, check if the current value of PriceDelta is lower than the previous value
if (PriceDelta < prevPriceDelta) {
// If it is, play a sound
AudioEmitter.PlaySound(“mySound”);
}

// Update the value of prevPriceDelta to the current value of PriceDelta
prevPriceDelta = PriceDelta;

This code will check if the current value of PriceDelta is lower than the previous value, and if it is, it will play a sound. The prevPriceDelta variable is used to store the previous value of PriceDelta so that it can be compared to the current value.

You can adjust the logic of this code to fit your specific needs. For example, you might want to play a sound only when PriceDelta decreases by a certain number of units, or when it reaches a specific threshold. You can also use different conditions in your conditional statement to control when the sound is played.

1 Like

Thank you Guys,

the second variable solved the problem.

2 Likes