Hello, I’m new to Unreal Engine and I’m trying to write a blueprint of a generator, I have a building in which all the electrics work with a generator, I was able to turn on all the electrics when interacting with it, but my goal is to add the fuel that it will work on, now I have this blueprint:
(Pressing the E key on the generator turns on the light in the entire room)
I’m sure that the code could be optimized and reduce the number of lights, but I’m really new to this
I will be very grateful for your help
Create Variable named FUEL of type float set your default value say 1000
Create Variable named COSTPER type float set your default value for each cycle of tick say 0.001
Create Variable named RUNNING of type boolean set to false
E->FlipFlop->SetVisiblity true node drag wire and set variable RUNNING = true
E->FlipFlop->SetVisiblity false node drag wire and set variable RUNNING = false
Add Event Tick:
Add the Sequence Node
First sequence: Branch condition on RUNNING
true Subtract COSTPER from FUEL
false do nothing
Second Sequence: Branch condition on FUEL less equal 0
true drag wire to E->FlipFlop->SetVisiblity false node
false do nothing
For some reason, turning on the light works from anywhere on the map until the moment of interaction with the generator, after turning on and off the input works correctly, only near the generator.
I am 99.9% sure that it was me who compiled the blueprints incorrectly, what you wrote does not work for me, you already helped, but I need your help again
You want COSTPER to be something like CostPerSecond. You then want to multiply the DeltaTime with CostPerSecond, and subtract the result of that from Fuel. The reason is that tick rates will be different on different computers, and will even vary on the same computer based on load.
Regarding all the objects: Make an array of type PointLight (click the little blue line next to the variable type, switch it to the matrix/array kind.) Then add each light you want to be controlled to that array on the instance of the generator, in the level editor. Then, when turning on/off, simply use a for loop over all the items in the array, and turn them on/off that way – much cleaner code.
I have no idea why “turn on” works from anywhere – sounds like a bug in your interaction code. Typically, objects that can be interacted, will have a collision sphere/shape for where the player must be, and in the “on overlap started” callback, they add themselves as a possible interaction to the player, and in the “on overlap ended” callback, they remove themselves. When the player executes “interact,” it checks that there’s something to actually interact with, and then calls it back. Typically this, in turn, is mediated with some simple interface, implemented by the thing that can be turned on/off.
It’s good practice to keep al input interaction with the character. think you are using level blueprint for this, but since you are already creating a blueprint for the generator, consider using the generator as the one to handle the logic for your lights.
All those lights… you can make the engine work for you… For Example:
Here you can tell the engine on Begin Play event to search for all lights and add them to an array. In this example told the engine to search those with a specific tag allowing to have more than one generator or limit the lights affected.
With this array of lights, you can now loop through them and do what you need:
First: I used a float variable that holds current fuel. Used a variable that I can tweek to deplete X amount of fuel per second. For this you need to do the math expression in the image. The math should be self explanatory.
the print node is just for debug
And last, the branch checks if fuel is less than 0. If true, toggle the generator to turn it off.
And last, the interface, how the player character interacts with the generator:
Hello again, I didn’t have time to thank you then, but I want to do it now, thanks to everyone who responded and answered in this thread, this is my first experience in UE and I don’t know how I would have done some things without you, I haven’t implemented everything yet and I will work on the game further, so maybe we’ll see you again on the forum, thanks to all of you :3
Game I’ve been working on: https://globalgamejam.org/2022/games/test1-8