0 votes

How do you check if a Node is ready/fully loaded in?
I have a strange bug, I spawn a player and _PhysicsProcess() is being called before it even enters the scene fully, thus resulting in being moved in a random direction after loading, instead of just showing up on the position I set.
My player is a KinematicBody2D.

public override void _Ready()
{
    //...
    _ready = true;
}

This was my best shot, still not really working as intended...

_PhysicsProcess() code:

        Vector2 force = new Vector2(0, gravity);

        if (isOnGround)
        {               
            if (Input.IsActionPressed($"p{_PlayerNumber}_right"))
            {
                if (velocity.x < maxMovePower)
                {
                    force.x += movePower;
                }
            }
            else if (Input.IsActionPressed($"p{_PlayerNumber}_left"))
            {
                if (velocity.x > -maxMovePower)
                {
                    force.x -= movePower;
                }
            }
            else
            {
                float velocitySign = Mathf.Sign(velocity.x);
                float velocityLength = Mathf.Abs(velocity.x);

                velocityLength -= stopPower * delta;

                velocity.x = velocityLength * velocitySign;
            }
        }

        if (Input.IsActionJustPressed($"p{_PlayerNumber}_action"))
        {
            if (Input.IsActionPressed($"p{_PlayerNumber}_right"))
            {
                force.x += flapPowerX;
                force.y -= flapPowerY;
            }
            else if (Input.IsActionPressed($"p{_PlayerNumber}_left"))
            {
                force.x -= flapPowerX;
                force.y -= flapPowerY;
            }
            else
            {
                force.y -= flapPowerY;
            }
        }

        velocity += force * delta;

        var collision = MoveAndCollide(velocity);

        if (collision!=null)
        {
            velocity = collision.Remainder;

            var normal = collision.Normal;

            if (isOnGround)
            {
                velocity = velocity.Slide(normal);
            }
            else
            {
                velocity =velocity.Bounce(normal);
            }
        }

Spawning method looks like this:

    var scene = (PackedScene) GD.Load("res://PlayerKinematic.tscn");
    var newPlayer = (PlayerKinematic) scene.Instance();
    newPlayer.Name = playerName;
    newPlayer._PlayerNumber=playerNr;

    newPlayer.Position = new Vector2(point.X,point.Y-(_tileSize/2f));

    AddChild(newPlayer);
in Engine by (12 points)

Please log in or register to answer this question.

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.