Jump to content

Recommended Posts

So I guess it's a big no-no to try and get the world position of an entity that no longer exists.

 

I have a function in my custom component that runs on update. And this line of code has been giving me trouble:

local x, y, z = owner.Transform:GetWorldPosition()

It runs fine whenever "owner" exists.

except that "owner" can disappear at any time (it's supposed to do that, I promise). But even after the owner is removed, the code still runs anyways, crashing straight to desktop when it can't find the owner's world position.

The actual error printed out by the log is "Stale Component Reference"

 

I've tried a lot of different things to try and get it to ignore the code if the owner isn't there, but even checking for the owner seems to crash the game.

local x, y, z = nilif owner and owner.Transform:GetWorldPosition() then --THIS LINE CAUSES A CRASH FOR THE SAME REASON THE LINE BELOW IT DOES	x, y, z = owner.Transform:GetWorldPosition()end

apparently, just checking for "if owner then" doesn't work because the game still thinks it exists? Or something. I dunno.

 

and "if owner.Transform:GetWorldPosition() then" also crashes the game for "Stale Component Reference"

 

So I don't know how to get this function to check to make sure the owner exist before running that line of code. Does anyone have any ideas?

You could add an 'OnRemoveEntity' function to the owner's definition, that sets a variable which can later be accessed by the component.

Oh awesome, it worked. Thanks!

I didn't even know OnRemoveEntity was a thing, that's pretty useful to know

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...