Lua Roblox API: 等价计算操作和Roblox API对象:为什么一个表达式输出false,另一个输出true?
2021-1-22 17:19:4
收藏:0
阅读:159
评论:1
背景:
下面有两个等价比较表达式,它们很相似。在四个参数的左右两边,输出框的值都相同,但是这两个表达式(我自己发现的)不同点是正在比较的对象的类型(作为左侧和右侧参数)。 因此,一个表达式相对于等价操作产生布尔值 "true",而第二个表达式相对于等价操作产生布尔值 "false",尽管在两个表达式中,单个输出值(在输出框中)相同,并且类型也相同。( 即,我尝试为每个表达式产生真实值,但只有一个输出为真实值)。
问题:
为什么一个表达式(具有 "string" 类型的对象)会产生 "true",而另一个表达式(具有 "Instance" 类型的对象)会产生 "false"?
代码:
--在下面的两行代码中考虑这两个函数的上下文:
box.Touched:Connect(onTouchedDebounced)
function onTouchedObject(otherObjectPart)
--考虑以下代码:
--在以下3行中声明要比较的对象后:
local otherObjectPartsParent=otherObjectPart.Parent;
local resultFromGetPlayerFromCharacterFunction = game.Players:GetPlayerFromCharacter(otherObjectPartsParent);
--在这里,我想说:
--ActiveUserPlayerName == ActiveUserPlayerName 为真
--其中: (#1):Object1=“ActiveUserPlayerName” ;
--其中: (#2):Object2=“ActiveUserPlayerName” ;
--其中: (#3):左侧参数=类型为Instance的ActiveUserPlayerName
--其中: (#4):右侧参数=类型为Instance的ActiveUserPlayerName
print ("resultsFromGetPlayerFromCharacterFunction: ", resultFromGetPlayerFromCharacterFunction); --输出: ActiveUserPlayerName
print ("otherObjectPartsParent.Name: ", otherObjectPartsParent); --输出: ActiveUserPlayerName
print ("typeof(resultsFromGetPlayerFromCharacterFunction): ", typeof(resultFromGetPlayerFromCharacterFunction)); --输出: Instance
print ("typeof(otherObjectPartsParent): ", typeof(otherObjectPartsParent)); --输出: Instance
print ("resultFromGetPlayerFromCharacterFunction == otherObjectPartsParent结果是: ", resultFromGetPlayerFromCharacterFunction == otherObjectPartsParent); --输出: False
--在这里,我想说:
--ActiveUserPlayerName == ActiveUserPlayerName 为真
--其中: (#1):Object1=“ActiveUserPlayerName” ;
--其中: (#2):Object2=“ActiveUserPlayerName” ;
--其中: (#3):左侧参数=类型为string的ActiveUserPlayerName
--其中: (#4):右侧参数=类型为string的ActiveUserPlayerName
print ("resultsFromGetPlayerFromCharacterFunction.DisplayName: ", resultFromGetPlayerFromCharacterFunction.DisplayName); --输出: ActiveUserPlayerName
print ("otherObjectPartsParent.Name: ", otherObjectPartsParent.Name); --输出: ActiveUserPlayerName
print ("typeof(resultsFromGetPlayerFromCharacterFunction.DsplayName): ", typeof(resultFromGetPlayerFromCharacterFunction.DsplayName)); --输出: string(*已更正)
print ("typeof(otherObjectPartsParent.Name): ", typeof(otherObjectPartsParent.Name)); --输出: string(*已更正)
print ("resultFromGetPlayerFromCharacterFunction.DisplayName == otherObjectPartsParent.Name结果是: ", resultFromGetPlayerFromCharacterFunction.DisplayName == otherObjectPartsParent.Name); --输出: True
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你的变量名让阅读起来非常困难。但是,通过将
otherObjectPartsParent重命名为character,将resultFromGetPlayerFromCharacterFunction重命名为player,就可以更清楚地了解正在发生的事情。local character = otherObjectPart.Parent; local player = game.Players:GetPlayerFromCharacter(character); print ("player : ", player); -- 输出: ActiveUserPlayerName(Player 对象的 tostring'd 版本) print ("character.Name: ", character.Name); -- 输出: ActiveUserPlayerName print ("typeof(player): ", typeof(player)); -- 输出: Instance(Player 类的实例) print ("typeof(character): ", typeof(character)); -- 输出: Instance(Model 类的实例) print ("player == character is: ", player == character); -- 输出: False(指针比较,它们是两个不同的对象) print ("player.DisplayName: ", player.DisplayName); -- 输出: ActiveUserPlayerName print ("character.Name: ", character.Name); -- 输出: ActiveUserPlayerName print ("typeof(player.DisplayName): ", typeof(player.DisplayName)); -- 输出: Instance(这看起来是错误的,应该是字符串) print ("typeof(character.Name): ", typeof(character.Name)); -- 输出: Instance(这也看起来是错误的,应该是字符串) print ("player.DisplayName == character.Name is: ", player.DisplayName == character.Name); -- 输出: True(字符串比较,这应该是正确的)最终,你正在比较两个不同的对象。当有人加入游戏时,他们的 Player 被添加到 Players 服务中,他们的 Character 被添加到 Workspace 中。
Player 和 Character 有相同的名字,但一个代表他们在 Roblox 中的身份,另一个是他们在游戏中的物理表现。
在进行比较时,类型的种类很重要。Lua 原始值,如 bool、int、string 和 nil,将通过值进行比较。表和 userdata 对象将通过指针引用进行比较。因此,在处理 Roblox lua 时,需要注意正在处理的对象类型。Instance 在幕后是 userdata,因此即使两个对象具有相同的名称,它们也不总是相等的。