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);