0 votes

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?

in Engine by (205 points)

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?

1 Answer

+1 vote

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 processphysics(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.

by (271 points)

Thanks I'll look into these.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.