Custom Inventory System - Some Questions

Hi, i want to create an inventory system, but i don’t want to use Structures because they are still bugged. (See here).

Question 1: I think that there is no need to spawn an “Item” as actor and attach it to a Object Inventory, just for performances purpose.** It is a proper thinking?**

Using GitHub - getsetgames/BlueprintReflection: A plugin for Unreal Engine 4 that adds reflection capabilities to the blueprint system. i was able to instanciate UObject based class from blueprints (thanks to the author).

The problem is that UObjects created are not destroyed, and foreach PIE run, i can see for example:

PIE 1:

  • UObjectChildClass_01
  • UObjectChildClass_02
  • UObjectChildClass_03

PIE 2:

  • UObjectChildClass_04
  • UObjectChildClass_05
  • UObjectChildClass_06

Question 2: How i can free the references to this objects?

They are. You really have to go out of your way to instantiate UObjects without them getting cleaned up during world teardown. (See: RF_RootSet, AddToRoot, etc.) And if you do end up with a lingering object in the end, the engine will throw all sorts of warnings at you.

What you’re seeing is simply the internal counter incrementing with each instance. This counter allows object names to be unique without having to manually name each one of them.

In other words, don’t worry about it.

Thanks for your reply, but they are unecessary so there is no need to keep this instances, it’s a memory optimization

The instances are being destroyed. The counter value persists for as long as the engine is running, which in the editor can span across multiple PIE sessions.

See for yourself, put a TObjectIterator somewhere in your code:

	for( TObjectIterator<UObjectChildClass> It; It; ++It )
		UE_LOG( LogMyGame, Display, TEXT("UObjectChildClass: %s"), *GetNameSafe( *It ) );

You will see that you only ever have as many instances as you created in that run, even if their indices aren’t starting at 0.

Thank you cmartel. And how i can check it if UObjectChildClass is a blueprint class?

That’s not something that’s commonly done… Normally you want to use classes transparently, regardless of if they were implemented in blueprint or native. It might help knowing what are you trying to achieve by checking this class?

In any event, assuming UObjectChildClass is the base class you want to use for your inventory system… You already know that it is not a blueprint class since it is a native base. However, if you have an instance of such an object, you can call GetClass() on it to get the actual class of the object. For objects implemented in blueprint, GetClass() will return a special kind of class, UBlueprintGeneratedClass, which contains extra information about the blueprint data. But as mentioned, you rarely have reason to operate on those.

Hello Kheka,

The answer to your first question (in my opinion) is yes. I have built my own inventory system utilizing UObject, however there is a caveat to that. As it stands Actors are the only things that do network replication, base UObjects can only be replicated FROM an actor (even though actors are UObjects, Actors are the first in the class hierarchy to implement network replication), UActorComponents support replication but still utilize the owning actor for network replication. So if your game is single player you have nothing to worry about, however if your game is multi-player (like mine) then figuring out replication will be a royal pain as I have found the documentation is severely lacking on the C++ side.

Your second question was answered by cmartel and I cannot answer it any better than he/she :slight_smile: