I have a player which as a flash light. If you press F the light goes on and out. But now I want to make that during the light is burning the variable battery (integer) will be Lower every second. So if the flash burns 1 second the battery lose as example 1%.
Can somebody help me?
Hi AVARSStudios,
This can be done fairly quickly with booleans and a timer. I’ll include screenshots of how I did it.
Note: I didn’t actually do anything with a light here, I just put print strings in to make sure everything worked correctly.
To start off with, I made 3 variables. A boolean named bIsLightOn, another boolean bIsBatteryAlive, and an integer Battery. Be sure to set all of these to public to avoid issues.
The first part is the timer. I set it to 1.0 for the time, so every second, and put the Light function into it. We’ll see the function later. I then put a check in on Event Tick to check for when Battery runs out of juice. I then set up the toggling of the flashlight.
I then made a new function called Light and placed these nodes into it. The first branch checks to see if the light is on, if that’s true, it goes onto the next to check if the battery is alive. If it passes that, it’ll start subtracting 1 from battery. Seeing as the timer is set to loop, it’ll loop this every second and only subtract if the light is on. You can ignore the print strings, those were there for me to test if the function was doing what it was suppose to.
Hope this helps!
Thanks. But the timer don’t stops when the battery is low. And cam tell me how to do it that light goes out when the battery is low?
I found the mistake why it doesn’t stop
If you wish to stop the timer, instead of setting looping to true on the timer, you can have it check the bIsBatteryAlive variable to see if it should loop. This means that when the battery dies, it’ll stop looping.
And to make that the light goes out when the battery is low and the flash is on?
You could set the line of nodes that you use for the light to actually be turned on to check for if bIsBatteryAlive is true with a branch statement. Otherwise you can modify both the ‘To toggle flashlight’ and ‘Checks to see if battery is alive every frame’ to set bIsLightOn to false when bIsBatteryAlive is false and never let it be set to true unless bIsBatteryAlive is set to true again.
How it looks like in the blueprint?At the moment I don’t know how you mean this.
thank you very much for your great help!
This should explain it. This will only work if you’re function to turn the the light on and off is checking the state of bIsLightOn to determine what to do.
At which node i put the toggle visibility for the flash light when the battery is low? There where it checks if the battery is alive? I have tried everything but the light don’t turn out when the battery is low…
I would need to see what your blueprint looks like as I am unsure of what you’re doing to actually turn the light on and off.
This is my blueprint. the light goes on and off when i press f and the battery is higher than 0. but when the battery is 0 and the light is on the light should be turn out.
This should do it, I added a branch statement at the beginning that is checking for bIsBatteryAlive and stopping the rest of the nodes if it isn’t, setting bIsLightOn to false, and toggling the visibility if it is visible.
It dont works. The light burns but the battery is 0. The light cant be turn when it is 0. Yes this works. But the light is burning when the light is on and then the battery is 0
Can you send me your project itself? The problem must be elsewhere as this should work for your needs.
I have found the problem. the branch node have to be at the event tick node. Now it works
Thank you very much!!!
You’re welcome!