AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77295190
Accepted
ChenZX
ChenZX
Asked: 2023-10-15 11:13:05 +0800 CST2023-10-15 11:13:05 +0800 CST 2023-10-15 11:13:05 +0800 CST

设置实场输入小部件的阈值

  • 772

大家好:

我想在实场小部件中设置输入值的阈值。下面的代码将创建一个实场小部件,输入值的阈值在 0 到 0.05 之间。

如果您在小部件内输入 0.06,然后单击 GUI 之外的某个位置,它会警告您这不是正确的值,并将该值重置为最后一个值。

但如果你输入0.05,它不会警告你。然后如果您输入任何其他值,它会显示“错误的配置文件”。

我仔细检查了逻辑,没有发现任何错误的代码。有人可以帮我调试吗?多谢。

问候

陈正旭

Number get_tag_as_number(TagGroup tg, String tag_path, String tag_type, Number low_limit, Number high_limit)
{
    Number tag_value, tag_is_valid
    if (tag_type == "Boolean") tag_is_valid = tg.TagGroupGetTagAsBoolean(tag_path, tag_value)
    if (tag_type == "Number") tag_is_valid = tg.TagGroupGetTagAsNumber(tag_path, tag_value)
    if (!tag_is_valid)
    {
        Result("ERROR: Can not get tag value of " + tag_path + "." + "\n")
        ShowAlert("Can not get tag value of " + tag_path + ".", 0)
        Exit(0)
    }

    if (tag_value < low_limit || tag_value > high_limit)
    {
        Result("ERROR: Wrong config file, " + tag_path + "." + "\n")
        ShowAlert("Wrong config file, " + tag_path + ".", 0)
        Exit(0)
    }
    return tag_value
}

Class testUI : UIFrame
{
    TagGroup ui_config_tg
    Void tg_init(Object self)
    {
        ui_config_tg = NewTagGroup()
        ui_config_tg.TagGroupSetTagAsFloat("display_font:init_value", 0.015)
    }
    TagGroup create_realfield_widget(Object self)
    {
        Number low_limit = 0
        Number high_limit = 0.05
        Number init_value = get_tag_as_number(ui_config_tg, "display_font:init_value", "Number", low_limit, high_limit)
        TagGroup realfield_widget
        realfield_widget = DLGCreateRealField(init_value, 16, 3, "action")
        realfield_widget.DLGIdentifier("#realfield_widget")

        return realfield_widget
    }
    Void action(Object self, TagGroup item_tag)
    {
        Number low_limit = 0
        Number high_limit = 0.05
        Number curr_value = item_tag.DLGGetValue()
        Number init_value
        init_value = get_tag_as_number(ui_config_tg, "display_font:init_value", "Number", low_limit, high_limit)



        if (curr_value < low_limit || curr_value > high_limit)
        {
            OKDialog("Font size must between " + low_limit + " to " + high_limit)
            self.LookUpElement("#realfield_widget").DLGValue(init_value)
            item_tag.DLGValue(init_value)
            Exit(0)
        }
        ui_config_tg.TagGroupSetTagAsFloat("display_font:init_value", curr_value)
        Result( curr_value)
    }
    
    TagGroup create_dialog(Object self)
    {
        self.tg_init()
        TagGroup realfield_widget = self.create_realfield_widget()
                
        TagGroup tabs = DLGCreateTabList()
        tabs.DLGAddElement(realfield_widget)
        TagGroup dialog = DLGCreateDialog("test")
        dialog.DLGAddElement(tabs)
        return dialog
    }
    
    
    testUI(Object self)
    {

        self.init(self.create_dialog())
        self.Display("test")

    }
}

Alloc(testUI)
dm-script
  • 1 1 个回答
  • 11 Views

1 个回答

  • Voted
  1. Best Answer
    BmyGuest
    2023-10-15T18:01:56+08:002023-10-15T18:01:56+08:00

    逻辑很好,但是您遇到了舍入问题,这些问题在具有自动数字和字符串转换的脚本语言上特别难以发现。

    • DM-Scriptnumber在内部被处理为double(至少在 GMS 3 中)
    • 当您将值作为浮点数写入标签时,您会失去精度。

    坚持下去...asNumber可以避免这些问题。仅在重要的情况下才需要指定类型,例如流式传输到二进制流。

    请参阅以下示例

    number tag_value = 0.05
    number low_limit = 0
    number high_limit = 0.05
    
    clearresults()
    Result("\n Limits are (double):")
    result("\n: low limit:  "+low_limit)
    result("\n: high limit: "+high_limit)
    
    Result("\n Tag value is (double):")
    result("\n tag value:  "+Format(tag_value,"%20.12f"))
    Result("\n (tag_value - low_limit) : "+(tag_value - low_limit))
    Result("\n (tag_value - high_limit): "+(tag_value - high_limit))
    
    taggroup tg = newTagGroup()
    
    tg.TagGroupSetTagAsNumber("Test",tag_value)
    tg.TagGroupGetTagAsNumber("Test",tag_value)
    
    Result("\n ~~~~~ Now with Tag read & write as NUMBER (internally this is DOUBLE)")
    result("\n tag value:  "+Format(tag_value,"%20.12f"))
    Result("\n (tag_value - low_limit) : "+(tag_value - low_limit))
    Result("\n (tag_value - high_limit): "+(tag_value - high_limit))
    
    tg.TagGroupSetTagAsDouble("Test",tag_value)
    tg.TagGroupGetTagAsDouble("Test",tag_value)
    
    Result("\n ~~~~~ Now with Tag read & write as DOUBLE")
    result("\n tag value:  "+Format(tag_value,"%20.12f"))
    Result("\n (tag_value - low_limit) : "+(tag_value - low_limit))
    Result("\n (tag_value - high_limit): "+(tag_value - high_limit))
    
    tg.TagGroupSetTagAsFloat("Test",tag_value)
    tg.TagGroupGetTagAsFloat("Test",tag_value)
    
    Result("\n ~~~~~ Now with Tag read & write as FLOAT")
    result("\n tag value:  "+Format(tag_value,"%20.12f"))
    Result("\n (tag_value - low_limit) : "+(tag_value - low_limit))
    Result("\n (tag_value - high_limit): "+(tag_value - high_limit))
    

    或者,您可以通过不完全检查“0”,而是检查解决精度问题的小 epsilon 值来修复脚本:

    [...]
        number eps = 1e-9
        if ((tag_value - low_limit)< eps || (tag_value - high_limit) > eps)
        {
            Result("ERROR: Wrong config file, " + tag_path + "." + "\n")
            ShowAlert("Wrong config file, " + tag_path + ".", 0)
            Exit(0)
        }
    
    • 1

相关问题

  • 是否可以使用一个类来定义 UI 中的一个选项卡?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve