"Accessed none trying to read property" error on SpawnActor

Hello,

I’m facing the classic error of null value in a specific variable, and although I know what it means, I don’t understand the reason.Basically i have a widget (health bar) binded to the function GetPercent :https://i.gyazo.com/9a6686fd45ba672f2c2f1859a14fa0b0.png

(I get the current and max health value using an interface).

The problem is, this code DOES work if i drag the character to the scene, but if the same persona is spawned using SpawnActorFromClass (or SpawnAIFromClass) then the widget doesnt work i have the Accessed none trying to read property

Why?

Check your begin play event. You might be calling for things too early.

I dont use the begin play anywhere but the Base blueprint, i dont see nothing wrong or called too early (i’ve added the delay just to try).

Begin Play is executed On All proxies of the class.

The client copy (simulated proxies) do not get controller copies. So the controller cast will fail and the reference will be null.

I’m a little confused by your answer. I didn’t know that under certain circumstances (like the one you mentioned) proxies might not receive a copy of the controller; however, I don’t see how that affects my problem (from my complete lack of knowledge of corse).

AFAIK, if a cast to node fails, it doesn’t interrupt the code flow and it continues executing, it may play the Cast Failed, but in this case, im not using it. In fact, the characters created with the Spawn AI from Class have a normal behaviour, the cast doesnt fail and the As AIC Enemy Base is working in other parts of the code when used as “get”

AI is server controlled. Unless you are the host (listen Server, Single player), AI on your screen are simulated proxies. Controller copies/references are not replicated. When they spawn on your client they will execute begin play. The cast will fail. If they have to reference As AI Enemy Base anywhere, it will fail and shoot an access none error.

Begin Play → Branch (Is Server) → execute logic

My game is single player indeed. I didnt try to replicate variables, components or events

In the end, I fixed it myself. From the start, I suspected the code was working, but either I wasn’t calling it when necessary (because I had the initiations in the Begin Play event) or I wasn’t giving it enough time to run.

In the end, I fixed it by changing the order: first, creating the widget, then binding events, and finally, moving the AIcontroller cast from the Begin Play event to the (new) Possessed event.