godot-python icon indicating copy to clipboard operation
godot-python copied to clipboard

How to load scripts within a script

Open ghost opened this issue 5 years ago • 5 comments

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

ghost avatar Dec 16 '20 21:12 ghost

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 avatar Dec 16 '20 21:12 DisDoh

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

DisDoh avatar Dec 16 '20 21:12 DisDoh

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 ?

touilleMan avatar Dec 20 '20 18:12 touilleMan

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`

Codified-Vexing avatar Dec 21 '20 09:12 Codified-Vexing

@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!")

omarha96 avatar Oct 31 '21 14:10 omarha96