Making a modular npc system using resources

Godot Version

Godot 4.2

Question

I’m trying to make a modular npc system because I’d like to add tons of types of npcs easily. By npcs I’m also including enemies, etc kinda like entities so to say.

Currently I have Hurt/Hitbox and attack components with a system that tracks the condition of the npc’s body parts. However I tried storing the information for the body in a resource so that in theory I could just use that resource for the same npc type,


(example of the resource)

but then I realised every npc that uses that same resource file will share the same body condition. Is there a way for each instance of the npc to still have their own information while still using a resource file to declare an npc type? Because I would like the ability to save the condition of npcs and make them persistent. Or is there a better way of doing what I’m trying to do.

Thanks in advanced

Resources have an option to make them Local to Scene. This should fix your issue.
image

1 Like

that should’ve worked, so I’m guessing something’s wrong with my code. Sorry for the horrible code and Frankenstein image, I can only upload 1 image.

The local_to_scene property will not make the resource unique for that scene (maybe using duplicate() could solve it).

Think of the Resources only for store the data. If you want to change it (like decreasing health when hit), you can define the variables in the node that will handle this information and then assign the value from the Resource file to that variable.

var health:int

func initialize() -> void:
     health = resource.health

From there, you can manipulate the data that will afect only that exact NPC.

2 Likes

That was actually my first thought, but I kept getting the error:
Invalid get index ‘parts’ (on base: ‘Nil’)

Which is why I turned to the forums, because I’ve been thinking about this game too much and it’s starting to tire my brain

@export var BodyParts: Body

var parts:Dictionary

func _init():
	parts = BodyParts.parts

ok, if you are not forgetting to assign a resource to BodyParts, maybe the problem can be that you are calling this in _init() function. This method happens really early in the tree, maybe the BodyParts isn’t ready yet.

For these things, I reccomend using a method for initialization of the node. Then, in the parent (or anywhere else that is instantiating this node) you call that initialization function.

func initialize():
   parts = BodyParts.parts

In the parent, you call initialize().

PartsComponent.initialize()

This way, you have more control in the initialization of the the scripts and its content.

1 Like

That fixed the error. But all of the npc’s still share the Body Parts conditions. So I’m back at step 1. I think this problem isn’t part of my original question anymore since you guys have told me the solution but I’m the one having problems implementing it

To make a resource unique, try to duplicate it.

var new_resource = resource.duplicate()

Can you describe in more details how are setting up this resources and scenes that will use it? Maybe a link with a project sample so we can analyze it?