How do I populate objects from a resource?

Godot Version

4.7

Questions

I was helped by @dragonforge-dev to create a resource for player statistics generation. I need to figure out how to use that to generate player objects in the game world. My idea is to create a pool of players to draft from and add to a team.

I have been looking through the forum, and came across this post, which is aimed at where I want to go:

The code that solved the issue in that post is this:

```func place_item(item : ItemResource) -> void:
	var item_to_spawn : PackedScene = load("res://models/item_actor.tscn")
	var item_actor: ItemActor = item_to_spawn.instantiate() as ItemActor
	item_actor.item_resource = item # Assign the ItemResource to the ItemActor
	var world = get_tree().get_first_node_in_group("GameWorld") # Get the game world node
	var player_position = self.position # Get player's current position
	player_position.z += 0.05 # Place the item just in front of the player
	world.add_child(item_actor) # Add item to the scene tree, which triggers _ready
	item_actor.position = player_position # Set the item's position
	self.actor_data["inventory"].remove_item(item) # Remove item from inventory```

My resource is different, though. I need to place players and attach/append their stats. I don’t have player sprites. I only have markers. My resource code is this:

@tool
@icon("icon.svg")
class_name Player
extends Resource 
 
 

enum Position {
	ATHLETE,
	QUARTERBACK,
	CENTER,
	GUARD,
	TACKLE,
	HALFBACK,
	FULLBACK,
	WIDE_RECEIVER,
	TIGHT_END,
	DEFENSIVE_END,
	DEFENSIVE_TACKLE,
	OUTSIDE_LINEBACKER,
	INSIDE_LINEBACKER,
	STRONG_SAFETY,
	CORNERBACK,
	FREE_SAFETY,
	LONG_SNAPPER,
	KICKER,
	PUNTER
}

enum Depth {
	STARTER,
	BACKUP,
	THIRD_STRING,
	FOURTH_STRING,
	FIFTH_STRING,
	SIXTH_STRING,
	EIGHTH_STRING,
	NINTH_STRING,
	TENTH_STRING
}

@export var position: Position = Position.ATHLETE
@export var position_depth: Depth = Depth.STARTER
@export_range(50.0, 100.0, 1.0) var foot_speed: float
@export_range(50.0, 100.0, 1.0) var strength: float
@export_range(50.0, 100.0, 1.0) var agility: float
@export_range(50.0, 100.0, 1.0) var throw_accuracy: float
@export_range(50.0, 100.0, 1.0) var throw_read: float
@export_range(50.0, 100.0, 1.0) var catching: float
@export_range(50.0, 100.0, 1.0) var routes: float
@export_range(50.0, 100.0, 1.0) var run_read: float
@export_range(50.0, 100.0, 1.0) var run_block: float
@export_range(50.0, 100.0, 1.0) var pass_block: float
@export_range(50.0, 100.0, 1.0) var pass_rush: float
@export_range(50.0, 100.0, 1.0) var run_gap: float
@export_range(50.0, 100.0, 1.0) var run_fit: float
@export_range(50.0, 100.0, 1.0) var blitz: float
@export_range(50.0, 100.0, 1.0) var man_cover: float
@export_range(50.0, 100.0, 1.0) var zone_cover: float
@export_range(50.0, 100.0, 1.0) var kick_accuracy: float
@export_range(50.0, 100.0, 1.0) var punt_accuracy: float
@export_range(50.0, 100.0, 1.0) var snap_accuracy: float

