Question about looping, sorry if its a dumb question, new guy over here

I have a question and it is probably easy for you guys to answer as I am very new to gdevelop. I am trying to build a circuit simulator with a friend familiar with javascript. I have already done the drag-and-drop stuff with a lightbulb and a battery to be placed on the grid. My question is how would I be able to check if it is an open or closed circuit.

I have asked some people and they mentioned implementing recursive or looping algorithms. I’m not too sure about how to do this, was wondering if someone can help me out or help me find an easier way to do the same task.

I can give further clarification of what I need help with if needed.

@GESHY47 Hi. Without knowing your implementation or your requirements, I can only comment on how I would do it without having vast electrical knowledge, so apologies if there are any inaccuracies in my terminology.

Considering that computers work with binary data because of how electrical currents are handled (0: no electricity, 1:electricity) and given how computer information is used as a representation, and not an exact copy of reality, you can consider creating a boolean expression with a variable that holds integer values in order to represent the state of the circuit.

So if electricity is applied, i.e the circuit is closed, assign the boolean variable a value of 1, otherwise assign it a value of 0 to represent the circuit is open. Once you do this then all your other code will depend on this variable. It would be basically a logic gate. I would call the variable is_closed, as if asking a question and if you only manage a single circuit it could be a global variable, or if you have different circuit cases in different scenes use a Scene varaible.

You don’t need to really create a complex system to check if it’s open or closed, since you can consistently assume from knowledge and observation that a circuit can only assume these two states.
If you need more states, you can then map them to more numbers and if using javascript for real you can create a dictionary using the keys for the state names with their respective numerical values (in other languages this could be considered a named ENUM[erator])

If your is_closed variable is set to false / 0 you will know that the circuit is open intrinsically, and thus you can create code that depends on this variable conditionally. If you need visual confirmation,e.g via a text object, you can use the variable to have an event “watch” when the variable changes state, so if the variable is true or false, put a certain message on the text object as well.

Since GDevelop renders itself 60 frames a second, this is called the main loop, that is, there’s always a loop running, so you can leave the reactive part to the main loop as it comes for free, and program the conditions needed for the simulator to work.

You obviously also will need code that sets the variable to the proper state when any of your test cases occur (i.e when and why the current flow breaks or resumes) and you also have to program those all of test cases for it to work properly using these assumptions of binary data.

1 Like

Wow, I understand more now, but I still have a question. I’m trying to do more of a sandbox mode, where you can create your own circuit, and you say to assign a boolean value of 1 or 0 to represent if the circuit is open or closed, but what code do I also add to determine if it is open or closed. Like do I have to check if objects are connected (colliding) like the battery, wire and lightbulb and it is only a 5x5 grid so it doesn’t get too wild with the circuits. To sum it up so you have a better understanding at what my new question is, what code do I add to determine the fact if it is open or closed circuit, obviously I can see it with my own eyes if it is open and closed, but I don’t want to have to keep changing it, rather the simulator should understand by itself if the circuit I am creating is open or closed.

@GESHY47 In these cases you need to consider the scope of your project and change your perspective of the problem. Oh boy, this is going to be a long write-up, sorry for that but I don’t know how else to explain it with less words.

(…) but what code do I also add to determine if it is open or closed. Like do I have to check if objects are connected (colliding) like the battery, wire and lightbulb and it is only a 5x5 grid so it doesn’t get too wild with the circuits.

If that’s what you want the simulator to do, then yes. When I say change how you view your problem, I literally mean that you need to stop thinking in terms of how the real world works and consider how you can make it seem to the end user that it works like in the real world.

While I understand that a simulator needs to be faithful to reality, no one will be asking how you programmed the electrical current behavior, for all we care electricity is something you can’t see unless it has an effect, so you don’t program the cause, you program the effect, the result.

but I don’t want to have to keep changing it, rather the simulator should understand by itself if the circuit I am creating is open or closed.

I already mentioned how in my previous comment. I understand it is not obvious if you’re new to programming, but it is literally that simple. You don’t need to make the simulator understand anything. You need to give it orders so it does tasks. One of those tasks is to be watchful of what changes the current flow state. This is why I mentioned which cases you were going to test, because you have to program every one of them.

It’s nearly impossible to just let the program “react” to a virtual world without programming behaviors and giving it stimuli to react to, you would probably need an AI and some machine learning algorithms for that. That’s why in my previous comment I was implying that you need to make use of your knowledge to reduce the amount of cases that you need to test.

Let me give you some concrete examples since you already mentioned some things. Let’s say you have a 5 x 5 grid like in your example, you have a battery, you have a wire and a lightbulb. Now let’s think, in a perfect world without restrictions what do you need to light the bulb? do we even need cables? do we need the battery? no, we just need the bulb. In programming you can make the light bulb turn on and off by itself without electricity, and you can record the state of the bulb in a variable.

Why am I saying this? because this means you can communicate between objects without needing physical connections. So if the example, regardless of the wire, if you want to light the bulb on it’s own you can. Let’s say you want the battery to light it on instead, what do you do?

You then make a variable for the battery object let’s call it voltage. This variable can be a boolean variable too. 1 for current, 0 for no current.

Then how can the light bulb react to the battery? Well you need a conditional statement (this is pseudo code) "if battery.Variable(voltage) = 1 then light_bulb.Variable(light) = 1

There we communicated the battery and the lightbulb, but what if the battery sends a 0 signal? Well the lightbulb will retain it’s previous state, so even if the battery doesn’t send more current, the lightbulb will still be lit. Why? because you need to program it what to do when it receives no current!

So you’d need a new set of events "if battery.Variable(voltage) = 0 then light_bulb.Variable(light) = 0. Seems pretty obvious, but unfortunately computers are in need of precise orders all the time to do anything.

Now, this could be the end of your simulation, but for the sake of argument let’s say you want to add the wires to affect the simulation. You have to think and ask yourself questions like:

  • how will these wires affect the way the current flows through the circuit?
  • how can the user add and remove these wires?
  • will the wires inform the light bulb that there’s no current? or the battery? both? neither?
  • etc

Let’s say you have a system where you can already add and remove wires during the game / app window, if you click a tile you can add a wire, if you right click you can remove it. Ina 5 x 5 grid, we have a wire that is 5 tiles long horizontally, and let’s say that each tile is a “part” of that wire. Then how does the flow become interrupted if you remove the a single tile?

  • Will you check each each to see if there’s a connected path?
  • Will you make that each tile that exists has a value? and if the sum of that value is less than the total distance there is no current?
  • Will you try to create a wire tile behavior that reports if they are next to another wire and aligned properly to conduct the current flow?

Now, getting more specific than that is basically me doing the work, and I’m sorry to say this but I don’t really have time for that. I think however this should be enough to give you ample understanding about how to tackle the problem, but you also NEED to break down the problem itself (creting a circuit simulator) into quantifiable & manageable tasks that will move you towards the objective.

Try to ask yourself what you want to do concretely and how you want the app to work. It’s important not to be vague about it because you can “see it” and know how to use one in reality. Be thorough and write it down as concise and as simply as possible, as if you were explaining it to an alien with zero knowledge of earth and make it so each task is phrased like an action that has to be taken. “I need to program the light bulb states”, “I need to create the light bulb sprites” etc

Consider taking time to plan what do you want to do with the simulator to scope the project correctly, otherwise there will be simply no end to the amount of work you will have to do…

1 Like

Very lengthy, but thank you so much for taking your time to answer my questions. This has helped me a lot.