Proxy method calls

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

Since there are some limitations (like a signal in editor cannot be connected to a child of a packed scene), I would like to have a generic script which will proxy all method calls (probably only custom methods, since I imagine proxying init or _ready might cause issues, if it’s even possible).

I found out there is a _get_property_list to override properties, but I didn’t find anything for get_method_list.

How to write a script which will proxy all method calls to a script on its first child?

:bust_in_silhouette: Reply From: Demindiro

As far as I know it is not possible to directly proxy method calls. There is no function similar to _get which works for methods.

What you could instead do, depending on your exact situation, is:

  • Implement each of the methods in the proxy class.

  • Add a single generic function call_function, which works like callv but which can also proxy methods.

  • Generate a proxy script at runtime with Script. This is probably a terrible idea though.


You also mention:

Since there are some limitations (like a signal in editor cannot be connected to a child of a packed scene)

Is there any particular reason you cannot use Object.connect? It sounds like that is what you need.

Is there any particular reason you cannot use Object.connect? It sounds like that is what you need.

I am not sure how would I do that in an universal way. I have an instance of a scene (packed scene) in my level scene, but because “instance variants” created via Editable Children (e.g. a door with a different texture and collider) cannot be saved as a scene directly (Save branch as sceneThis operation can't be done on instanced scenes.), I have to wrap this customized scene instance in another node (e.g. Node2D) and save that as a scene (so it can be reused). So I have a hierarchy level → wrapper → door with open method. I haven’t found a generic way how to allow using the wrapper (e.g. connect button to a door.open inside wrapper) without either manually writing proxy scripts (a bit tedious) or not using a wrapper and manually creating on-the-fly desired door variant (a lot more code [using descriptor objects, manual loading of assets, converting it to tool script so it is visible which variant is being used] and much more difficult to code properly, e.g. not loading animations and other assets of all doors when I want just one).

My goal is to have several variants of some scene (e.g. a door) which can be used from editor (without writing code), so a level designer can use it.

monnef | 2020-10-30 10:09