pickleplayer Posted December 10, 2015 Share Posted December 10, 2015 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()endapparently, 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? Link to comment Share on other sites More sharing options...
Arkathorn Posted December 10, 2015 Share Posted December 10, 2015 You could add an 'OnRemoveEntity' function to the owner's definition, that sets a variable which can later be accessed by the component. Link to comment Share on other sites More sharing options...
pickleplayer Posted December 10, 2015 Author Share Posted December 10, 2015 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 Link to comment Share on other sites More sharing options...
Developer bizziboi Posted December 10, 2015 Developer Share Posted December 10, 2015 @pickleplayer, For reference, "if owner:IsValid()" would have been the proper way to test if the owner was still alive. Link to comment Share on other sites More sharing options...
pickleplayer Posted December 11, 2015 Author Share Posted December 11, 2015 @pickleplayer, For reference, "if owner:IsValid()" would have been the proper way to test if the owner was still alive.Oh, also good to know. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.