• General Chat
  • 2d energy/power/electricity implementation techniques

Hello everyone! You have probably heard of the game Rimworld and Prison Architect or Factorio. They are all 2d topdown games that have some kind of grid... I mentioned these games because I want to know how to implement that kind of power/electricity/energy they all have. I mean this

I am not talking about practical stuff like creating nodes and scripts and stuff... I am talking about the idea of having some kind of power source that distributes power using wires. And when two wires are not connected, the power flow is interrupted. Should one have a 2d array in which they store 0s and 1s where 1s correspond to "Is electricity"?

Have you done something like that? I am open for any kind of idea, thank you!

Please ask if you want more explanation :smile:

7 days later

This isn't a direct answer, but I'm also working on developing a top down 2d power system for a starship simulator game I'm designing which has some inspiration in rimworld. I have made a nuclear reactor simulation and each system on the ship can have power allocated to it.

I have yet to implement the power conduits to the systems but you've got me brainstorming. If its going to be like rimworld than each length of wire on the grid needs to have a state that indicates if it is functional, if its on fire, etc. I'm unsure whether each length should be its own object, or if there should be a "trace" object that stores all the points and their state.

My case is simpler since I'm not letting the player design a grid, but you might think about defining a path and then using signals to stop the flow if something is disturbed. Sorry its late :) But I'll try to post back when I figure out how I'm going to do it.

4 days later

@alshady said: This isn't a direct answer, but I'm also working on developing a top down 2d power system for a starship simulator game I'm designing which has some inspiration in rimworld. I have made a nuclear reactor simulation and each system on the ship can have power allocated to it.

I have yet to implement the power conduits to the systems but you've got me brainstorming. If its going to be like rimworld than each length of wire on the grid needs to have a state that indicates if it is functional, if its on fire, etc. I'm unsure whether each length should be its own object, or if there should be a "trace" object that stores all the points and their state.

My case is simpler since I'm not letting the player design a grid, but you might think about defining a path and then using signals to stop the flow if something is disturbed. Sorry its late :) But I'll try to post back when I figure out how I'm going to do it.

I came with an idea that is quite okay for small maps. I also made a quick project for it. I have a 2d array that has 1 in each y,x if there is a wire. If I need to check if two things are connected, I use floodfill :) Good luck on developing your game ;)

9 days later

I have no clue how they do it, but linked lists came to my mind. Or some sort of Circuit class that simply stores 1D lists of coordinates for where the cables go and flags for missing links or connections to other circuit objects or special circuitry tiles.

4 years later

The best way I can think of doing it is with Godot’s built-in AStar class. I’m not certain, but I’d guess my way is also faster than yours by quite a bit. Start with AStar’s empty point array. Add the point of the power source. For every wire location x, add a new aStar point. Look at points on your grid adjacent to x, if there are points or “wires” there, it’s AStar’s connect points function. This connects wires that are together on the grid.

When placing an object O that requires power, take the following steps: Use Astar’s function get_closest_point(O.location). If the closest AStar point is adjacent, ask AStar to make a path from that point to any of your power suppliers. If it can’t make a path to any of them, it gets no power, else it does. This way you can also find out where it’s drawing power and subtract power appropriately like how in prison architect the power supply has limits. Keep object O powered every frame.

When a point is added or removed from the power line grid, ask all power-needing objects to re-evaluate their connection to a power supply.

Using AStar has the added bonus that you can disable points from being used to form paths, meaning every point already has the functionality of being a switch. So, for example, if you wanted the wire to quit working b/c it was damaged or something, you could just disable that point.

As far as I know the only downside is that AStar doesn’t seem to be multi-thread safe. Also. I LOVE those games you listed sooo much. Probably 2000+ hours combined in those three.

Thanks for this advice, I think it's the easiest way. I'll have to try working with AStar, as I've heard of it but haven't gotten around to it. I have an idea to repeat a similar game, but to make it a game for young electricians, with all the intricacies of electrical networks. But the problem is that I don't know anything about electronics. I just recently needed to fix a socket and I couldn't have done it without electricians from https://gordonpowers.com.au/inner-north/. I also just need to brush up on my knowledge of physics in this area if I want to do a very realistic project.