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

How to call function from .gd script in python and vise-versa?

Open ka23ppa3 opened this issue 4 years ago • 2 comments

I just loaded addon from assetlib which wrote using gd-script, unfortunately have no luck to call it. work_with_gdscript example didnt helped me much.

My code:

from godot import ResourceLoader
from godot import *

GDPrint = ResourceLoader.load('res://addons/katuiche-colorful-console/class/print.gd', '', False)

class Logger:
    def LogError(source, message: str):
        GDPrint.line(GDPrint.RED, "FROM: " + source + " MESSAGE: " + message)
        pass

getting error AttributeError: `GDScript` object has no attribute `line`

This is how looks my gd-script which Im trying to load and call(Keep in mind that I deleted most color-constants to clear some space):

class_name Print, "./icons/class.svg"
extends Node

const ESCAPE = ''
const COLOR_RESET = "[0m"

const RED = "[0;31m";

static func clear_console():
    match OS.get_name():
        'Windows': printraw(ESCAPE + 'c')
        _: printraw(ESCAPE + 'c')

static func _get_string(what, divider = ""):
    var string := ""
    if what is Array:
        for thing in what:
            string += String(thing) + divider
    elif what is String:
        string = what
    else:
        string = String(what)
    return string


static func line(color, what) -> void:
    set_color(color)
    print(_get_string(what))
    reset()


static func raw(color, what) -> void:
    set_color(color)
    printraw(_get_string(what))
    reset()

static func s(color, what) -> void:
    set_color(color)
    print(_get_string(what, " "))
    reset()

static func t(color, what) -> void:
    set_color(color)
    print(_get_string(what, "   "))
    reset()

static func debug(color, what) -> void:
    set_color(color)
    print(_get_string(what))
    print_stack()
    reset()

static func set_color(color):
    printraw(color)

static func reset():
    printraw(ESCAPE + COLOR_RESET)

Also Im new in python and godot so sorry for my incompetence

ka23ppa3 avatar May 30 '21 15:05 ka23ppa3

Okay, I see that ResourcesLoader.load create object of type GDScript when you trying to load gd-script, which have API for reflection call, so I changed my code to

from godot import ResourceLoader
from godot import *

GDPrint: GDScript = ResourceLoader.load('res://addons/katuiche-colorful-console/class/print.gd', '', False)

class Logger:
	
	def LogError(source, message: str):
		GDPrint.call("line", "[0;31m", "FROM: " + str(source) + " MESSAGE: " + message)
		pass

where "line" is method name which I want to call and others arguments is arguments for method "line".

but GDScript API lack of documentation, comments so I have no clue how to use most of functionality it provide...

Now my question is how to get value from variable/constant of gdscript.

ka23ppa3 avatar May 30 '21 16:05 ka23ppa3

hi everyone, hi @ka23ppa3 You can accomplish this by creating a scene and attaching the script (.py), then creating an instance of the newly created scene (the one with the .py) in the scene you want to use this code (the one with the .dg).

Already with the instance you can call the methods you want.

arisvaldez avatar Jul 13 '23 14:07 arisvaldez