#can't call method of instantiated scene

1 messages · Page 1 of 1 (latest)

next grove
#

i made scene A and attached a script to the root node and wrote a method called "pleasework", i then made scene B, instatiated scene A, and attached a script to root node of scene B. i then try to call "pleasework" on instantiated scene A but it says "pleasework" does not exist there

inner whaleBOT
sour stirrup
next grove
sour stirrup
#

There's no context here as to what you actually did. So i can't even guess.

next grove
#

Scene B is character, Scene A is protagonist

this is Scene A's script (truncated),

extends RigidBody3D
class_name Character

func pleasework():
    print("oh. i did work.")

this is Scene B's script (also truncated)

extends Node3D

class_name Protagonist

...a bit later

func _ready() -> void:
    $Character/EquipperArea.connect("area_entered",_on_area_entered)
    $Character/EquipperArea.connect("area_exited",_on_area_exited)
    $Character.apply_central_force(Vector3(0,0,3))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
    $Character.pleasework() #fails here

the error

E 0:00:00:368   Protagonist._process: Invalid call. Nonexistent function 'pleasework' in base 'RigidBody3D'.
  <GDScript Source>protagonist.gd:55 @ Protagonist._process()
  <Stack Trace> protagonist.gd:55 @ _process()

abstract relic
#

You instatiate it but use it in _ready already?
Where do you instatiate it?
Is it just an @onready ? and that doesn't fail, for this to work Scene B must be the root of Scene A, you sure $Character isn't returning null ?

Also make sure the right script is attached to the Scene A node

next grove
abstract relic
#

But where do u do it? Is it added to the tree? Is it actually instantiating? Do u have the correct scripts attached to the nodes?

next grove
#

also im using the Instantiate Child Scene button to put character inside protagonist

abstract relic
#

The scene of character

#

Ahh i see now lol

#
    print($Character)
    print($Character.get_class())
    print($Character.get_script())```
Do this in ready and see what it prints
#

If script prints null → script is not attached.
Make sure:

@onready var character: Character = $Character

func _process(delta):
    character.pleasework()```
next grove
#

one sec

#

yep that works

#

so you can't access methods on an instantiated scene even if the root of the actual scene has a script?

abstract relic
#

You should be able to

#

Only script is null?

next grove
#

i can live with having manually to attach the same script to it but it certainly is strange that you can't access methods without that

#

do you have any other advice to say about this

#

or is that just how it is

abstract relic
#

I would have to test

#

Try to re add the node after saving ur scene character

#

But you for sure can access the script like this

next grove
#

so if i instantiate a scene, attach a script later to the actual scene, it does not transfer over

#

i guess that's it

#

thank you gentleman