Invalid call. Nonexistent function.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jelly
:warning: Old Version Published before Godot 3 was released.

I’m currently working with a custom class I made. After creating it I am unable to access any of its contents without errors.

The main code looks like so:

func new_inventory( ):
   var i = cINVENTORY.new() # where cINVENTORY is the preloaded path to the class file.
   i.inv_init() # the function that causes issue.
   return i

The created class looks like so:

class InventoryBase:
	func inv_init():
	   print("created")

So as seen, the function certainly exists, however something I am unaware of deters things from proceeding as expected.

The exact error thrown is “Invalid call. Nonexistent function ‘inv_init’ in base ‘Reference (InventoryBase.gd)’.”

:bust_in_silhouette: Reply From: guppy42

Either remove the line ‘class InventoryBase:’ ( and detab the function )

  • or -

initialise it as var i = cINVENTORY.InventoryBase.new() instead

a file is automatically a class in gdscript ( bit odd but there it is )

for reference the gdscript class documentation is here

Bam, there it is. Thanks for pointing that out.

jelly | 2017-02-24 18:24

:bust_in_silhouette: Reply From: Zylann

Assuming you did this:

const cINVENTORY = preload("res://your_inventory_script.gd")

With your current code, a fix would be to write this instead:

var i = cINVENTORY.InventoryBase.new()

Because cINVENTORY represents the script file that contains InentoryBase, not directly InventoryBase (note that you can have multiple classes in a file!).

Other ways to fix it are to consider the script file as the class itself, so instead of writing an inner class (InventoryBase), you would just write this in an inventory_base.gd file:

func inv_init():
   print("created")

Because as said by guppy42, script files are treated like classes themselves in GDScript.

Yet another way is to move the inner class to the script where you use it.