Spawn Default Weapon Inventory


I decided to use an Actor class for my Weapons (as done in the ShooterGame example).

With Actors, though, I need to **SPAWN **the Actor, I can’t simply do NewObject<…> as I used to do with UObjects.

Looking for a solution in the ShooterGame example, I found this:

This is spawning a Default Weapon inventory with a for loop.

The problem is that I don’t find any definition of the DefaultInventoryClasses

I just found this in the ShooterCharacter.h

UPROPERTY(EditDefaultsOnly, Category=Inventory)
	TArray<TSubclassOf<class AShooterWeapon> > DefaultInventoryClasses;

It is declared and used, but where is it defined?

I need to know this in order to be able to create a Weapon Inventory!

they fill the array in the PlayerPawn blueprint (set its defaults), open it and in the class default search for default inventory and it will show up.

Done, thanks.

But when I spawn the Inventory, since they are actors, they also get spawned in the World.

Is this bad practice?

Should I make them invisible? But won’t that be bad practice too?

In a multiplayer shooter game, the ammount of weapon actors that will spawn is massive.

What is the best way to resolve this?

depends on the situation, there are tons of ways to handle it

for example in one of my projects there was only 1 master weapon blueprint and to change weapons we just swapped the mesh and some settings so we did not have to destroy or spawn anything, but on the other hand if you want the weapons to be visible ont he player (like on their back or something) then your gonna want to spawn them.

i think shooter game hides the unequipped weapon

The inventory can contain tons of weapons.

Only the equipped weapon must be visible/attached to the hands of the player.

then it depends on how many weapons you play to have in the inventory

1-4 i say it would be fine to spawn them all (look at gears and halo) but

lets say you make a dark souls type inventory with 100 weapons on the character at all times then you would be better off only spawning whats needed to doing what i posted earlier

They’re supposed to be no more than 15 weapons.

But anyway, since they are actors, I can’t add a Weapon to the inventory without spawning it. It’s a double-edged sword.

I see two options.

One is keep your current implementation but directly after spawning the actor, set the actor to hidden in game. This is what I do (in blueprints). Then just set the actor to visible on equip and hidden on unequip.

The second option is to change your inventory to reference objects instead of actors. The objects would hold all the values (what class, number of bullets, etc.) necessary to spawn the actor. And then only spawn the actor on equip and destroy the actor on unequip. The difficulty here is making sure the objects in the inventory are updated based on what has happened. For example, if you have a gun out and shoot a few bullets, and then unequip, the object in the inventory should know that there are less bullets in the gun. This could open up a whole new can of worms in terms of problems and replication, so you might be better off with option one.

I do something similar in my current project. That might make me biased, but I think keeping them as actors and just hiding them is, in general, a better idea. If you destroy them, you’ll have to make new ones every single time the player equips a different weapon. It’s not super expensive, I suppose, but it is an extra cost. Someone can correct me if I’m wrong, but I believe hidden objects just don’t render and won’t collide, so their ongoing cost is negligible . Also, like TG said above, you would have to track any changes to the status of a weapon separately.