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"

Casting from c++ to blueprint

When I try to cast my c++ actor class to it's blueprint class, it fails. I'm not sure why since the blueprint class was created from the c++ class.

The createItem function spawns a new Aitem and returns its pointer. In blueprints, I then attempt to cast it but it always fails : imagelike this.

 Aitem* ADungeonLevel::createItem(FName iname)
 {
 Aitem *newitem = NULL;
 FVector location(500.f, 500.f, 500.f);
 FRotator rotation(0.f, 0.f, 0.f);
 FActorSpawnParameters spawninfo;
 spawninfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

 for (int i = 0; i < ItemsPtr->AllItems.Max(); i++)
 {
     for (int n = 0; n < ItemsPtr->AllItems[i].Items.Max(); n++)
     {
         if (ItemsPtr->AllItems[i].Items[n].EditorName == iname)
         {
             newitem = GetWorld()->SpawnActor<Aitem>(location, rotation, spawninfo);
             
             // set item data
             newitem->setItemData(ItemsPtr->AllItems[i].Items[n]);

             break;
         }
     }

     if (newitem) break;
 }
 
 if(!newitem) UE_LOG(LogItem, Error, TEXT("Error creating new item") );

 return newitem;
 }
Product Version: UE 4.18
Tags:
more ▼

asked Feb 04 '18 at 06:11 AM in C++ Programming

avatar image

postal83
8 1 2 4

avatar image erik321123 Feb 04 '18 at 12:53 PM

So you try to cast a AItem to AItem or what? Your question isn't really clear, perhaps you can add a BP screenshot

avatar image postal83 Feb 04 '18 at 03:20 PM

Thanks I tried to clarify it a bit. I have a blueprint class that is created from my actor c++ class. When I try to cast my c++ class to the blueprint class it fails?

avatar image erik321123 Feb 04 '18 at 03:25 PM

is your spawn, and function successful? Is it really the cast that fails or the getting of data to cast?

avatar image erik321123 Feb 04 '18 at 03:26 PM

Also is AItem inherited from BP_Itembase?

avatar image postal83 Feb 04 '18 at 03:32 PM

BP_Itembase was created by "create blueprint class based on item" in the content browser by right clicking on the CPP class.

The spawn is successful and I can manipulate the Aitem object before I try to cast it and can even get it to render its sprite, but for whatever reason it won't cast to a blueprint class that was derived from the c++ class.

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

1 answer: sort voted first

I think I know what is going wrong, you are trying to 'down'cast. A super class is casted to a child class. That isn't possible, you can only cast 'up', to a super class.

more ▼

answered Feb 04 '18 at 03:41 PM

avatar image

erik321123
413 17 15 31

avatar image postal83 Feb 04 '18 at 03:48 PM

Ah that's a bummer but makes sense now. Thanks for the answer. Back to the drawing board...

(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