I have a small project I’ve been creating just to learn and I have a resource for my NPCs to hold stats and I wanted to add an alignment so NPCs can be friendly/hostile to each other.
I was thinking about using a dictionary with each NPC and a bool to determine hostility but would that not work because its passed by reference? Also, using a dictionary looks like it would muddy my NPC resources as the scope increases having to go through the inspector and tick every box for every NPC.
Or could I use groups in some way? I’m not sure if you can set a group in a resource but I could assign a group when the NPC is instantiated based off its resource. I’m not too sure what direction to take, I’ll probably try the latter, but any advice would be helpful.
how could i use an enum to track the alignment between multiple factions? say npc 1 is friendly with npc 2 but hostile towards npc 3 would i need multiple enums to track this? or could i just use a global enum to track all faction alignments?
If you set-up the enum for alignment in your entity or npc class you can instance each npc with the desired alignment. If hostility is something that changes dynamically based on something other than alignment you’d have to add additional logic.
yea im looking for simplicity here but im not sure how to set it up with an enum. im assuming i just put each faction in the enum but i dont know how to export it to the inspector so i can change it for each NPC resource
If hostility is static and only related to alignment, an enum is for sure the way to go. All it does is map integers to names that are easy to interpret by a human programmer (so it’s easier to write the logic). You can expose the enum to inspector with @export, but you can also change alignment through code if you’re instancing at runtime.
If you’re not sure about custom classes or inheritance yet, this is the perfect opportunity to learn. Using enums for alignment, then assigning it for each instance is very common and there’s loads of tutorials for it.
Don’t really know this guy or source, but this write-up looks fairly solid. Then for inheritance you can look for a basic tutorial that covers the design principle yourself, or have a look at the detailed docs on classes
thanks for the links inheritance and custom classes is something i understand enums ill have to play with more to understand how to utilize them like one thing that isnt clicking for me is if i have say an enum with all my factions in my npc resource how do i set their alignment for when they get instantiated ? currently i have 2 custom npc resources the resource holds a texture, health, and move speed i just dont know how i would set their alignment to one another so i could do something like if hostile attack else dont attack. i could do a match case for each npc to check against every other npc but that seems like it would be a pain as i add npcs
For the enum, you create it first than assign it to a var. If you export the var it shows up in the editor under properties. Something like:
enum Factions{HOSTILE, NEUTRAL, FRIENDLY} # defines enum
# sets enum to var faction (that can now only take HOSTILE, NEUTRAL, FRIENDLY as values
# @ export makes it show up in the editor
# No default takes first faction so in this case everything defaults to HOSTILE
@export var faction: Factions
func _ready() -> void:
# instance through code
var neutral_npc = npc.instantiate()
neutral_npc.faction = Factions.NEUTRAL
add_child(neutral_npc)
Yeah it’s a bit of a burden. You can store faction hostility as a relational matrix (store as a singleton dictionary) that you set up once so you can call a function that references that data and outputs a bool for hostility. I don’t know of an easier way than that.