func _init() -> void:
	foot_speed = randf_range(50.0, 100.0)
	strength = randf_range(50.0, 100.0)
	agility = randf_range(50.0, 100.0)
	throw_accuracy = randf_range(50.0, 100.0)
	throw_read = randf_range(50.0, 100.0)
	catching = randf_range(50.0,100.0)
	routes = randf_range(50.0,100.0)
	run_read = randf_range(50.0,100.0)
	run_block = randf_range(50.0,100.0)
	pass_block = randf_range(50.0,100.0)
	pass_rush = randf_range(50.0,100.0)
	run_gap = randf_range(50.0,100.0)
	run_fit = randf_range(50.0,100.0)
	blitz = randf_range(50.0,100.0)
	man_cover = randf_range(50.0,100.0)
	zone_cover = randf_range(50.0,100.0)
	kick_accuracy = randf_range(50.0,100.0)
	punt_accuracy = randf_range(50.0,100.0)
	snap_accuracy = randf_range(50.0,100.0)

	

		
	if (foot_speed + strength + agility / 3) + (throw_accuracy + throw_read + man_cover + zone_cover + catching / 5) /2 == 80 : 
		position = Position.ATHLETE 
	elif throw_accuracy >= 80.0 and throw_read >= 80.0:
		position = Position.QUARTERBACK
	elif foot_speed >= 85.0 and strength >= 75.0 and run_read >= 82.0 : 
		position = Position.HALFBACK
	elif foot_speed >= 78.0 and foot_speed <=83.0 and strength >= 80.0 and run_read >= 78.0 and throw_accuracy <= 80.0: 
		position = Position.FULLBACK 
	elif foot_speed >= 81.0 and agility >= 77.0 and catching >= 82.0 and run_block >= 79.0 and pass_block >= 75.0:  
		position = Position.TIGHT_END
	elif foot_speed >= 86.0 and agility >= 82.0 and catching >= 84.0 and routes >= 78.0 :  
		position = Position.WIDE_RECEIVER
	elif foot_speed <= 76.0 and strength >= 85.0 and run_block >= 75.0 and pass_block >= 75.0 and snap_accuracy >= 83.0 :
		position = Position.CENTER 
	elif foot_speed <= 75.0 and strength >= 85.0 and run_block >= 72.0 and pass_block >= 78.0 :
		position = Position.TACKLE
	elif foot_speed <= 76.0 and strength >= 85.0 and run_block >= 78.0 and pass_block >= 72.0 :
		position = Position.GUARD 
	elif foot_speed >= 75.0 and strength >= 83.0 and run_gap >= 72.0 and pass_rush >= 78.0 :
		position = Position.DEFENSIVE_END
	elif foot_speed <= 74.0 and strength >= 85.0 and run_gap >= 78.0 and pass_rush >= 72.0 :
		position = Position.DEFENSIVE_TACKLE
	elif foot_speed >= 77.0 and strength >= 78.0 and run_fit >= 74.0 and blitz >= 73.0 and man_cover >= 73.0 and zone_cover >= 68.0 :
		position = Position.INSIDE_LINEBACKER
	elif foot_speed >= 77.0 and strength >= 78.0 and agility >= 78.0 and run_fit <= 73.0 and blitz >= 74.0 and man_cover <= 74.0 and zone_cover <= 68.0 :
		position = Position.OUTSIDE_LINEBACKER
	elif foot_speed >= 83.0 and strength >= 76.0 and blitz >= 72.0 and run_fit >= 70.0 and man_cover >= 75.0 and zone_cover >=78.0 :
		position = Position.STRONG_SAFETY
	elif foot_speed >= 85.0 and strength >= 73.0 and man_cover >= 76.0 and zone_cover >= 77.0 :
		position = Position.FREE_SAFETY
	elif foot_speed >= 86.0 and agility >= 82.0 and man_cover >= 80.0 and zone_cover >= 74.0 :
		position = Position.CORNERBACK
	elif foot_speed < 75.0 and strength >= 80.0 and kick_accuracy >= 84.0 :
		position = Position.KICKER
	elif foot_speed < 75.0 and strength >= 80.0 and kick_accuracy <= 84.0 and punt_accuracy >= 84.0 :
		position = Position.KICKER
	elif foot_speed < 75.0 and strength >= 80.0 and run_block <75.0 and pass_block < 78.0 and snap_accuracy >= 83.0 :
		position = Position.LONG_SNAPPER```

	

I guess what I’m having trouble with is figuring out what functions or calls need new names and which need to stay the same as the resouce.

Meant to add: my design flow is akin to modular synthesis. I want to have modules that each do one thing, and then simply connect them to the game world “output.” I am going to make a sprite resource, as well. I just need to know how to connect things. I have an idea now of the object modules, I simply don’t have the “cables” to connect them.

I don’t see how your situation is different, you will want to instantiate some kind of Node2D or 3D with a script ready to handle your resource data.

Here’s another sample, assuming you’ve made a script with var my_player_resource: Player attached to the sample scene root.

var new_player_node := load("res://your_playernode_scene.tscn").instantiate()
new_player_node.my_player_resource = player_resource
add_child(new_player_node)

This is much simpler than I expected. I was trying to re-name everything in the code that was posted and I was getting nothing but error messages.

I’ll try this. Thank you!