如何获取尚未声明但即将声明的变量? [英] How to get variables that not declared yet but will declared soon?

查看:130
本文介绍了如何获取尚未声明但即将声明的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取尚未声明的变量?

How can I get variables that not declared yet?

以下是简单的示例:

a = b
b = 123

我想从这两行中得到的是a<< 123.但是,它不起作用.

What I want from these 2 lines is a << 123. But obv it doesn't work.

我知道获得答案a = 123的简单方法是剪切第一行并将其粘贴到低于第二行.

I know the easy way to get the answer a = 123 is cut 1st line and paste it to lower than 2nd line.

但是我遇到了一些问题.我需要像"WillDeclaredVar()"这样的函数,可以像这样使用:

But I'm in some problem. I need some function like 'WillDeclaredVar()' that I can use in like this:

a = WillDeclaredVar(b)
sheepCount = 123
b = sheepCount
print(a)

所以我可以得到答案'123'.

so I can get the answer '123'.

或者有任何内置功能可以让我做类似的事情?

Or there are any built-in functions that will allows me to do similar thing?

===

我认为timrau提供的链接无法说明我的情况.关键是如何获取尚未声明的变量.

I think the link given by timrau is not telling my case. the key point is how to get Variables 'that not declared yet'.

===

添加实际代码:

triggerCount = 0 -- Counting number of 'Trigger' function
local Trigger = function (t)
triggerCount = triggerCount + 1
return Trigger (t)
end


-- following Triggers are same as while statement.
-- following Triggers doing: Add 1 MarineCount until get 64000 MarineCount



Trigger { -- Here the Trigger function. Now triggerCount = 1.
    players = {P1}
    actions = {
        SetDeaths(P1, Add, 1, "Terran Marine")
    },
flag = {preserved},
}
Portal(LoopStart);
-- function Portal(VariableName) returns VariableName = triggerCount. So LoopStart = 1.

Trigger { -- Now triggerCount = 2.
    players = {P1}
    actions = {
        LinkList(LoopEnd, LoopStart);
-- function LinkList(From, To) changes 'From' Trigger's next pointer to the 'To' Trigger.
-- But now the problem happens. Because 'LoopEnd' is not declared yet.
    },
flag = {preserved},
}


Trigger { -- Now triggerCount = 3.
    players = {P1}
    conditions = {
        Deaths(P1, Exactly, 64000, "Terran Marine");
    }
    actions = {
        _LinkList(LoopEnd);
        -- Reset LoopEnd's next pointer(= LoopEscape) if MarineCount hits 64000
    },
flag = {preserved},
}
Portal(LoopEnd); -- LoopEnd = 3.

  1. 更改触发器的顺序将破坏触发器逻辑(while语句).

  1. Changing Order of Triggers will break the Trigger logic(while statement).

我想要的只是易于编码.坦率地说,我不需要解决此问题(获取未声明的var).我可以想像一些避免这种情况的方法.但是,如果我使用这些方式,那么编码工作将非常复杂,编码难度将大大增加.困难使我在最近几个月停止编码.

All i want is get easy to coding. To put in bluntly, I don't need to solve this problem(get undeclared var). I can imagine a few ways to avoid it. But if i using these ways then the coding work will be very complicated and the difficulty of coding will increases greatly. The difficulty made me stop coding in recent months.

推荐答案

要使var1成为var2的引用,请写var1 = ReferenceF or var2(请注意"ReferenceFor"内有空格!)

To make var1 be a reference for var2 write var1 = ReferenceF or var2 (please note a space inside "ReferenceFor"!)

do
   local values, references, reference_flag = {}, {}
   setmetatable(_G, {
      __index = function (_, name)
         if name == 'ReferenceF' then
            reference_flag = true
         elseif reference_flag then
            reference_flag = false
            return {[references] = name}
         elseif references[name] then
            return _G[references[name]]
         else
            return values[name]
         end
      end,
      __newindex = function (_, name, val)
         if type(val) == 'table' and val[references] then
            references[name] = val[references]
         else
            values[name] = val
         end
      end
   })
end

a = ReferenceF or b  -- a is Reference For b
b = ReferenceF or c  -- b is Reference For c
sheepCount = 123
c = sheepCount
print(a, b, c)  --> 123 123 123

这篇关于如何获取尚未声明但即将声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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