How should i used packedscene?

Godot Version

4.3

Question

how should i use packedscene?
should i do like A or B or have better way to do it?
A :
1 - i make new autoload - name- PackedSceneManager
then i put dict/func
like this

var item_icon : Dictionary ={
	"unknow"    : preload("res://assets/ui/item_icon/unknowItem_32x32.png"),
	### seed
	"UnknowSeed": preload("res://assets/ui/item_icon/seed/unknow seed.png"),
	"WheatSeed" : preload("res://assets/ui/item_icon/seed/wheatSeed.png"),
	"TomatoSeed": preload("res://assets/ui/item_icon/seed/tomatoSeed.png"),
	"WeedSeed"  : preload("res://assets/ui/item_icon/seed/WeedSeed.png"),
	"PineCone"  : preload("res://assets/ui/item_icon/seed/pineSeed.png"),)

or like this

func get_plant_PackedScene( ID  ) -> PackedScene :
	var new_id
	if ID is String:
		new_id = ID
	elif ID is Entity_ID_enum:
		new_id = Entity_ID_enum.find_key( ID )
	
	match new_id : 
		"DiePlant"   : return preload("res://base resources/plant/DiePlant_ND.tscn")
		"WheatPlant" : return preload("res://base resources/plant/WheatPlant_ND.tscn")
		"TomatoPlant": return preload("res://base resources/plant/TomatoPlant_ND.tscn")
		"WeedPlant"  : return preload("res://base resources/plant/weed/WeedPlant_ND.tscn")
		"PineTreePlant" : return preload("res://base resources/plant/pine tree/PineTree_ND.tscn")
		_: 
			print("plant manager dont have this id")
			return null

then when i need any of this i just take it

#####################################################################

B :
i just make new var in each Entity/obj like this
var WheatSeed := load("res://assets/ui/item_icon/seed/wheatSeed.png")
like when i destroy a plant, the plant will drop wheat seed, so just put var like this in plant.
and when i want to drop seed form inv , i do the same in inv slot.

#####################################################################

can someone tell me which one or another way to do more effectively? and what pro and con.

i also dont understand about saving and unsaving resource, ( i saw many video say something like this "if you do like this or like that you can save allot resorce " , they talking about memories?), just in case you talk about saving resouce pls explain bit to me.

Preload will cache the object at runtime, allocating memory upfront. Load will cache when called, allocating memory at that time. (Both will hold that memory indefinitely unless manually told to unload the resource)

Depending on the asset load could take an asynchronous amount of time. Not necessarily a con but just be aware of the difference and could cause a hang depending on the asset.

Preload is preferred for smaller and static-path resources in general.


Generally what I do in a scene is create a static function for the class.

Extends Node2D

class_name Plant

static func create() -> Plant:
  return Preload("res:\\plant_scene.tscn").instantiate()

... # Plant implementation 

Using it like this

# some other scene
...
var new_plant : Plant = Plant.create()
...

You can also setup args to do any customization if you like during the create call.
Which, depending on your organization, could be hard to manage in a singleton that handles many scenes.

I will say after rereading the link above, you may want to have your own resource management system for larger projects. Which I think will require an entirely different solution then anything presented here.

1 Like