touilleMan / godot-python

Python support for Godot 🐍🐍🐍
Other
1.87k stars 140 forks source link

How to load scripts within a script #267

Open Codified-Vexing opened 3 years ago

Codified-Vexing commented 3 years ago

I need an hint as to how to load scripts. self.prog = ResourceLoader.load(str(path)).new() Throws a long error message.

DisDoh commented 3 years ago

I've successfully loaded a py script into a GD script with constructors too but couldn't call the function in the loaded class.

See my post.

https://github.com/touilleMan/godot-python/issues/266

DisDoh commented 3 years ago

As I see you're trying to do the opposite 🙂

touilleMan commented 3 years ago

Hi @Codified-Vexing,

Can you provide more information about your issue ? like the error message and parameters, and ideally a minimal code to reproduce the issue ?

Codified-Vexing commented 3 years ago

In a file named "PeriphPanel.gd" I have the following:

func _ready():
    var foobar = load("res://DEVICES/generic_device.tscn").instance()
    $devList.add_child(foobar)
    foobar.set_prog("res://DEVICES/CUSTOM/Addition.py")
    foobar.prog.handler("It Works!")

Where it throws the error: Invalid call. Nonexistent function 'set_prog' in base 'VBoxContainer (generic_device.py)'. The "generic_device" script is as such:

@exposed(tool=True)
class generic_device(VBoxContainer):

    prog = export(PluginScript)

    def set_prog(self, path):
        self.prog = ResourceLoader.load(str(path)).new()

The "Addition.py" has:

@exposed
class Addition(Node):

    def handler(self, this):
        print(this)

This is what appears in the Editor's Output Panel:

Pythonscript 0.50.0 (CPython 3.8.5.final.0)
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 98, in _godot.pythonscript_instance_call_method
  File "/home/vex/Scripts/GODOT/HxV Abacus/DEVICES/generic_device.py", line 49, in set_prog
    self.prog = ResourceLoader.load(str(path)).new()
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 15541, in godot.bindings.Reference.new
RuntimeError: Refcounted Godot object must be created with `PluginScript()`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `_import_path`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `hint_tooltip`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `script`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `Gbal` object has no attribute `_import_path`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx[...]
[output overflow, print less text!]

if I replace set_prog() to just print path instead of trying to load the file, this appears:

Pythonscript 0.50.0 (CPython 3.8.5.final.0)
res://DEVICES/CUSTOM/Addition.py
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `_import_path`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `hint_tooltip`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `generic_device` object has no attribute `script`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `Gbal` object has no attribute `_import_path`
Traceback (most recent call last):
  File "build/x11-64/pythonscript/_godot_instance.pxi", line 67, in _godot.pythonscript_instance_get_prop
  File "build/x11-64/pythonscript/godot/bindings.pyx", line 244, in godot.bindings.Object.__getattr__
AttributeError: `Gbal` object has no attribute `script`
omarha96 commented 2 years ago

@Codified-Vexing try using the class this way:

func _ready():
    var foobar = load("res://DEVICES/generic_device.tscn").instance()
    $devList.add_child(foobar)
    foobar.call("set_prog", "res://DEVICES/CUSTOM/Addition.py")
    foobar.prog.handler("It Works!")