wurstscript / WurstScript

Programming language and toolkit to create Warcraft III Maps
https://wurstlang.org
Apache License 2.0
226 stars 28 forks source link

Is there a way to determine if a class instance has been destroyed? #1047

Closed tester1101 closed 2 years ago

tester1101 commented 2 years ago

Is there a way to write code like this?

class TestClass

function test1()
    let a = new TestClass()
    destroy a
    if isDestroyed(a)
        print("a is destroyed")
        doSomething1()
    else
        print("a is alive")
        doSomething2()
Frotty commented 2 years ago

There is no reliable way to do this, because object ids are recycled. Thus your references to a destroyed object may turn into a valid reference to a new object which has been created afterwards. Hence checking for "destroyedness" is an anti-pattern. What you should do is cleanup all references of the object when you destroy it, so that there are no references to the destroyed object. In many cases you can avoid having to clear a bunch of references by using the observer pattern.

tester1101 commented 2 years ago

I understand. I need to study the observer pattern. Thanks for the reply.