#Casting to PlayerController fails inside a function library

1 messages · Page 1 of 1 (latest)

inner pendant
#

I have functions like this in multiple blueprints where I need various references cached - player controller, game instance etc, and they work fine, but I got tired of constantly copy pasting them so I figured a function library could help.

I made 2 functions for fetching the game instance and an AI base and they're working. All they do is check if the value is null, and if it is do a cast, otherwise return it as is. Lazy loading I guess. However when I try to do the same for the player controller, the cast always fails, so it keeps returning null. Trying to understand why the others work but the PC fails to cast inside the function library.

  • 1st image is the old, local function which casts fine, 2nd is from the library, which fails;
  • This Caller is just a reference to Self, which I'm not sure is even needed, if they work they work without it as well, just figured it doesn't hurt to have it;
ivory kindle
#

This is my function lib version , I call it once on begin play and save return value

inner pendant
#

yeah I just added some more bells and whistles so it stores it automatically, without worrying about the return value, but I don't understand why it fails

ivory kindle
#

you might need a small delay if you are calling it before the PC loads

inner pendant
#

ohhh you're right, haven't thought about race conditions

ivory kindle
#

Like, if you change map, the PC might not be loaded

inner pendant
#

I was thinking it might be something else or a bug with 5.4, cause earlier I opened another project and several files were corrupted, even if last time it worked fine, had to rename some, recreate the game mode, game instance etc

#

but yeah a race condition sounds reasonable

inner pendant
#

so it's not an execution order issue, just in initialization it doesn't like the library function, wants the local one instead

#

and then this is for hud, but it's pretty much the same issue, it's called on init, it works, it's called when I press play, it works, the value is never null

#

however if I use the variable it says is null, if I use the one returned by the function for both it's fine