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

I have an array of resources as is discussed here. I step through the array in series to change the appearance of the sprite.
I have other series that I would like to add. Can I have an array of arrays and then call the nested arrays to get to the resource I want?
Example:

var texture_series1 = [res//texture1, res//texture2]
var texture_series2 = [res//texture3, res//texture4]
var series = [series1, series2]
var texture = 0
var texture_index = 0
sprite2d.texture = load(series[texture[texture_index]]) < - - How would this line actually read?

This would allow me to make each series an integer variable that I can call and change at will to make different sprites with the same scene functionality but different appearances.

@ tesfalcon
it should work, except you get elements from nested arrays like this:
load(series[texture][texture_index])
if both texture and texture_index is 0 then this will load res//texture1

Awesome. Trying it now.

It works!

1 Like

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