Big problem with RandomInRange and timer

Hey everybody! I’ll cut to the chase. I’m trying to make what I call “Perfect traffic”. It’s a simulation of 4 streets with 4 different cars that can only spawn if a series of conditions are met. The result (at least what I wanted) was random cars spawning and miraculously not colliding. Here’s the scene:

I actually got them to not collide, but the result was that there was a lot of empty space. But the real issue is that, while looking into this, I discovered that they always spawn in a certain order (not even the cronological order!). Car2, Car, Car4, Car3. I mean, the RandomInRange variable resets until the next number fits the pattern!

I think the problem might be in ‘the conditions’ part of the code. Simplified, to check if a certain car can spawn, my code: (This is clarification for the next images, I’d skip this part if you’re reading the post, it’s a hard read)

  1. Picks the “main” car that’s closest to it’s spawn point and, at the same time, closest to the car that will be spawned (If you think about it, every car has one “main” car, the one that has more chances to collide with, the one on the closest street that goes in the other direction).
  2. Checks if that car is in it’s “danger zone” (for example, in my game, if we want to spawn Car and the position Y of Car2 is more than 518, then it will surely collide with it). If it’s in it’s danger zone, then it doesn’t spawn and resets the timer.
  3. If it’s not in the danger zone, then it also checks for the next threat, the other opposite-direction car.
    If there’s none or it’s not in it’s danger zone, then the car spawns. If it is on the danger zone, then the timer is restarted.

Of course I don’t actually expect help from just these steps, they serve to explain how the code works and what I wanted to do (If there’s a better option I’ll change it in a heartbeat).

Those are the directions of the cars

At the beginning of the scene, and timer and RandInRang event (The “firstTime” boolean works backwards, it’s supposed to indicate that this is not the first round. I needed separate events because in the first round the game doesn’t need to check for existing cars because, well, there’s none)

This is the first “real” car event. Its structure is repeated 3 other times for the other cars, which I felt was unnecesary to display. Basically the same with different cars and positions.

This one is just to show the bottom event. You can place the 3 pictures together and you will see its the whole code.

That’s all I thought of including. If there’s anything else I should add for clarification please let me know and if you have a solution or an improvement (even if it’s changing everything), I’ll be very available these days. Thanks a lot for reading!

1 Like

There’s a few things I could see that could be causing problems. One is that you’re using “RandomInRange” in a comparison condition. Expressions are evaluated every frame. This means that the RandomInRange in this condition is randomizing each frame. Which means that it’s very likely you could end up with MULTIPLE times where it just lands on 0.8 seconds (Since it’s being randomized 60 times a second)

You should set a variable for “spawnCarTimer” to RandomInRange(0.8,1.5) at the start of the scene, use that as the comparison in your timer in this event, and then reset that variable to a new random number as an action within each of hte subevents.

Another issue is that you have to be REALLY careful when combining Timers and Wait actions, because Waits are asynchronous (don’t care about frame/order/etc) and could mess up your timers. Generally you want to avoid combining them whenever possible.

I don’t know if resolving either/both of the above items will resolve your issue fully, but you will definitely want to address them if you can.

Thanks so much for the quick response! I will change those parts and tell you how it goes.

Ok so I implemented the extra variable with the RandomInRange(0.8,1.5) value and it no longer “flashes” different values like before. Before I get to the problem(s?) I should add that the Wait action is just to change the boolean “firstTime”, so as long as it’s value is less that the minimum of the timer’s range (0.8), there’s no issue. I will have that in mind in future projects.

Now the real issue, aside from the emptiness of the scene, is that there’s still the pattern of Car, Car4, Car3, Car2, Car(…). I don’t think I can send a video, but basically, no matter how many things I add to try and prevent this it still happens. It always starts with Car, and there’s two options for each car; either spawn more of the same car, or go to the next car. It’s pretty frustrating because I wanted it to look random and this seem staged. By the way the code works, I understand that no “opposite” cars spawn as a preventive meassure, but parallel cars should spawn, specially when there’s no “opposites” for that car either! Anyways, I hope you can help me with this and tell me if there’s anything I could add to make this easier. Maybe the project? I’m not really familiarized with the forum. Thanks!

The & condition isn’t needed unless inside an or. All conditions need to be met by design. It’s not causing any issues, it just takes more time to write.

I noticed in several spot you use consecutive pick the nearest object conditions. Since the first condition only picks the nearest object then the 2nd one can only compare it to the car’s location. The 2nd condition does nothing but pick the same car because that’s the only choice.

1 Like

Hey! Interesting thing here, those pick the nearest… conditions work together to make sure that the car that will spawn isn’t checking with the one nearest but also with the one furthest from it’s “end position”:

For example, in this picture: which car is closest to the car to be spawned (yellow) and which one has more chances to collide? Those two conditions make sure to pick, in this case, the car at the bottom (I tested it by deleting said car and it workd 100%)

1 Like

Does it? The condition pick nearest object only picks 1 object. Which means the next pick nearest object only has 1 object to pick from. So, it picks the object that was just picked. Conditions work like filters. They pick the objects that match the conditions and the next condition(s) pick from that list.

