To create an object (of some class) in a listener

◇◆丶佛笑我妖孽 提交于 2020-01-25 19:19:42

问题


I'm creating a script and have troubles.

Is it possible to create an object (of some class) from within a listener? I tried it but I get an error: ``class not found''.

I want to do something like:

class ONE {
    class_ONE_code
}

class TWO {
    object o = alloc(ONE)
}

I need this to create a new listener when I execute another listener.


回答1:


What you wish to do is certainly possible. Most likely you have a syntax error in your code. For example, your implementation of class TWO is invalid since a member variable like "o" cannot be initialized in the member declaration section of the class code. This can only be done within a class method, as illustrated in the example code below.

class One
{
    void DoClassOneAction(Object self)
    {
        OKDialog("Class One action executed.");
    }
}

class Two
{
    Object oneInstance;

    void DoClassTwoAction(Object self)
    {
        if (!oneInstance.ScriptObjectIsValid())
            oneInstance = Alloc(One);
        oneInstance.DoClassOneAction();
    }
}

void main()
{
    Object twoInstance = Alloc(Two);
    twoInstance.DoClassTwoAction();
}

main();

Note that the coding requirements for DM script classes differ somewhat from those of other languages that support objects. You may want to review details in the Scripting > Objects section of the DM on-line help (accessed via Help > Search… menu item).



来源:https://stackoverflow.com/questions/32014758/to-create-an-object-of-some-class-in-a-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!