How do I replicate an inventory?

I think the point was that if you use structs, you can’t store them as pointers (for replication), so it’s not possible to have an array containing different subtypes.

You can just store identifiers for the item type, rather than instantiated items. If you need to have them instantiated with their own subtype-specific data, then what is wrong with having them spawned in the world? A few hidden, non-ticking actors is hardly going to be a performance problem, assuming inventory contents isn’t changing super fast.

Ah sweet, I didn’t know you could do that! Thanks.

I’ll just gives structs a try.

And I just don’t feel like it’s a very good way of dealing with an inventory(having each item spawned).

EDIT: Alright, so I’m trying to do the same thing with structs now, though I have no idea how to cast structs.
For example, I want to do this:


FItemWeapon weapon = Cast<FItemWeapon>(item);

where item is of type FItemBase.
How do I do this?

Also, how do I check if a struct is null? I tried these:


if (item)
if (item == NULL)
if (item == nullptr)

but they all give errors.

You use standard C/C++ casts with pointers to structs. You can’t cast structs by value.
So you can do:


FItemWeapon* weapon = (FItemWeapon*)item;

assuming item is a pointer to FItemBase.

But as I said above, this won’t work for your inventory. TArray< FItemBase* > is no good since pointers to structs are not replicated (at least I’m pretty sure they’re not). If you just had a TArray< FItemBase > then this isn’t polymorphic - all the items have to be of FItemBase, you can’t put a derived type in that array.