Edit: Here’s an example. The balls are draggable.

try me: No matter where the objects are, it’s going to pick the object near the 1st spawnpoint.

1 Like

I tested it by then saying to delete said object. In the case I send it picked the bottom one, unlike before.

1 Like

Also, it looks like RandomInRange(0.8,1.5) only returns numbers with .8 at the end? Did they change RandomInRange in some way?

1 Like

That’s only because of the order of the pick nearest statement. It just happens that the bottom yellow car gets picked by the “Pick nearest to 474, 864”, and so the “Pick nearest Car.X(), Car.Y()” is always going to return that one car, because that’s all it’s checking with.

Those two pick events are not being run at the same time, and GDevelop does not picks whichever one is common to both. GDevelop filters the list of cars it works with in the first pick condition to 1 car, and then uses that list of 1 car in the next pick condition.

So what @Keith_1357 wrote is correct. You’ve just been lucky that the car pick events are in the right order.

1 Like

Understood. Solved it by Deleting the “firstTime” variable and sort of implementing the detection into each “whichCar” event. Here’s what I did:

I’m pretty sure there’s a better way to do this though

1 Like

Sadly now I have overlapping as a problem, despite coding against it:

I’ll explain it event by event:

  1. The game checks which car to spawn

    1. If there are already more than 4 cars in the scene, then reset the timer (to avoid
      monopolization of a car)
    2. If there are 0 cars of the same type → then begin the filter process (If there are any events that are not visibly in the screenshot, it’s because those are the “filter” events that aside from scanning if there are any opposite cars living in the scene, they also expand wayy down, impossible to show in one screen, let alone with other events)
    3. If the number of cars living on the scene is more than 0 but less or equal to 4, then
      5. If the car’s distance to it’s spawnpoint (where this car will/would spawn) is more than 128 (originally it was 70, to avoid overlapping and add some space) → begin filter process
      6. If it’s less than 128 → reset timer

At first I also had an “Pick the Car that’s closest to [spawnpoint]” and then compare those numbers, but it also didn’t work.

I’m pretty sure that reseting the timer is either not enough or I wrote it poorly, because most of the time there’s overlapping and way more than 5 cars on the screen.

Please tell me if you find a dolution to this problem or some optimization. Thanks a lot for reading!

1 Like

What do you mean by that? And what are you expecting to happen?


Thank you for share with us. 9apps

1 Like

Well since the screen was looking pretty empty, I thought that if I turned down the minimum the variable in “Timer > Variable(valueRand)” (which is a RandomInRange) I would get way more cars in the scene (which is right):

I knew overlapping was going to be a problem, because I’d have tens of cars spawning, so I thought if I wrote that condition (the one I uploaded), it would keep restarting the timer until some car could spawn (since the distance between the closest car to the spawnpoint and the spawnpoint is more than 128).

Basically, I want to spawn every car that can spawn at the moment.

Sorry, I’m usually a very messy and forgetful person. So if I forgot to mention something important or maybe a detail please let me know!

I recorded some gameplay that I thought could help orientate you in what I did and what I’m trying to do, since I know realize I explained it very poorly. I don’t know how to send the video though. But I do think it’s very informative.

In case I can’t (because of website rules or something) basically I’m trying to simulate an intersection of on-coming cars that never collide, though sometimes seem very close to. Because I can’t control what happens after they’re spawned (they move at a constant speed and can’t turn nor stop), I thought I could control before they were spawned by making conditions that would assure they never collided with anything on-screen at the time of the spawn (after they’re created, they’re on-screen and become the problem of other cars that want to spawn). There are different 4 cars, each one goes in one direction. They move at a constant speed and for an organic effect they are created at different rates (Thus the RandomInRange variable). The reason why I didn’t have overlapping as a problem before is that I had that variable as “RandomInRange(0.45,1.5)”, whose minimum value (0.45) leaves a gap between the cars. But when I tried changing that to 0.2, overlapping revealed to be a big problem.
Then I thought that if I wrote some conditions for that problem so that they could never overlap (since they needed a certain distance to spawn), and turned the variable down further, I could get way more cars in the scene that don’t overlap (clsoe to the maximum cars that can spawn at the moment).
I don’t know why (I haven’t used Gdevelop in months, maybe that’s why) the code is like, bleeding through the “Compare two numbers” and the “Reset timer” barricades and just creating cars that, obviously given that low number, overlap.

Sorry if I changed tenses or subjects and missed some commas. I wanted to give a full-view of what I’m working on and hopefully it helped (at least a bit). I tried using Italics and Bold so as to not get lost as you read, it’s a big chunk of text and hopefully worth it.

Thanks a lot for reading and do ask if anything’s unclear!

Basically I’m trying to make the game restart the timer until a car can spawn. Which I aproximate is 3cars/sec

I tried changing the condition of compare two numbers to a trigger in the scene that checks if a car is colliding with it. Didn’t work.

Captura de pantalla 2024-01-13 135342
Captura de pantalla 2024-01-13 135412

I did give them a sprite (little dot) even though it doesn’t look like it. I don’t know what else to replace or change

@MrMen Is something wrong?