Automatic Item Loader

I am making a FPS game and it has a lot of items/weapons in it. So, to keep track of all of them, I added a variable to the “Item” script each of these items have in their prefab called “ID”, which holds an integer. This ID is used for a few things, for example stacking multiple items of same type in the player’s hand.

Thing is though, I had trouble keeping track of what weapon have what ID, so to make things easier I made a script “ItemManager” and added an array of “Item” scripts to it. I can just drag all the items that I am going to use and throw them there, and it will automatically assign each of them an ID based on their position on the array, so I don’t have to worry about that.

However, this is not optimal. For one, I have to manually drag all items into the ItemManager before starting the scene. For two, I am not sure how could I make this work after I build the game. How will the script load all items? And finally, I don’t need the game to load every single item every time it loads the scene, would be easier if it just loaded once when I start the game (the built one I mean). Even better, if possible make the game update the list literally only once when the game first starts, save it in a folder and never do it again.

So how do I that? I want a way to get every single prefab with component X (before void Awake ()), regardless if I am using the editor or a standalone build, once, then change a Y variable inside this component X permanently, saving the results in a file that it can read later if it needs this information again (preferably cryptografed(is this how you spell it??) just like the files Unity makes when it builds your game, to prevent hacking).

Another thing I forgot to mention, I plan on this game having Multiplayer so I would also require for the saved file to generate the same array every single time that the game is run the first time, so the ID list for all copies of the game are the same at all times.

I like to use ScriptableObjects to organize stuff. For instance, here’s a generic GameObjectCollection,and you can make as many of these things as you like, such as one for guns, one for ammo types, etc.

https://gist.github.com/kurtdekker/68b3fd9d4202888b4afc17b32eed6790

Just have a loading scene, then additively load subsequent scenes once the content is ready.

2 Likes

Much like @Kurt-Dekker said, what I usually do in this situation is make scripable object, place them all in a folder in the Resources folder and load them to a static class(or a normal component with don’t destroy) on game start.

Sorry the late reply, yeah I will try this out if I have any issues I will respond them here. I will mark this thread as resolved, but we can still talk here in any case.

1 Like

Ok I made a ScriptableObject with all the items, but I didn’t know how I would automatically set all objects’ ID like I wanted, so I removed that shuffle part (as in my case shuffling would be the nightmare, so there is no reason to ever shuffle) and I added this method:

    public void SetID()
    {
        for (int i = 0; i < ItemArray.Length; i++)
        {
            ItemArray [i].ID = i;
        }
    }

But now I run into the same issue, as I have to call that method on GameManager everytime the game starts.

Do you care what those IDs are in the sense that other things refer to them? If so, then make an editor script and actually change the prefabs on disk to have the IDs you want.

Remember though, Unity already supports excellent item-referential serialization right in the editor, so make sure you’re not reinventing an inferior wheel by using an ID system.

This isn’t possible. Every minute you spend trying to do this impossible thing is a minute you are never getting back and that you are not spending working on your game.

1 Like

Of course, why would I use IDs if I don’t refer for them to anything? Currently, the only use they have is stacking items in slots in inventory, so if the player picks up an item that has the same ID with whatever they were holding earlier, they stack. But I plan on using them for commands as well, so the player can spawn an item with something like /spawn id:22 to spawn an item with the id of 22.

I am gonna pretend I didn’t see this thoughtless comment.

Prepare to pretend you didn’t waste a lot of your time! :wink:

1 Like

I already lost a lot of time with just being lazy lol.

But giving up partially fixing a problem because you can’t fix it 100% is a stupid idea. By that logic CS:GO would be teeming with spin-botters, “well you can’t get rid of all of them so why mind?”.

Would adding this class to the ItemCollection script (not the ItemCollection class itself, obviously) be the most efficient way of doing so?

public class ChangeID : Editor
{
    private ItemCollection ItemCollectionScript;

    void Awake()
    {
        ItemCollectionScript.SetID();
    }
}

In our game we use the prefab name as Id, works well. It’s mostly used when you save weapon attachment config to disk though. Otherwise we use the normal unity way of referencing prefabs.

I personally don’t like using strings as IDs because they are more easily mistaken, even if they are obvious names like the name of a gun “Glock”, you still need to know if it is “glock” or “Glock”, or if you added a sufix to it like “Glock_17” or “Glock17”, in the case of say an object that has synonims, you might forget it you called an object “Light” or “Lamp”.

Number IDs, although harder to memorize, you can’t mistake them, if you know the Glock’s ID is 10, it will always be ID 10 no matter what, you don’t have to know anything else about it. It’s also easier to write something like /spawn 45 than /spawn liquid_nitrogen_barrel, where you can mistake a letter or two.

Also, I believe it’s more performant for the computer to use integers than strings, but that might not be a big concern nowadays.

1 Like

Well in our case no human ever touches them. Our number ids are just the index in the scrtipable object array that contains all in game items.

Our item spawner takes the prefab reference, can never be more safe than full reference safety :wink:

7025299--832132--upload_2021-4-10_21-8-46.jpg

1 Like