Spawned Actor not passing IsValid check

I have a function that spawns an actor. It takes in a TSubclassOf and an MyActorClass*.

Here’s the code:

I call it via another function that’s triggered on input:

I’m checking the validity at the end of SpawnThruster, which succeeds, but I have a timer that checks validity of the thruster every two seconds saying it’s invalid. The actor still spawns on the screen, but I can’t call its functions (Ex: Thruster001->Fire()) without it crashing the editor.

Oddly, when I uncomment the code in the second screenshot and remove the SpawnThruster function, it works perfectly and the spawned actor is valid.

Am I doing something wrong in passing variables to the function? Is there a scope issue? Everything here is declared under protected.

Greetings @rancidponcho !

Welcome to the Unreal Engine community! :medal_sports:

Just so you’re aware, this topic has been moved from the International forum category to the Development - Programming & Scripting - C++ forum category.

When posting, please review the categories to ensure your topic is posted in the most relevant space.

Wishing you the best of luck in a resolution,

Your Friendly Neighborhood Moderator :smiley:

1 Like

It would be really helpful if you posted your code as text in code blocks, rather than as a screenshot. Screenshots get resized down and make it hard to read.

OK, so, you’re… not doing what you think you’re doing here, I think. It looks like you’re trying to use the second parameter of SpawnThruster() as an out variable, but there’s no reason at all to do that… and to do that, you’d have to pass it by reference, using AKThrusterBase*& (or maybe it’s &* i almost never use * and & together, so i can’t remember which way they are supposed to go when you use both)

You probably just want to return AKThrusterBase* from SpawnThruster, eliminate the second parameter, and at the end of SpawnThruster return the spawned thruster.

As well, there’s probably no reason to use GetActorOfClass – the Thruster that you probably want to operate on is the one held in your Thruster variable.

2 Likes