Download

Mysterious cast is mysterious

Step 1) Open this video on YT: https://www.youtube.com/watch?v=HQoRXhS7vlU
Step 2) Look at the following image:

1fffcfe7b8df4dba4badf9699376720497281045.png

Sorry for the spam but I really can’t understand this warning (WeaponInSlot class inherit from ItemClass class)! :V

it can’t work, because you trying cast item class to weapon_in_slot. its like you want to get random static mesh from level and cast it to game mode… wheres mystery?

edit: you probably dont understand what cast node is. im not sure too but as far as i know its something like “selection” node…
you can have for example actor material. then you create child classes like material_steel or material_iron or material_gold etc…

when you come to forge, you will put into forge material input… because its better than create 100 inputs for different materials…
and now, when you want to melt these materials, you cast it to iron or gold or steel… and run some logic… so it means you must have material input to cast node, and not for example player controller input…

To be honest I know perfectly what a cast is. When you have a reference to an object you have access to any variable and method of that object. If you create a class that inherit from an other class you can save the istance of child class as reference of the parent one. Obviously you can’t access to child class data unless you cast it as child. Ex: I create the class “fruit” and then from that class I create “apple”, “banana”, “pear” and “Batman” (that isn’t a fruit but I can so I will). I can use the “get all actor of class” node (input fruit class) to get an array with every reference at fruit objects I’ve in the scene. Now I can’t directly access to “BatCave” variable because the code can’t be sure you are reading Batman or a banana so i need to cast my fruits to Batman. If the cast work I’ve access to the bat cave and call “FightJoker” method, if the cast fail I can at least peel it.

The mistery is the warning: I’ve the item class and the Weapon class that is inherit from it, OBVIOUSLY I can cast the item class ref as weapon and the cast work (as you can see from the big gray arrow in the image) even if the warning tell me it should fail, so, why it warn me?

hmmm. so you created actor “Item class” and then created child bp “weapon in slot” from it? i tested it now, i casted hero_base to hero_sniper in my project and got no error… soo i dont have more ideas :slight_smile:
btw i stopped using breakpoints because i played with debugging a lot and i found breakpoints themselves are buggy… im using only print strings

Yep, forgot to specify I don’t use default classes of the engine :smiley: I use both breakpoints and printstring, sometime following the exec flow reading variables can help a lot :smiley:

You’re casting a self reference to a child class. I doubt it but I’m not entirely sure if this should work. But it’s definitely not good software design.

This is actually what polymorphism is made for. In your ItemClass blueprint your DestroyItem should simply call DestroyActor. And your child class WeaponInSlot should override the function DestroyItem and call DestroyWISActor instead.

You are brilliantly right, I can totally override!!! Thank you!

BTW still wondering the reason of the warning because the cast works!