如何从对象本身关闭UIFrame窗口? (行为差异GMS的1.x到GMS 2.X)(How to

2019-10-21 15:52发布

在GMS2.x,关闭UIFrame窗口带有如下所示的代码将导致DM崩溃(在按下关闭按钮。)

然而,同样的代码工作正常与大湄公河次区域1.x的

有没有办法来解决大湄公河次区域2.X这个问题呢?

class UIWindowCloseTest : UIFrame {

    void CloseSelf( object self ) self.GetFrameWindow().WindowClose(0);

    UIWindowCloseTest( object self ) {
        TagGroup tgDialog = DLGCreateDialog( "window close test" );
        tgDialog.DLGAddElement( DLGCreatePushButton( "Close", "CloseSelf" ));
        self.super.init(tgDialog);
        self.Display( "test" );
        result( self.ScriptObjectGetID().Hex() + " constructed\n" );
    };

    ~UIWindowCloseTest( object self ) \
        result( self.ScriptObjectGetID().Hex() + " destructed\n\n" );
};

alloc(UIWindowCloseTest);

Answer 1:

这是这个问题的GMS 3.X的扩展:

从根本上,在下面的答案是GMS 3以及正确的,但只是因为它的3.2版本(也许GMS 3.1.2以及)。

GMS 3的早期版本中,如KEVIVI在答案的评论指出了一个错误。

然而,有一个变通的解决了这个,这是稍微说明一下:

Class myDLG : UIframe
{
    myDLG(object self)  result("\n Create DLG")
    ~myDLG(object self) result("\n Kill DLG")

    void DeferredClose( object self )
    {
        TagGroup tgs = GetPersistentTagGroup()
        number scriptID
        if ( tgs.TagGroupGetTagAsLong( "DummyTag_CloseWindow_ID", scriptID ) )
        {
            object obj = GetScriptObjectFromID( scriptID )
            if ( obj.ScriptObjectIsValid() )
            {
                obj.GetFrameWindow().WindowClose(0)
                return
            }
        }
        Debug( "\n Sorry, but could not close dialog." )
    }

    void CloseButtonAction( object self )    
    {
        // Normally, it would be save to use "self.close()" here,
        // but due to a bug, this is currenlty not possible in GMS 3.1
        // The alternative method of getting the window of the UIframe object
        // and closing it, is okay, but it must not be called directly here,
        // or it will crash DM.
        // As a work-around, one can store the object ID and have a separate
        // thread pick it up, get the object, and close the object's window.
        // This is, what we are doing below.


        // Write ScriptID into tags 
        TagGroup tgs = GetPersistentTagGroup()
        tgs.TagGroupSetTagAsLong( "DummyTag_CloseWindow_ID", self.ScriptObjectGetID() ) 

        // Launch separate thread just to close... (0.1 sec delay for safety)
        AddMainThreadSingleTask( self, "DeferredClose", 0.1 )
    }

    TagGroup CreateDLG(object self)
    {
        TagGroup DLGtg,DLGtgItems
        DLGtg=DLGCreateDialog("my Dialog",DLGtgItems)
        DLGtgItems.DLGAddElement(DLGCreatePushButton("Close","CloseButtonAction"))
        return DLGtg
    }
}

{
    object dialog=Alloc(myDLG)
    dialog.Init( dialog.CreateDLG() )
    dialog.display("")
}

所以:

  • 对于GMS 3.2和更高版本:使用self.close();

  • 对于GMS 3.0和3.1(有错误):使用变通方法。

  • 对于GMS 2.X:使用self.close();

  • 大湄公河次区域1.x的:使用self.GetFrameWindow().WindowClose(0);



Answer 2:

是的,在大湄公河次区域2.X你必须使用

self.close();

代替

self.GetFrameWindow().WindowClose(0);



文章来源: How to close a UIFrame window from the object itself? (Behavioral difference GMS 1.x to GMS 2.x)
标签: dm-script