Creating an InventoryItem Editor

Regardless of what comes out, id refrain from using anything before 2020, maybe 2019 because at this point your engine version is 4 years back, I don’t even think many tutorials use this version

Honestly was just following along with what Rick/Sam have been using in the videos to this point. And to answer my own question, yea TGC just isn’t part of 2018.3. Guess it’s as good a time as any to migrate the project to a newer unity version

1 Like

yeah prolly is, I like to keep a 2021 and a 2022 version, but thats just me, you can keep 2018, and just install 2022 or 2021 on top of that

TryGetComponent was added in 2019.2. The Get The Project lecture in Inventory should have instructed you to use 2019.3 for the Inventory course (we recommend upgrading your project to the current version in the lectures as the courses progress, sticking to LTS versions. I recommend the latest LTS in Unity 2021, as Unity 2022 has not yet gone to LTS.

Yea I ended up just grabbing a 2022 version. Great tutorial, by the way, on an item editor!

For the ‘Use’ function in Part 5 of this tutorial, my ActionItem.cs ‘Use’ function (which is supposed to be overridden) is a boolean, and the ‘Use’ function of the HealingSpell (the overrider) is a void, which is causing me a massive collision (my compiler can’t detect the function to override as a result). Which one do I change to meet the requirements of the other?

Also this line of initiating the ‘potentialPickup’:

GameObject potentialPickup = pickup ? pickup.gameObject : null;

Is not being picked up by the compiler. I am aware that we have a pickup initiated at the beginning of the class, but the compiler for some reason just doesn’t want to pick it up, and I can’t run my code as a result (Edit: I placed it in the ‘SetPickup’ function of InventoryItem.cs, and it works now (please tell me if I’m doing it right or wrong!). First problem still persists though)

Edit 2: Apparently deleting the keyword ‘override’ fixes the first problem, but I’m not sure if that’s the right thing to do… but it works I guess for now.

Please tell me if I solved the two problems the right way or not

If your Use() method is set up as a boolean, it’s better to leave it a boolean.
This method will compile:

public bool Use() []

even if you call it l ike a void function. The resul is then just discarded. This doesn’t work the ter other way around.

This change is done in the ‘ActionItem.cs’, right? Trying that in ‘HealingSpell.cs’ just causes me issues (also how do you create a Pickup again?

I tried overwriting my OnGUI function in ‘InventoryItemEditor.cs’, from this:

void OnGUI() {

            if (!selected) {

                EditorGUILayout.HelpBox("No InventoryItem Selected", MessageType.Error);
                return;

            }

            EditorGUILayout.HelpBox($"{selected.name}/{selected.GetDisplayName()}", MessageType.Info);

            selected.SetItemID(EditorGUILayout.TextField("ItemID (clear to reset)", selected.GetItemID()));
            selected.SetDisplayName(EditorGUILayout.TextField("Display Name", selected.GetDisplayName()));
            selected.SetDescription(EditorGUILayout.TextField("Description", selected.GetDescription()));
            selected.SetIcon((Sprite)EditorGUILayout.ObjectField("Icon", selected.GetIcon(), typeof(Sprite), false));
            selected.SetPickup((Pickup)EditorGUILayout.ObjectField("Pickup", selected.GetPickup(), typeof(Pickup), false));
            selected.SetStackable(EditorGUILayout.Toggle("Stackable", selected.IsStackable()));

            selected.DrawCustomInspector();

To something like this (I’m following the code on Git):

void OnGUI() {

            if (selected == null) {

                EditorGUILayout.HelpBox("No Item Selected", MessageType.Error);
                return;

            }

            if (!stylesInitialized) {

                descriptionStyle = new GUIStyle(GUI.skin.label) {

                    richText = true,
                    wordWrap = true,
                    stretchHeight = true,
                    fontSize = 14,
                    alignment = TextAnchor.MiddleCenter

                };

                headerStyle = new GUIStyle(descriptionStyle) { fontSize = 24 };
                stylesInitialized = true;

            }

            Rect rect = new Rect(0,0,position.width * .65f, position.height);
            DrawInspector(rect);
            rect.x = rect.width;
            rect.width /= 2.0f;

            DrawPreviewTooltip(rect);

        }

And here are my results (similar to Bantus, but I think my issue is completely different):

Please Help. I struggled to follow up in the end, regarding which function demands which lines of code (needless to say the issue persists in the game runtime as well)

I also didn’t know where to enable ‘Rich Text’ from, hence when the game is running, the colors aren’t visible (instead, I get the color reference we put in the game code. Please help me with this too)

For my Ranged and Magic weapons, this is what I see during game Runtime:

and for my Melee weapons, it gets worse, since there’s no image to start with:

(basically it just says ‘Melee weapon’ repeatedly, similar to the ranged weapons, during the game runtime…)

So to keep it short, I need help with:

  • The code (eliminating the issues)
  • Activating Rich Text
  • The background not existing (I want a similar background to what we saw at the start of this post)
  • Possibly stylizing the title and text to make this game a bit more fun to play

Please help

Go into each TextMeshPro object in your UI. Within the inspector click on “Other Options” and then check the Rich Text option. You’ll need to do this with every TextMeshPro you want to have rich text.

It looks like you have some sort of recursion going on, but I’m not sure because generally recursion crashes the editor.

In this case, we’re going to have to look at the DrawPreviewTooltip method, and follow the chain to the GetDescription() methods.
It LOOKS like this is happening:

I’m not exactly sure where in any of the 4 edited scripts the GetDescription() function you provided above exactly is. All of them have a uniquely overrideen one, and none of them has the coding snippet shown similar to the one provided above. I also checked the git code again (for the last lesson) to compare it to mine, and I still couldn’t find it either. Where can I find this code, or which function do I edit to make it stop recursing itself? :slight_smile: (I’ll get back to the rich text issue later)

And hovering over some of the equipment (with the tooltip) crashes the engine at runtime sometimes… (or freezes it at the very least, because of the repetition)

Edit: I tried modifying the return type of ‘GetDescription’ of ‘WeaponConfig.cs’ to match the given snip above, but it activated a recursive function that shut Unity down, and I can’t think of any other solutions… (so based on that, I can confirm there was no recursion at first, so it’s something else…)

I also tried adjusting my code for this line in ‘InventoryItem.DrawCustomInspector()’, making it a direct solution, so instead of this:

SetItemID(EditorGUILayout.TextField("ItemID (clear to reset)", GetItemID()));
SetDisplayName(EditorGUILayout.TextField("Display Name", GetDisplayName()));
SetDescription(EditorGUILayout.TextField("Description", GetDescription()));

I changed it to this (as it was mentioned to another user above):

SetItemID(EditorGUILayout.TextField("ItemID (clear to reset)", itemID));
SetDisplayName(EditorGUILayout.TextField("Display Name", displayName));
SetDescription(EditorGUILayout.TextField("Description", description));

and the issue still persists… Please help

For the tooltip section (and the GetDescription() code that should be there, look above for Lesson 6. My last post was a quick example on the recursion issue.

Already went through lecture 6 four times so far, and the issue still persists… Any alternative solutions we can examine to identify where I’m going wrong? I apologize if I’m being a bit of a nuisance right now, but I mentioned everything I tried so far, and I really want to figure out why it’s being repetitive…

Adding the base.GetDescription(); in all my ‘GetDescription()’ functions just keeps resulting in my UnityEditor to crash, and I can’t find a way to stop it from repeating itself (and just adding ‘GetDescription()’ to the result makes the editor crash much faster…)

Edit: it took me an insanely long time to figure out what’s going on, but I FIXED it… I revised the solutions from above, and apparently I figured out changing ‘GetDescription()’ to ‘description’ in InventoryItem.DrawCustomInspector().SetDescription() solves the issue (thanks again Brian). Next, I deleted the current description of my weapons in the game engine and re-wrote them, and that solved my problem (I didn’t delete the current description for a long time, I completely forgot to even do that…)

1 Like

Just a follow up question. Any way we can include in the tooltip the requirement levels to wield an item through code, rather than manually? I’d also love to color the requirements based on whether the player achieved them or not (it will scan through the level, Quests, Traits, and any other evaluators we will need, which will determine if the item can be equipped or not (if it’s equipable, that is). If he has the requirement, it will be colored green. If not, it will be colored red, the same way we colored the ‘bonus’ thing in the tooltip). Considering how frustrating it gets sometimes that I don’t know what requirements I still need to wield an item, this would be an extremely helpful function in my game

It would be a nice function, but you’re asking quite a bit on this one. You can find out if the character meets requirements on an EquipableItem with CanEquip, passing in the slot and Equipment component. Breaking things down to individual requirements requires you to manually reach out to the relevante components and checking. At this point, you should be getting the hang of these things…

For changing colors, check this page on TextMeshPro’s RichText codes: Rich Text | TextMeshPro | 4.0.0-pre.2

Privacy & Terms