TArray get item

Hi everyone,

in a C++ class I have something like that :

TArray<TSubclassOf<class AArmorMod> > modSlot;

and I would like to do something like :

for (int32 i = 0; i < modSlot.Num(); i++)
	{
		AArmorMod *current = Cast<AArmorMod>(modSlot[i]);
		//do something with current
	}

modSlot will be set with some BP classes which extend AArmorMod

how can I do that ?

What you want modSlot to hold? If armor classes then you shoud use

TArray<UClass*>  

if objects spawned from class you should use

TArray<AArmorMod*>

the TArray is holding some BP classes which extend from AArmorMod
AArmorMod is the main class for armor modification, the mod will be created in BP

But class or object of already spawned class? Because class (which blueprint also is) is like master copy for objects you can spawn

I Have created a BP that extend from AArmorMod like that :

I have created another BP that extend from AArmor and I have added in Property the Mod in the array

after that, for testing I do that in the level blueprint :

Ok do this

for (int32 i = 0; i < modSlot.Num(); i++)
    {
       UClass *current = Cast<UClass*>(modSlot[i]);
       //do something with current
    }

You would use AArmorMod pointer if you already had spawned object, but you made array of UClass which are refrence to classes. Most likely you would like to spawn them before doing anything.

()->SpawnActor(current)

And most likely you want to keep return (which will be AArmorMod*)

so I need to spawn all AArmorMod before ?

In curent I can acces to the property/atribut of the AArmorMod ?

I need to get an AArmorMod do some stuff around it like that (for a mod with shield)

for (int32 i = 0; i < modSlot.Num(); i++)
	{
		AArmorMod *current = Cast<AArmorMod>(modSlot[i]);
		if (current->hasShield)
		{
			maxShield += current->maxShield;
		}
	}

But you made TSubclassOf array, now it only hold pointers to UClass which is like class identificator not objects, you can’t do much with it other then spawn it or search for objects on level. Maybe this way you will understand, UClass* is a type used in “Class” (purple one) input of SpawnActor node in Blueprint.

Make arrays like this:

TArray<TSubclassOf<class AArmorMod>> initalMod;
TArray<AArmorMod*> modSlot;

And then you can do something like this:

//Checking inital armor list
    for (int32 i = 0; i < initalMod.Num(); i++)
        {
           //Spawning armor
           AArmorMod *current = ()->SpawnActor<AArmorMod>(initalMod[i]);
           //adding spawned armor to slot
           modSlot.Add(current);
           //Doing things
           if (current->hasShield)
           {
             maxShield += current->maxShield;
           }
        }

Would be better if you also had functions to adding armor (with UClass* input and spawn if you want) and remove armor so you can do that on the fly during game… thats the basics of inventory system ;]