How to run _PhysicsProcess in both Parent & Child classes ?

raamraam Posts: 1Member
edited August 1 in Programming

I am trying to follow the tutorial but I tired to code the programs in C#

my code

Actor.cs

using Godot;

public class Actor : KinematicBody2D
{
    private int gravity = 1000;
    public Vector2 velocity = new Vector2(0, 0);
    public Vector2 speed = new Vector2(100, 0);

    public override void _PhysicsProcess(float delta) {
    // gravity script
        velocity.y += gravity * delta;
        velocity = MoveAndSlide(velocity);
        GD.Print("In Action Physics");
    }
}

Player.cs

using Godot;

public class Player : Actor
{
    private float direction;
    private Vector2 character_direction;
       public override void _PhysicsProcess(float delta) {
           // movement script
            direction = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");      
            character_direction = new Vector2(direction, 0);
            velocity = speed * character_direction;
               GD.Print("In Player Physics")
       }

}

The player.cs is attached to the Game Objet

Everything else works, but after I added _PhysicsProcess in child script, the parent script physics is not getting called at all . Can any one help me to run both the physics scripts ?


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,814
    Accepted Answer

    Welcome to the forums @raam!

    Because the function is overridden, the parent implementation of the function will not be called and so you'll need to manually tell the child class to call the parent class's version of the function. Based on what I found online, adding base._physics_process(delta) should call the parent's implementation of the function.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,814Admin
    Accepted Answer

    Welcome to the forums @raam!

    Because the function is overridden, the parent implementation of the function will not be called and so you'll need to manually tell the child class to call the parent class's version of the function. Based on what I found online, adding base._physics_process(delta) should call the parent's implementation of the function.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file