godot-python
godot-python copied to clipboard
Instancing Object between .py and .gd scripts.
Hi,
Is there a way to instanciate a python class in gdscript file and then use the methods of the instanciated object?
I could do:
`
var ffsnn = load("res://PythonScript.py")
var ffsnnObject = ffsnn.newvar1,var2,...)
` But could not do:
ffsnnObject.function(var1,var2)
Hi @DisDoh
You should be able to use from GDScript any function declared in the exposed Python class. However function and variables dynamically defined are not available, so doing something like this will not work:
def __init__(self):
self.function = lambda: "doh !"
self.var = "doh !"
I'm stucked... Could you provide me a simple working example for loading and instancing python script in gdscript and calling a function declared in @exposed Class?
So i could pass the first step of instancing and calling a function which can return what she receives.
Now I've a question about arrays with the return value from a function in python script loaded in gdscript.
How can change the type of array to gdscript compatible arrays because i'm getting this error :
sys:1: UserWarning: Cannot convert
<class 'numpy.ndarray'>to Godot's Variant
And I've tried with Pool String/Bytes/Real/Int Array
I had to extract each value at once and recompose the array on gdscript.
I'm stucked... Could you provide me a simple working example for loading and instancing python script in gdscript and calling a function declared in @Exposed Class?
Hi, could you explain me, how you make instancing python in gdscript?
You can load it like a gdscript.
var loadedScript = load("res://PythonScript.py")
var loadedScriptInstance = loadedScript.new()
loadedScriptInstance.funcToInitVar() <---- Not _init()
var testReturn = loadedScriptInstance.funcToLaunch()
You have to note the comment of TouilleMan that self.pythonVar in pythonScript is available just within the python class.
You can load it like a gdscript.
var loadedScript = load("res://PythonScript.py") var loadedScriptInstance = loadedScript.new() loadedScriptInstance.funcToInitVar() <---- Not _init() var testReturn = loadedScriptInstance.funcToLaunch()
You have to note the comment of TouilleMan that self.pythonVar in pythonScript is available just within the python class.
Thank you for your great help.
I did that. but I couldn't understand your last sentence about TouilleMan's comment, exactly. I implemented my function as below in python class:
Is it true?
You can load it like a gdscript.
var loadedScript = load("res://PythonScript.py") var loadedScriptInstance = loadedScript.new() loadedScriptInstance.funcToInitVar() <---- Not _init() var testReturn = loadedScriptInstance.funcToLaunch()
You have to note the comment of TouilleMan that self.pythonVar in pythonScript is available just within the python class.
Thank you for your great help. I did that. but I couldn't understand your last sentence about TouilleMan's comment, exactly. I implemented my function as below in python class:
Is it true?
The self. is to reference the variable to the class in Python and would not be reachable from outside that what touilleMan has said. You can do it like you're doing it.