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?

asked Nov 4, 2019 in Engine by hidemat (91 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

0 votes

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.

answered Nov 5, 2019 by Xian (165 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 How to use this Q&A? before posting your first questions.