How to make RPC play nice with subclasses / imports?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cosine

I have this:

a.gd:

extends Node
onready var encapsulation_is_good = preload("res://b.gd").new()

b.gd:

sync func thing():
	print("here")
# How can I rpc("thing") from b.gd??

Any way to make rpc play nice with imports?

I’d rather not have to combine b.gd into a.gd as its over 100 lines. I messed around with signals but that was a feat of its own. Other things that don’t work:

rpc("thing")
# rpc doesn't exist (if I extend Node, then !is_in_tree or whatever)
a.rpc("encapsulation_is_good.thing")
:bust_in_silhouette: Reply From: hilfazer

I can’t see Your code but it looks like encapsulation_is_good is not on SceneTree, thus it can’t call RPCs. Try adding it to a.gd as a child first. Of course a.gd needs to be in SceneTree too :slight_smile:

If you want to add b.gd as a child in _ready() you need to make a deferred call like this:

func _ready():
    call_deferred( "add_child", encapsulation_is_good )

Deferred call is needed because SceneTree is locked during _ready() calls. I’m almost sure it’s same for _enter_tree() callback.
Yes, b.gd needs to extend Node.

Thanks a lot!! <3

I can’t believe it didn’t occur to me to, just put it in the scene tree.

One thing to note for anyone reading this: I had to set_network_master on the child node so I could use the master and slave keywords; it didn’t happen automatically.

Cosine | 2018-02-19 17:08