• Godot HelpProgramming
  • how to attach a gdscript code file (drawing_line.gd) to a node during runtime and to run the script

I tried to attach a gdscript code to a child node during the runtime. But no effect of the script on run. Need help!!!

Thanks in advance.

Codes are below.

(attached script to the "parent" Node2D)

extends Node2D

func _ready():
		$Line2D.set_script(load("res://drawing_line.gd"))  

(drawing_line.gd - gdscript which I tried to attach to the child node)

extends Line2D

var line2D_coord_X1 = 697.1718063
var line2D_coord_Y1 = 157.0043504
var line2D_coord_X2 = 224.4594551
var line2D_coord_Y2 = -55.62737387

func _ready():
	self.clear_points()
	self.add_point(Vector2(line2D_coord_X1, line2D_coord_Y1))
	self.add_point(Vector2(line2D_coord_X2, line2D_coord_Y2))
  • Welcome to the forum @Volkovino!

    I think the issue is that the code on the script you are attaching only has code in the _ready function, which is called when the node is added to the scene. Maybe try calling the _ready function manually after setting the script and seeing if that works?

    Something like:

    $Line2D.set_script(load("res://drawing_line.gd"))
    $Line2D._ready()

Welcome to the forum @Volkovino!

I think the issue is that the code on the script you are attaching only has code in the _ready function, which is called when the node is added to the scene. Maybe try calling the _ready function manually after setting the script and seeing if that works?

Something like:

$Line2D.set_script(load("res://drawing_line.gd"))
$Line2D._ready()
2 years later