Trying to understand super() in extended class

Godot Version

4.2.2

Question

Working on a state machine and I am having a little difficulty understanding why calling super() that returns an int, doesn’t finish or return but keeps processing code below it.

BaseState

func physics_process(delta) -> int:
	character.velocity.x = character.direction * character.SPEED
	return States.NULL

Extends BaseState

func physics_process(delta) -> int:
	super(delta)
	if character.direction == 0:
		return States.IDLE
	
	return States.NULL

So, if the extended class calls super(), but in the super class it returns, why does code below super() in the extended class process? Thanks in advance.

Calling super() from the child (extended) class to the parent (base) base class is no different than calling any other method in your game.

That is, it calls the method, does its job and returns. That’s why after calling super() the code in your extended class is executed. It’s just another method.

3 Likes

You are just calling the inherited version of the function via the super comand. If you don’t intend to execute the extended class function, remove it from the extended class. And don’t override the function. Just call the function directly.

3 Likes

So, if the super class method returns an int, does it return it to the extended class calling super?

it’s like any other function, you would need to store it in a variable to access the return value.

func physics_process(delta: float) -> int:
	var base_process_result: int = super(delta)
	if character.direction == 0:
		return States.IDLE
	
	return base_process_result

Maybe you intend to call the base version after the if statement? This would prevent execution when the if triggers, because of the return States.IDLE

func physics_process(delta) -> int:
	if character.direction == 0:
		return States.IDLE

	super(delta)
	return States.NULL

Makes sense. So…

extends Node2D
class_name BaseState

func physics_process(delta: float) -> int:
    return 0
extends BaseState
class_name GroundState

func physics_process(delta: float) -> int:
    print("GroundState")
    return 0
extends GroundState
class_name IdleState

func physics_process(delta: float) -> int:
    super(delta)
    print("Idle State")
    return 0

So in IdleState’s physics_process, super() is called and executed returning 0, and that’s it, not storing it in a variable or anything. For some reason I was expecting it to return there back to its parent. CS Major junior year brainfart :crazy_face:

1 Like