Raycast freezes my game

So my racing game bot’s system works by checking if each node has the right order number, and then it moves to that node based on its ID. The problem is, this only lets me create one path for the bot to follow.

To fix this, I tried changing it so that the bot checks the node’s ID like before, and then does a ray cast to see if there’s a clear path to the node. If there are no obstacles in the way, the bot moves to that node.

But here’s the thing - this solution is causing my game to freeze up!
Here is my implementation:


Here is a GIF showing how and when the game freezes (The menu doesn’t lag, but the video is):
Screencastfrom2024-08-1020-35-48-ezgif.com-video-to-gif-converter

Here is the full code of the bot system if you want to (You might need to open it in a new tab):

I haven’t recreated or thoroughly examined your events but, by looking at the code I think it is not supposed to be running every frame but rather once by using Trigger once. This might be the issue of why it crashes so quickly, as too much stuff is going on in the background. Running a profiler test should let you know which area of the code is taking the most.

You can also try slowly going trough the code by disabling the parts until you get to the area of where the code fails , this is done by pressing D on the condition.

Lastly you can also search for your variables in the debugger , I assume it might be a while loop problem of some of your variables responsible to end the loop not updating.

https://wiki.gdevelop.io/gdevelop5/interface/debugger/
https://wiki.gdevelop.io/gdevelop5/all-features/advanced-conditions/

I’m assuming the foundNode variable is never being set to true. FYI, wait events don’t elapse within a loop like a while event because the time is checked between frames and the while event is keeping execution within the same events. I’m guessing the events after the wait never get triggered. Try it without the wait.

Will there always be a valid node. If not, you might need a different strategy to prevent being stuck inside an infinite loop. Honestly, I don’t like the while event bc of the possibility for infinite loops.

As is, it looks like it’s stuck in a loop and keeps adding a wait as quick as it can cycle. That would be a lot of waits that can’t be processed.

Thanks for the help, but…

This while loop is in a repeat for each bot loop. Using Trigger Once DOESN’T HELP, it still freezes. I tried putting trigger once on every condition in the while loop.

The profiler doesn’t respond when the game freezes

The raycast in the while loop is the cause.

I tried without the wait. The same exact result.

Do you have any other ways to do what I am trying to achieve? I have no other idea.


I tried doing a run without the ray cast, and it worked, but without the ray cast, it can’t identify if the node is reachable.

Is the node always reachable. If not then you need some form of fail safe. You need to be able to exit the while either way.

You might be able to remove the while and just use the events

FoundNode is false
Pick a random node
Ray cast to node
---- sub events

This would try to find a new node on each frame. You could add a timer to reduce the frequency of the event.

FoundNode is false
If time elapsed > 1 then reset timer
(sub events)
—Pick a random node
—Ray cast to node
------- sub events

Another option would be to use the while but add a counter so maybe each time it tries 5 times.

Long loops like while and for… have the potential to cause lag. It’s more efficient to spread the actions across frames.

1 Like

I’m exhausted, It doesn’t lag, but the AI keeps rotating.
I removed found node thing and made it do that every frame and then rotate to it.
My implementation:

If anyone has another idea or a solution, I would be pleased to know it. I am OUT OF IDEAS.

Folks, just a reminder that putting [Not Solved]/[Unsolved] or any other combination is unnecessary and is likely to lead to less responses, not more, due to people scrolling through titles quickly. If you have something marked as solved but it ends up unresolved, just remove the solved tag, please.

I’ve removed it from the title here to avoid potential issues.

Going back to your original screen shot:

You are picking a random node, then checking if it’s ID number matches the AI’s node id. This could be why it’s freezing, especially if it doesn’t match the node number repeatedly.

It should be the other way around - find nodes with matching IDs first, then pick a random one (or one closest) out of that filtered list…

Response to mod Silver-Streak

Sorry, I will try to exercise greater fastidiousness in my titling henceforth.
Also, I want to close this post, should I label it as solved??

I tried this right now. Same result, no freezing and the bot just doesn’t work

After some investigation, it looks like the events below do nothing.

I will rewrite the bot system in hopes it works, and if I find a solution I won’t post it here, because it won’t be useful.

Thanks for the help.
This post doesn’t need further response.

Yeah, yeah I know you requested no more responses, but I think there’s one more thing you need to be aware of. This raycast:

image

will only succeed if it hits a Collision object. If it doesn’t, then the condition is false, and the event’s actions and subevents are not processed. AIX and AIY are the point of the raycast intersection with the first Collision object.

So your first subevent that checks the values of AIX and AIY should be on the same level as the raycast event.

1 Like

If you’d like to close out the post, you can mark it as solved if it’s solved.

While I’m not a mod any longer, If you’d just like to close out the thread, reply here with confirmation and I can ensure it gets closed.