Wait a few seconds in loop

Question

Guys i am trying to wait a few seconds in an “while” loop, beacuse its crashing beacuse of “output overflow”.
Here’s what i got now. Help will be appreciated.

Script

while ray_cast_2d.is_colliding() or ray_cast2.is_colliding():
print(“colliding”)
velocity = Vector2.RIGHT * CONVEYOR_SPEED

I would very highly discourage the usage of the while loop in almost any context during game development, as it will run on the same thread as the rest of the game, which means it’ll freeze execution COMPLETELY, and as things pile up, your game will crash really quickly, as you experienced.

If you need to “stop” the game for a few seconds, I recommend simply pausing the game, and setting the “Process” mode for your nodes accordingly, so some nodes won’t be paused while others will be.

Unfortunately from that limited amount of information I’m not entirely sure what you’re trying to achieve, but try to pause instead of using a while loop.

1 Like

Im trying to achieve and 2d top down conveyor system for and automation/factory game. If you havesome ideas how to implement conveyors then maybe you can tell me about it. Thanks for help.

You cannot pause a loop from looping (with ‘await’ function for instance).

Instead use a signal and a timer perhaps. As tibaverus said previously, “while loops” can be very dangerous without escape clauses or tight encapsulation and control. Personally I never use them.

Perhaps your conveyor belt (and I am assuming you are talking about dropping things onto it) could just use a timer. When the timer expires, say 2 seconds, drop an item on the conveyer and reset the timer.

If you are talking about how to move things along a conveyor, I would have the conveyor made of several parts (segments) that move along a path beside each other. When they reach the end of the path they animate curving under and then move back to the beginning of the path to animate curving back out again. Then your items dropped on the segment would just follow the movement of their parent segment, except rather than animating off, they drop off when the parent segment signals ‘end_of_belt’ or something like that.

Hope that helps.

1 Like

Thank you. Is that possible using tilemap? I moved to godot maybe a month ago. I don’t understand all the things about that sofware yet. So i wanted to ask how i can make it work? Thanks.

I’m sure you could achieve what your going for using lerp or the animation player.
Edit: I misunderstood the question.

So how to achieve that by using lerp ?

I just googled “Conveyor belt in Godot Game” and there are lots of tutorials. Try a few of those to get some ideas.

I am not being “just google it” rude here, but there are lots of ways to achieve any particular thing, and your choice of how to go about it depends on your game ambitions, your set up so far, your future aims etc. There are genuinely lots of ways to achieve any particular thing, like KatDawg57 said, ‘Lerping’ could do it too. But how you go about it just depends on so many other factors. For instance, how many belts are you going to have on a screen? How long will they be? Will they cross over each other? Will they curve around corners. All these things will have an affect on your design and approach.

For a grid-based game, you can use a TileMap to place conveyor belt tiles. This allows for easy placement and management of the conveyor belt system. Each tile can have its own script to handle item movement and detection.

You could use an Area2D node to represent the conveyor belt. This node can have a collision shape to detect when items or characters are on the belt and an animated sprite for the belt movement.

Keep at it, do some tutorials and experiments, you will get there!

2 Likes

Yeah, so I definitely misunderstood the question. My bad…
But you should look into IF statements during the physics process when it comes to moving nodes around, I’m 90% it’s more performant than a while loop running in process (which is what I’m assuming your running your code in).

The process function is already a loop, so adding another is just wasting performance. And another guy already mentioned how horribly while loops (and other loops) run in Godot and most programming langs.

Thanks you, but how to attach area2d node to an tilemap?

Those two paragraphs were different approaches. You can use a tilemap, and have that tile move things along in a particular direction. String them together and your belt is working.

OR

You could have an area2D node, with a collision layer (that represents your belt) and when something enters it, you move it along in the belts direction.

As I described, there are lots of ways to do it. Did you watch some tutorials to get some ideas?

1 Like

For the tile map method, lets say you have an object/scene called “moveable_box”. You would, in its script, detect which tile it is on. If the tile is, say, in group “conveyor_belt”, you would then move your moveable box in script.

Hope that helps.

1 Like

Thanks for the advice. By the way do you have some recommeded tutorials about that? I didn’t found anything about conveyor belts or “tilemap placement in-game”. Like the Coding with Rus youtuber devlog about an factory game

Not really, I have never done a conveyor belt. I did a quick search for you though and found this listed as the first five results. No idea how good they are but here they are anyway.

It does not really matter if they are for different versions of godot, you can learn the approach at least.

Hope that helps,

Paul

1 Like

Thanks for helping me, i think beacuse of that i will have some additional knowledge to Godot. But do you know how to implement to the game grid based placing system? Have a nice day.

Well, in the grid system, I would have tiles for my conveyor belt. When an object is detected on that tile type, make it move. You could put the tiles into groups like convey_left, convey_right, convery_up etc. So in your object script, you would have a process like “am I on tile type conveyor”, if so is that tile in group “convey_right”, if so, my object slides right until it is no longer on a tile of type “conveyor”.

As I said though, I have not done a conveyor belt, and my first game had tilemaps but I have not used them since then. But that is in essence how I would do it. Good luck with it!

1 Like

Thank you… the third time? Anyways you helped me alot and I got from you some knowledge so thank you⁴ heh.
Oh and i forgot, I asked above about the detection of the body, what I should use to get that “detection” to work, without collision layer/shape? Like you said “am I on tile type conveyor”.