Uniquely identifying large number of prefabs without references

I’m working on a scripting system who I would like to function without any type-specific knowledge about the objects it operates on, so I can just write something like “MoveObject(MyObject, SpawnPoint1)”, and trust that the system will find any objects matching MyObject and SpawnPoint1, and execute whatever logic I’ve requested on them.

This seemingly points to a single, universal ID system that uniquely identifies every object in the game my scripting stuff could possibly refer to, but if you add up all the NPCs, spawn points, save points, items, and miscellaneous interactables, you’re looking at a list numbering in the low thousands. Making one giant honkin’ enumeration with a unique value for every object doesn’t sound practical, but at the same time labelling them with an int or string and running comparisons every single time a script executes sounds even worse. Is there a better solution I’m missing, or is my best bet making the actual scripting commands themselves one layer less generic, so instead of calling MoveObject I’m calling MoveNPC, or MoveSpawnPoint or whatever, and use a much smaller pool of enums for each object type?

Use a “Dictionary” instead of a List, and don’t iterate over it to find the match but use the key, which could be whatever you want, like an object’s Hash value or name or whatever, to retrieve the object reference.

Though you question is weird, because how exactly are you going to know what object you want to move in the first place, if you don’t already have some sort of interaction or reference to it in the first place?

If you are interacting with objects and want objects of various different types to be able to do some of the same functionalities, then implement a common Interface for them that defines this functionality, then you code can simply say “If object has IWhateverInterface, call MoveTo(target);” as an example.

1 Like

Hmm okay, thank you for the recommendation- and I should’ve clarified, this is as part of a data-driven quest system, so a writer can write logic that will effect the game world without hardcoding anything. We have a list of 100-some unique NPCs, and each level has its own unique spawn points, interactable items, and so forth. So from the writer’s perspective, they need to be able to specify a fairly generic range of functionality, stuff like “Once the player starts this quest, move Jimmy the NPC’s spawn point from in front of the tavern to behind the seedy alley”.

Alright, then a dictionary would work well for you. You can store them all in a Dictionary<string,GameObject> or maybe Dictionary<string,NPCClass>, and simply access one at any time from the Dictionary with something simple like npcDict[“Joe”].someClassFunction(), or npcDict[“Amy”].gameObject.SetActive(“false”);, you get the idea. They’re extremely efficient for getting a specific value out of a large list versus other methods.

You’ll want to write it to avoid any null ref exceptions though, so something like:

private Dictionary<string, NPCClass> npcs;

public void MoveNPC(string name, Vector3 toPos)
{
    NPCClass npc = null;
    if(npcs.TryGetValue(name, out npc)) //If key is found, output value into our npc local var and do something with it
    {
        npc.transform.position = toPos;
    }
}
1 Like

That is considerably easier than all the overcomplicated methods I was half-considering, thank you very much for clarifying! :slight_smile:

If this is for editor time, another alternative is to use reflection and then create proper references in the background.