Need help with creating a generator (4-27)

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 :heartpulse:

1 Like

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

1 Like

Hello, I have two new problems:

  1. 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.
  2. 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

    :sweat:

The outline purple area blocks the E key when away from the pump

This purple X is on a wire you can not use

This is what you tick should look like: just remember the out of fuel wire goes to lights off

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.

1 Like

Hey, a few thing think could help you.

  1. 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.

  2. 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.

  3. With this array of lights, you can now loop through them and do what you need:

  4. For the interaction of the light, I’m running an event calling an interface. This is the event:

  • First I toggle a bool (bIsOn) to know when the generator is on or off.
  • Then I select the material to glow green if on, black if off.
  • I toggle the tick event (more on that later)
  • and for last I tell the lights to update with the event in # 3.
  1. For the tick event, this will only run when the generator is on.
  • 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.
  1. And last, the interface, how the player character interacts with the generator:
  • The branch checks if there is fuel. If there is, then toggle on or off.

This is everything in the generator blueprint:

This is how it works: notice I can turn on / off and it shuts down when fuel <= 0.
Generator00

2 Likes

Wish I could post faster… now it looks like I just did what you recommented. lol

Sorry I was trying not to overwhelm the OP, small steps! little math to show logic flow ideal for new user lacking in knowledge of BPs.

Think a good way to teach is to challenge others, and hopefuly learn something in the process.

Like me when overthinking what I did above, felt like an idiot when I found out this other way is easier to understand and does the exact same thing: :rofl:

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

1 Like