Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Destroyed entity is non-nil

edited April 9 in Bugs Posts: 1,795

Code:


function setup() craft.scene.main = craft.scene() parameter.action("make foo", function() foo = craft.scene.main:entity() fooVolume = foo:add(craft.volume) print("** made foo and volume") end) parameter.action("destroy foo", function() foo:destroy() print("** called destroy()") end) parameter.action("print foo", function() print("foo: ", foo) if foo then print("foo volume: ", fooVolume) print("foo triggers if-then") end end) end function draw() end

After destroying an entity, it still prints out as if it exists, and it still passes a nil check such as if entity then.

I think this is likely counter-intuitive behavior, and if it can’t or shouldn’t be corrected I think it should be in the documentation.

@John if you think it should remain this way I will try to make a documentation pull request with a revision that mentions this.

Comments

  • dave1707dave1707 Mod
    Posts: 9,977

    @UberGoober I not sure where I read this, but after you use destroy, you’re also supposed to set it to nil. At least I’ve always did that to make sure it didn’t exits anymore unless I was going to reuse it right away.

  • Posts: 2,689
    @UberGoober - I tried using collect.garbage() to see if that would restore memory, but no joy.
  • Posts: 1,795

    @dave1707 problem is there’s no way to tell if setting the variable to nil has actually gotten rid of it, or has just removed the reference but left the entity there.

  • dave1707dave1707 Mod
    edited April 9 Posts: 9,977

    @UberGoober If you set foo to something then set foo to nil, it should be gone and anything else that was assigned to foo.

  • Posts: 1,795

    @dave1707 it should be but there’s no real way to tell. With physics bodies you can check against #physics.bodies to verify if a body has actually been destroyed. But there’s no such system with entities afaik.

  • dave1707dave1707 Mod
    Posts: 9,977

    @UberGoober Anything you set to nil should be gone. That's the purpose of nil. Maybe not instantly, but by the next cycle because some routine has to clear it.

  • Posts: 1,795

    @dave1707 sure, and we also know that nil doesn’t get rid of 2D physics bodies unless they’ve been destroyed first.

    It’s reasonable to assume that entities act the same way, but the difference is that we can verify when a 2D physics body has been permanently removed, and with entities I don’t think there’s any way to.

    So we know for sure of at least one situation in which nil does not actually get rid of anything, and we don’t know verifiably if entities work exactly like that or not.

  • dave1707dave1707 Mod
    Posts: 9,977

    @UberGoober Some things are just a multi step process. The physics destroy gets rid of the physics reference and the nil gets rid of the variable reference. I don’t know the true workings of Codea. Can I say for sure everything works the way it’s supposed to, not really. So I do the destroy and then the nil.

  • Posts: 1,795

    @dave1707 the reason I ask is not academic, by the way: I am working on a project that often destroys and re-creates entities, and I am getting crashes that I suspect may be due to phantom entities that have been nilled out and destroyed but still exist in some way.

  • dave1707dave1707 Mod
    Posts: 9,977

    @UberGoober If you’re destroying and creating entities at a fast pace, maybe Codea doesn’t have enough time to properly do everything it needs to do. Does Codea do everything in one draw cycle or does it need multiple cycles, I don’t know. I’m not sure if collectgarbage has any affect or not.

Sign In or Register to comment.