如何从对象本身关闭UIFrame窗口? (行为差异GMS 1.x与GMS 2.x) [英] How to close a UIFrame window from the object itself? (Behavioral difference GMS 1.x to GMS 2.x)

查看:105
本文介绍了如何从对象本身关闭UIFrame窗口? (行为差异GMS 1.x与GMS 2.x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GMS2.x中,使用下面显示的代码关闭UIFrame窗口将导致DM崩溃(按 close 按钮.)

In GMS2.x, closing UIFrame window with the code shown below will cause DM to crash (at pressing the close button.)

但是,相同的代码可以在GMS 1.x上正常工作.

However, the same code works fine with GMS 1.x.

在GMS 2.x中是否有解决此问题的方法?

Is there a way to work around this problem in GMS 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);

推荐答案

这是针对GMS 3.X的此问题的扩展:

本质上,下面的答案对GMS 3也是正确的,但仅适用于3.2版(也许是GMS 3.1.2).

This is the extension of this questions for GMS 3.X:

In essense, the answer below is correct for GMS 3 as well, but only since its version 3.2 (Maybe GMS 3.1.2 as well).

KEMSVI在答案的注释中指出,GMS 3的早期版本存在一个错误.

Earlier versions of GMS 3 have a bug as KEVIVI pointed out in the comment to the answer.

但是,对此有一个解决方法,该解决方法略为复杂:

However, there is a work-around solution to this, which is slightly elaborate:

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("")
}


所以:


So:

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

对于GMS 3.0和3.1(带有错误):使用解决方法.

For GMS 3.0 and 3.1 (with the bug): Use the workaround.

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

For GMS 2.x: Use self.close();

对于GMS 1.x:使用self.GetFrameWindow().WindowClose(0);

For GMS 1.x: Use self.GetFrameWindow().WindowClose(0);

这篇关于如何从对象本身关闭UIFrame窗口? (行为差异GMS 1.x与GMS 2.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