Gameloop for a tetris like game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hidemat

So I’m trying to make a tetris-like game. I’ve come pretty far but I’ve run into a problem I’m not quite sure how to fix. I have a Piece (KinematicBody2D) scene that runs on a _physics_process loop for falling and movement from left to right. Which works ok.

But I want the parent Grid (Node) to have a STATE {IDLE, PIECES_FALLING, PIECES_BREAKING} variable, and tell the Piece only to fall if the PIECES_FALLING state is active. The approach I took in the parent grid was something like this:

func _process(delta):
match state:
	STATE.IDLE:
		if Input.is_action_just_pressed("start"):
			state = STATE.PIECES_FALLING
	STATE.PIECES_FALLING:
		#check for pieces that need breaking
		if pieces_need_breaking():
			state = STATE.BREAKING
	STATE.PIECES_BREAKING:
		# break pieces
		state = STATE.PIECES_FALLING

And it works, but it gets really laggy after the first cycle. So I was wondering how to fix this approach? Is it at all good, or should I just discard it and try something else?

The lag could be due to checking the state during each cycle of _process(), which I think is each frame. Maybe there’s some other way to check it without using _process() so much?

Ertain | 2019-11-04 19:22

:bust_in_silhouette: Reply From: Xian

There are lots of reasons why it could be lagging. (And your sample code seems to be lacking to make it easier to tell us why is it lagging.)

It could be that the broken pieces are still active thus entity count increases over time and thus processing power resource is being suppressed due to increased amounts of traffic.

It could also be that in some states, there are redundancies in your code that is eating all the processing power.

It could even be that in one state or two, has undergone a multitude of processes that it is not sharing any of the processing resource.

It could even be that it is not “match State:” that is causing lag but other functions that activate because of a certain state.

All I could recommend is trying “timer nodes” instead of _process_physics(delta) for some case that do not require sub-second activations. Next try reading about “Big O notations”, “Multi-threading”, and a little bit of “data structures” to reduce the lag of any codes.

Thanks I’ll look into these.

hidemat | 2019-11-05 14:07