Lua Roblox API: 等价计算操作和Roblox API对象:为什么一个表达式输出false,另一个输出true?

背景:

下面有两个等价比较表达式,它们很相似。在四个参数的左右两边,输出框的值都相同,但是这两个表达式(我自己发现的)不同点是正在比较的对象的类型(作为左侧和右侧参数)。 因此,一个表达式相对于等价操作产生布尔值 "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
点赞
用户2860267
用户2860267

你的变量名让阅读起来非常困难。但是,通过将 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,因此即使两个对象具有相同的名称,它们也不总是相等的。

2021-01-18 19:58:49