x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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 alt text

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

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,

Product Version: Not Selected
Tags:
more ▼

asked Apr 04 '15 at 07:34 PM in C++ Programming

avatar image

Venros
129 10 11 15

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Apr 07 '15 at 07:29 AM

avatar image

kamrann
2.1k 83 34 119

avatar image Venros Apr 07 '15 at 05:15 PM

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).

alt text

ty

avatar image kamrann Apr 07 '15 at 07:42 PM

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.

avatar image Venros Apr 07 '15 at 08:15 PM

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.

alt text alt text

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.

alt text

avatar image kamrann Apr 07 '15 at 09:31 PM

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.

avatar image Venros Apr 07 '15 at 10:10 PM

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.

alt text

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: https://answers.unrealengine.com/questions/97898/tarray-of-actor-bp-reference-how-to-set-spawn-them.html

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

avatar image kamrann Apr 07 '15 at 10:26 PM

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.

avatar image Venros Apr 07 '15 at 10:32 PM

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.

alt text

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

avatar image kamrann Apr 07 '15 at 10:46 PM

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.

avatar image Venros Apr 07 '15 at 11:49 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question