I currently have an issue in UE5 in which my casts don’t work anymore. Upfront I had corrupted my project and recovered it bit by bit and it, so I thought, should work completly fine again, I also reinstalled UE5 in the meantime.
Coming straight to the point, I can’t cast anymore. In any shape or form however I do it, I get a None reference. I was pretty sure I knew how casting works but I am not sure anymore… Maybe theres a weird setting/option to tune?
My easiest example would be trying to cast from my PC to my Rifle to call an event called InitialisierungPistol (sorry it’s German). If I can solve this I am fairly certain I can fix the other casts as well. Anyway this is the things I tried already:
Weapon Reference is a variable o type Actor reference.
Funny enough, as soon as the cast has that blue note alert attached the exec stops right after (I have a create Widget following, and that only shows up with my first example)
This would be my Target Event in BP_Rifle Actor
Up front, thank you so so much for whoever is taking the time to help me.
Happy developing!
Hey there @Shykaro! Welcome to the community! Looks like you’re receiving the warning you get when you try to cast an object of the same type to itself. I’d like to ask a couple questions to clarify things.
It looks like you’re using this from an object called item. Is that a child BP of BP_Rifle or is it a BP_Rifle itself?
Where are you casting this from and what is it’s relation?
Lastly I see you have a direct reference there at the end, if you have that you don’t even have to cast. Not sure if you got that reference from the hierarchy or set it directly, but here’s how you can use that direct reference to call functions on the object without the need to cast.
As I was struggling with this phenomenom for the whole night, posting it here was my last resort. I did however seem to have figured it out this morning
I noticed with some tries that I can actually cast just fine to variables and there seems to be a problem with the Event call itself. Giving back an Error
“Blueprint Runtime Error: “Accessed None trying to read property CallFunc_Array_Get_Item”. Node: Initialisierung Pistol Graph: EventGraph Function: Execute Ubergraph BP First Person Character Blueprint: BP_FirstPersonCharacter”
Since I build everything ontop of the FirstPerson template i modified the rifle BP to my liking.
Seems like the whole error was resolved with adding the actual rifle BP to the level map, which i hadn’t done before.
Can someone explain to me why that has to be done? - does everything need to be added to the level to be loaded initially? → why was I able to cast to variables then…? Dont know how that works.
I mean the model itself doesn’t even exist physically anymore when simulating…
I did however adjust to your way @SupportiveEntity of casting/calling external events, since i tried alot of different stuff as seen above and didnt know what’s right. Thank you for clarifying!
Item was/is indeed BP_Rifle itself.
Tried casting from BP_FirstPersonCharacter (Child of Character) to BP_Rifle (Child of Actor).
Basically if you do a cast to the class itself, it’ll be checking to see if it can find the class itself, not the actor. That said if the class itself doesn’t exist in reference, it won’t do anything. Best practices is to avoid casting if you don’t have to. Since this is for your pistols initialization it’s not a big deal, but for something happening frequently it could get rather taxing. Conversely if you cast to itself or a child class it’ll still extend the same class as it’s parent, but also it’s own. Seems like you’ve got it now, please don’t hesitate to let me know if you have any more questions!
You need an Actor Object Reference to cast “From”.
Nothing → Cast to What Ever… Will always fail. Essentially the actor must exist in the world. Either placed or spawned.
Get all actors of class searches the game world for instances of said class. Where you call “Get all actors of class” matters.
If on the Autonomous player (controlled local client) you’re searching your simulation of the world. If on the server you’re searching the servers simulation of the world.
For example if your locally owned character spawns an actor and the server runs get all actors of class for it…it won’t be found. If the server spawns an actor and it’s not replicated, you won’t get a result locally.