How to define _process function for node created in code? Godot 4

Is there any way to define nodes _process function from code? Without using external script file?

Now my code is like this:
var process_script = load(“res://src/scripts/music_process.gd”)

var mp = Control.new()
mp.name = MUSIC_PLAYER;
mp.set_script(process_script)
mp.set_process(true)
Globals.ROOT.add_child(mp)

This works, but it would be great to get rid of that externally loaded script file. Process function just tracks music play position. This could be done also with timer, but I think I might need this anyway approach in the some other case too.

No, it’s not possible.

EDIT: Nevermind, it kinda works:

extends Node

var src = """
extends Node

func _process(delta):
	print('it works')
"""

func _ready() -> void:
	var node = Node.new()
	var script = GDScript.new()
	script.source_code = src
	script.reload()
	node.set_script(script)
	add_child(node)
1 Like

Thanks! One external script file less!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.