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

Instancing Object between .py and .gd scripts.

Open DisDoh opened this issue 4 years ago • 8 comments

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)

DisDoh avatar Dec 15 '20 18:12 DisDoh

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

touilleMan avatar Dec 20 '20 18:12 touilleMan

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?

DisDoh avatar Dec 21 '20 14:12 DisDoh

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

DisDoh avatar Dec 24 '20 20:12 DisDoh

I had to extract each value at once and recompose the array on gdscript.

DisDoh avatar Dec 25 '20 18:12 DisDoh

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?

nosa999 avatar Jan 13 '21 11:01 nosa999

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.

DisDoh avatar Jan 17 '21 14:01 DisDoh

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: image

Is it true?

nosa999 avatar Jan 18 '21 09:01 nosa999

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: image

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.

DisDoh avatar Jan 24 '21 16:01 DisDoh