How to load resource to a node by code? without @export

Godot Version

V4.2.1

Question

Hello!
I just discovered the resources in Godot and I have a question, I want to create a loading sequence for a node2d.
The Resource has basic data like name, health, and aim_accuracy.
I have created a new resource type, and a few instances of that resource type.
The resource is working well by using the @export and manually adjusting the resource I’ve created, but I want it to be more dynamic.
Where I store the resources in an array (or any other way possible), and choose randomly one.

example of the target node:

extends CharacterBody2D

# defining veriables
var general_aim
var health
var name

# Used to load the player attributes from a resource database ( this is how it works now)
@export var player_attributes:player_stats # I want this part to be modular by code

func _ready():
	general_aim = player_attributes.general_aim
	health = player_attributes.health
	name = player_attributes.name

example of the resource:

extends Resource
class_name player_stats

@export var name: String
@export var health: int
@export var general_aim: float

what I was thinking of:

# a function to choose random resource
func choose_resource():
   var an_array_of_resource = [resource1, resource2, resource3...] # i don't know how to call them
   var choose_random_resource = an_array_of_resource[randi() % len(an_array_of_resource)]
	return choose_random_resource

and than to load it to the node

extends CharacterBody2D

# defining veriables
var general_aim
var health
var name

# Used to load the player attributes from a resource database ( this is how it works now)
var player_attributes:player_stats = choose_resource()

func _ready():
	general_aim = player_attributes.general_aim
	health = player_attributes.health
	name = player_attributes.name

I suppose that var node = load("res://path/to/resource") is what you are looking for. You can build the path dynamically.

1 Like

Yes!!
Thank you