Adding BP_Object to TARRAY - Object added before initialization

Hi guys,

I have an array of spelltype objects that is basically a spellbar holding references to spells that get added dynamically from blueprints. My issue is that lets say I have BP_FireBall, that I used via exposing C++ class named ASpellSystem, adding that BP_Fireball to my TArray comes in invalid. I’m assuming that is getting added before it gets initialized but not sure how to go about fixing this when the init values are set in blueprints.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = SpellBar)
TArray<ASpellSystem*> spellBar;

void AArchetypeCharacter::AddSpellToBar(ASpellSystem* newSpell){
  if (newSpell->IsValidLowLevel()){ //<--- doesn't pass
    GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Adding spell"));
    spellBar.Add(newSpell);
  }
}

Here is a screenshot from my character bp - mind you the fireball is invalid in the bp

and here is one from the fireball it self - in here testing if valid passes since it is after beginplay for the fireball bp:

I was told to use FReferenceCollector::AddReferencedObjects but that wouldnt work considering my spells are not initialized till BeginPlay; and would not make sense in my case since I want the player to choose what spells he or she are going to use.

Also I was researching and I could maybe use PostInitComponents() and call it on the spell getting added, but not sure how to go about this, since all the values get setup under BluePrint_Class_Defaults.

I looked at the other post with the same question but it did not help me.

Thank you,

You do not show how/where your Fireball2 variable is being initialized. Your problem is either that that initialization is failing, or is not being done at all.
Your BP_Fireball is just a blueprint, not an instance. You need to spawn an instance of your blueprint somewhere and assign that to Fireball2, and this needs to be done before calling AddSpellToBar.

If you are initializing it somewhere, post the relevant code/BP and I’ll try to see what the problem is.

Hi,

So i even tried a cast to fireball2 and set my variable, but the cast failed. Here is a screenie (Even put fb2 as input to the cast but non worked).

ty

It’s not a question of needing a cast, you need to create the object in the first place. An object type variable is just a reference to an object, initially it will be empty. You need to create an object of type Fireball2 (or is it BP_Fireball? It’s not clear from what you’ve posted) by calling SpawnActor, then assign the result to your variable, before passing the variable into your AddSpellToBar function.

If you follow through one of the blueprint tutorials, or look at one of the samples, then you can find many examples of how to do this.

Hi sorry for the confusion, the bp named FIREBALL2 is my so called BP_Fireball. and Fireball2 is of type BP_Fireball. Going to post a new SS with the name change so its clear.

So I renamed my BP to BP_Fireball - this has all the values the damage, cast time, cost, etc.

37857-screenshot+-+4_7_2015+,+1_21_14+pm.png

and here is my character BP named testmage, that is trying to instantiate BP_Fireball and add it to my spellbar so I can then cast it, as you can see the Fireball2 is of type BP_Fireball.

Okay, so you should get rid of your initial ‘Cast To BP_Fireball’ node, since you have nothing to cast. Replace it with a SpawnActor node, specifying BP_Fireball as the class to spawn. Then pass the output into your ‘Set Fireball2’ node.

But thats the thing im trying to avoid, I already have a cast function in code, I want to avoid having to preset spawnActor because I dont know what spells the player will pick before hand.

(SpawnActor Node would require additional info before hand as well, like location etc, all of this is already done in C++)

The way it should work is :

  • Player picks 6 spells
  • Add the 6 spells to the spell bar
  • They press 1 , cast index 0 from spell bar

As you can see from my first post I add the spells to a TArray, and here is the part of the code that casts from the TArray using the index.

My issue is not the casting, my issue is that I cant add copies of the fireball to my TArray because it is invalid.

EDIT: It is very similar to:

Which was a bug at the time, and I find it very hard to believe that there is no workaround for 7 months =/

Okay that makes sense. But then where is the code/BP relating to the player picking spell types? So far you haven’t posted any code which actually creates a spell object, which is the reason your AddSpellToBar is failing, since your variable is apparently still uninitialized.

That part is not implemented yet, I just wanted to test my code, so I ended up using beginplay and faking it.

Essentially it will be a UMG button that holds a spell, and I click it which then adds that spell to the spellbar. But it just doesnt make sense to me that creating an object of type BP_Fireball wouldn’t be initialized… that is the entire point of creating such object, is to copy those values.

Hell I even tried going under BP_Fireball and creating a function to return self, which I then use to set Fireball2; but even that did not work.

37884-screenshot+-+4_7_2015+,+3_32_44+pm.png

Ty for sticking it out with me, I have been stuck on this for weeks.

Okay, but like I said, you haven’t actually created the object anywhere, so far as I can see. You’ve created a blueprint (from which objects can be made) and you’ve also created a variable of that blueprint type (which is just a reference), but until you call SpawnActor, there is no actual object.

I’d suggest you add the SpawnActor node as I suggested above. Then later when you have your UMG widgets, each one could have an associated UClass property - you would set in the editor that one widget is associated with BP_Fireball, another with BP_AnotherSpell, etc. Then instead of hard coding BP_Fireball into the SpawnActor node, you instead pass in the UClass from the clicked widget as the class to be instantiated.

Woot that worked!!! man 2 weeks stuck on this. Tyvm!