How is the Shooter Game populating the TArray of AShooterWeapon?

In the Shooter Game example, there are 2 weapons that the player starts with. Both classes (ShooterWeapon_Instant / ShooterWeapon_Projectile) are inheriting from ShooterWeapon. The thing that confuses me, is that in ShooterCharacter.cpp it only calls the SpawnActor method using DefaultInventoryClasses[i] as the class it spawns. When I use prints to debug, DefaultInventoryClasses is already initiated with both of the derived classes. Where is this happening? I don’t see anywhere that the DefaultInventoryClasses TArray is being populated by those variables.

ShooterCharacter.h

/** default inventory list */
	UPROPERTY(EditDefaultsOnly, Category = Inventory)
	TArray<TSubclassOf<class AShooterWeapon> > DefaultInventoryClasses;

ShooterCharacter.cpp

	int32 NumWeaponClasses = DefaultInventoryClasses.Num();

	for (int32 i = 0; i < NumWeaponClasses; i++)
	{
		if (DefaultInventoryClasses[i])
		{
			FActorSpawnParameters SpawnInfo;
			SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
			AShooterWeapon* NewWeapon = GetWorld()->SpawnActor<AShooterWeapon>(DefaultInventoryClasses[i], SpawnInfo);
			AddWeapon(NewWeapon);
		}
	}

Thank you! I was so confused because I couldn’t find any references anywhere.

From here, a few questions.

  1. Why is it done like that? Why is the array set in Blueprint rather than code? It seems like it yields the same outcome.
  2. If I were to do this in code using structs for the array elements, what would be the best way to do that?

Hello nukeydukey69,

These are being set inside of the blueprint classes that are being created based off the ShooterCharacter class. When you use TSubclassOf when declaring a UPROPERTY, this allows for that property to be set to any class type that is of the declared type or a subclass of it. You can see an example of this in Content/Blueprints/Pawns/PlayerPawn. The DefaultInventoryClasses will be listed there in the details panel.

This is also included in our Battery Collector tutorial if you’d like to check that out sometime.

Hope this helps!

  1. It’s a good way to make these types of things that could change at any time (such as by map type, game mode, general design) available to other members of a team that aren’t quite as well versed in C++. It also doesn’t detract from speed or performance as this is all done prior to runtime. In general, it’s easier to pick from a list of all available classes rather than changing it in code and needing to compile to save those changes.
  2. If you’re looking to use structs, I’m actually not sure how you’d do this other than using a single class and using enums inside of the struct to do your selection. As far as I know TSubclassOf can’t be used with structs so the same workflow isn’t an option.
  1. Ah that totally makes sense. I’m trying to challenge myself to use as much c++ as possible, so I’ll figure it out within the code.
  2. My bad, I think I meant something like a TArray. The Shooter Game example uses one as well, and I was unsure how to fill it in code. I found this example which is exactly what I’m looking for.
    Find all subclasses of a certain baseclass? - C++ - Epic Developer Community Forums

Thanks!