0 votes

So I am trying to add code from a loaded gd script. I already can use the functions from any loaded script likeload("res://MyScript.gd"), but how can I use it's processing function code?

Like in MyScript.gd there is:

func _process(delta):
    print("MyScript code.")

But only loading does not add that print process method to my node. Is that possible to do?

in Engine by (369 points)

1 Answer

+1 vote
Best answer

My understanding of your question is that you'd like to execute that print statement once upon your object loading. In that case, you could use the _ready() function that runs once the node has entered the tree for this first time.

func _ready():
    print("MyScript code.")

If you really do need to call the _process() function once, you could do that by calling it from the _ready() function. Note that you will need to pass a delta value.

func _ready():
func _process(delta):
    print("MyScript code.")
by (135 points)
selected by

No, the print part is only for implementation, the idea is to import additional process codes from a loaded script into the caller node _process function.

This will open the ability to import script modules to add segmented logic.

Yes. Best not to call your function _process :) but you can for sure do var my_script = load("res://MyScript.gd").new() and then my_script.do_something(). If you didn't do the new() to create a new instance then you'd only be able to use the script's static functions (assuming it had any).

Ah I see what you're saying now. Off the top of my head, one way to do that may be to keep a list of type Node and for every node in the list, call a method like "imported_process" if it exists. Then load all of the extra scripts you want into that list and the code in those functions should be executed in the _process() function.

var Nodes : list

func _ready():

func _process(delta):
    for n in Nodes:

    #rest of _process function below

So then AnyScript would just have to implement that function.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.