Workaround overriding a static variable?

So, I’m making an Inventory System and I need to have items derived from a base class BaseItem and replace variables from this base class in each child.

I have my BaseItem class that holds basic information that all items should have, this basic information is static so it can be accessed without an instance.

  • Name FString]
  • Description FString]
  • Weight float]
  • Cost int32]

So the problem appears when I need to replace/override this information in a derived class, I can’t override a static variable from the parent class.
I only need this information once for each derived class, that’s why I need it to be static. The inventory also should be able to reach thousands of items and have no problem at all.

The actual inventory is an array of InventoryItem Struct which contains:

  • Item BaseItem Class]
  • Amount int32]

Extra Information:

  • The inventory has to be Serializable so it can be saved
  • There will be lots of items and all derived from the base item
  • The item information should be easily accessible and fast. (fast in the case of having thousands of different items)

How could I workaround this issue?
Or in what other way could I approach this problem?

Thank You in Advance!


There is no workaround. Static variables are fully equal to global variables, just hidden inside class namespace. So your basic information is the same for all classes derived from BaseItem.

Simplest way, don’t use static variables. Just duplicate necessary information.
Also, you can create struct BasicItemInfo, that contains all basic information (Name, Description, Weight, Cost). Create a global array with information for each inventory item type. And BaseItem class should store only index (non static variable) to a some item of this array.

Something like this:

struct BaseItemInfo
	char name[64];
	char description[256];
	float weigh;
	int cost;

BaseItemInfo g_BaseItemInfo] = 
	{ "", "",  0,  0 },
	{ "Item 1", "Description 1", 10, 10 },
	{ "Item 2", "Description 2", 20, 20 }

class BaseItem
	int index;

	BaseItem() : index(0) {}

class Item1 : public BaseItem

	int someNewInfo;
	Item1() : someNewInfo(0) { index = 1; }

class Item2 : public BaseItem

	float anotherInfo;
	Item2() : anotherInfo(0) { index = 2; }

PS: Sorry for my English :rolleyes:
PPS: Also you can use UDataTable class for storing common information.

The simplest way would be to use a Singleton class template, call it ItemManager or something like that, where you create exactly one instance of each item, and simply access it on demand. Works the same as static variables - conceptually speaking - and serves the point. Plus, it is far more flexible and you get an added bonus of having everything in one place. Its how I’ve been doing it for quite some time and it never failed me.

So a typical structure would be:

  • Item Manager
    – Item (i.e. a list of templateable items, exactly one instance of each template)

Then direct spawns on an inventory would be:

  • ItemInstance
    – Item (containing a pointer to item basically)

UDataTable has one major inconvinient: you can only store one information type at a time. Picture this: you have multiple damage types you want to list in an item, or maybe, multiple bonuses. This is fairly easy to do in XML (well, a bit cumbursome as UE4 doesn’t provide a decent XML Parser as far as I saw, their version is a bit confusing tbh), you simply add nested tags after tags. Its a major drawback of XLS and data tables, wish epic would start focusing on XML Parsers as well, I wonder what they actually used for Paragon.

Side note: avoid at all costs hardcoding item information like you have. Its a recipe for disaster should you ever need to update/modify such information.

Thank you! I knew there was no real workaround but I wanted someone to confirm it.
So I will try approaching it by having something like an Item Manager with all the information of the items.

Feel free to PM me, will help in any way I can.