面临这样的问题:必须使用值来自一个模块脚本中的其他模块脚本。两个模块脚本都包含具有浮点值、布尔值和字符串值的表。从理论上讲,这是怎么做到的?这可能吗?如果没有,有什么替代方案?我在论坛上读到这是可以做到的,但具体是如何做到的,哪里都没有解释。例如一个模块脚本:
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = {"Fruit", "Meat"}
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = {"Meat"}
}
}
return characters
两个模块脚本:
local food = {
["Fruit"] = {
["Saturation"] = 20,
["Price"] = 2
},
["Meat"] = {
["Saturation"] = 50,
["Price"] = 7
}
}
return food
ModuleScripts的文档告诉您如何在其他源容器中使用它们:require
函数。
因此,假设您已将ModuleScripts分别命名为Characters
和Food
,并假设它们是文件夹中的兄弟,则可以使用require
函数,如下所示:
local Food = require(script.Parent.Food)
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = { Food.Fruit, Food.Meat }
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = { Food.Meat }
}
}
return characters