0 votes

I have a custom class that represents a Player in a game world, with attributes and methods. This is initalized in through a load() call in a Class configured to function as an AutoLoad, so that character attributes set in one Scene persist to the multiple game world scenes.

What ends up happening is, the call to set the Player Name (a method extended from Player2) errors out, and the script fails. The error message is "Invalid call. Nonexistent function 'SetPlayerName' in base 'GDScript'."

Due to the incredibly poor introspection in regards to class and methods, I don't know if GDScript has loaded in the necessary methods, or if I'm missing something here in the Player2.gd class that I need to account for, when running a method in a non-Autoloaded class. This is really confusing me, especially since Godot has very poor debugging tools in regards to exploring what methods are in memory.

In essence, how I can get the method call Player.SetPlayerName(Name) to work?

Below is the code for the PlayerInstance class which is run once the player has approved their attribute changes on a scene (not included).

# PlayerInstance.gd
# Singleton to track the player character (YOU) in the game world
extends KinematicBody

# Inherited Classes
var Player = load("res://scripts/Player2.gd");

# Fields
var PlayerObj

# Constants
const MAX_SLOPE_ANGLE = 45

# Event Methods
func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    PlayerObj = Player.new()

# Methods
func InitNewPlayer(Name, Const, Str, Int, Per, Prov):
    """
    TODO
    """
    Player.SetPlayerName(Name)
    Player.SetPrimaryStats(Const, Str, Int, Per, Prov)
    # PlayerObj.SetPlayerName(Name)
    # PlayerObj.SetPrimaryStats(Const, Str, Int, Per, Prov)

The Player2 class can be viewed on Pastebin here. It's literally too big for this field.

in Engine by (63 points)

SetPlayerName is static?
If it is, you may not be using it right, check http://docs.godotengine.org/en/stable/learning/scripting/gdscript/gdscript_basics.html#static-functions

1 Answer

0 votes

This may be because the player class not loaded when calling its methods.

Change the load with preload.

You should also make sure that InitNewPlayer is called atleast one frame after creating the new instance, PlayerObj = Player.new(). So change the line where you call InitNewPlayer to call_deferred(PlayerInstance,"InitNewPlayer").

by (750 points)
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.
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 webmaster@godotengine.org with your username.