Lua Roblox API:等价算术运算和 Roblox API 对象:为什么一个表达式产生假而另一个产生真?

Lua Roblox API: Equivilance Arithmetic Operations and Roblox API Objects: Why does one expression yield false and one yield true?

上下文:

下面有两个等价比较表达式。两种表达方式非常相似。所有四个参数的左侧和右侧在输出框中产生相同的值,但表达式之间的差异(我发现)是被比较的对象的类型(作为左侧和右侧参数) .因此,一个表达式就等价运算产生布尔值“真”,第二个表达式就等价运算产生布尔值“假”,尽管在两个表达式中,各个输出值(在输出框)是相同的,并且该表达式中的类型是相同的。 (即,我试图产生一个真值的每个表达式,但只有一个结果为真)。

问题

为什么一个表达式(具有“字符串”类型的对象)产生“真”而另一个表达式(具有“实例”类型的对象)产生“假”?

代码:

--In context of these following 2 function in the following 2 lines:
    box.Touched:Connect(onTouchedDebounced)
    function onTouchedObject(otherObjectPart)

--consider the following code:


--After declaring objects to be compared below in the following 3 lines:    
     local otherObjectPartsParent=otherObjectPart.Parent;
     local resultFromGetPlayerFromCharacterFunction = game.Players:GetPlayerFromCharacter(otherObjectPartsParent);

--Here I want to say:
--ActiveUserPlayerName == ActiveUserPlayerName is true
--Where: (#1): Object1 = "ActiveUserPlayerName";
--Where: (#2): Object2 = "ActiveUserPlayerName";
--Where: (#3): Left_Side_Arguement = ActiveUserPlayerName of type Instance
--Where: (#4): Right_Side_Argument = ActiveUserPlayerName of type Instance
    print ("resultsFromGetPlayerFromCharacterFunction: ", resultFromGetPlayerFromCharacterFunction); --OUTPUT: ActiveUserPlayerName
    print ("otherObjectPartsParent.Name: ", otherObjectPartsParent); --OUTPUT: ActiveUserPlayerName
    print ("typeof(resultsFromGetPlayerFromCharacterFunction): ", typeof(resultFromGetPlayerFromCharacterFunction)); --OUTPUT: Instance
    print ("typeof(otherObjectPartsParent): ", typeof(otherObjectPartsParent)); --OUTPUT: Instance
    print ("resultFromGetPlayerFromCharacterFunction == otherObjectPartsParent is: ", resultFromGetPlayerFromCharacterFunction == otherObjectPartsParent); --OUTPUT: False


--Here I want to say:
--ActiveUserPlayerName == ActiveUserPlayerName is true
--Where: (#1): Object1 = "ActiveUserPlayerName";
--Where: (#2): Object2 = "ActiveUserPlayerName";
--Where: (#3): Left_Side_Arguement = ActiveUserPlayerName of type string
--Where: (#4): Right_Side_Argument = ActiveUserPlayerName of type string
    print ("resultsFromGetPlayerFromCharacterFunction.DisplayName: ", resultFromGetPlayerFromCharacterFunction.DisplayName); --OUTPUT: ActiveUserPlayerName
    print ("otherObjectPartsParent.Name: ", otherObjectPartsParent.Name); --OUTPUT: ActiveUserPlayerName
    print ("typeof(resultsFromGetPlayerFromCharacterFunction.DsplayName): ", typeof(resultFromGetPlayerFromCharacterFunction.DsplayName)); --OUTPUT: string(*corrected)
    print ("typeof(otherObjectPartsParent.Name): ", typeof(otherObjectPartsParent.Name)); --OUTPUT: string (*corrected)
    print ("resultFromGetPlayerFromCharacterFunction.DisplayName == otherObjectPartsParent.Name is: ", resultFromGetPlayerFromCharacterFunction.DisplayName == otherObjectPartsParent.Name); --OUTPUT: True

你的变量名使这个非常难以阅读。但是通过将 otherObjectPartsParent 重命名为 character 并将 resultFromGetPlayerFromCharacterFunction 重命名为 player,它使正在发生的事情更有意义。

local character = otherObjectPart.Parent;
local player = game.Players:GetPlayerFromCharacter(character);

print ("player : ", player); --OUTPUT: ActiveUserPlayerName (a tostring'd version of the Player object)
print ("character.Name: ", character.Name); --OUTPUT: ActiveUserPlayerName
print ("typeof(player): ", typeof(player)); --OUTPUT: Instance (of class Player)
print ("typeof(character): ", typeof(character)); --OUTPUT: Instance (of class Model)
print ("player == character is: ", player == character); --OUTPUT: False (pointer comparison, these are two different objects)

print ("player.DisplayName: ", player.DisplayName); --OUTPUT: ActiveUserPlayerName
print ("character.Name: ", character.Name); --OUTPUT: ActiveUserPlayerName
print ("typeof(player.DisplayName): ", typeof(player.DisplayName)); --OUTPUT: Instance (this looks wrong, this should be a string)
print ("typeof(character.Name): ", typeof(character.Name)); --OUTPUT: Instance (this also looks wrong, this should be a string)
print ("player.DisplayName == character.Name is: ", player.DisplayName == character.Name); --OUTPUT: True (string comparison, this should be correct)

归根结底,您是在比较两个不同的对象。当有人加入游戏时,他们的 Player is added to the Players service, and their Character 会添加到工作区。

玩家和角色的名字相同,但一个代表他们在 Roblox 上的身份,另一个代表他们在游戏中的物理表现。

当谈到比较时,比较的是什么类型。 Lua bools、ints、strings 和 nil 等原语将按值进行比较。表和用户数据对象将通过指针引用进行比较。所以当谈到 Roblox lua 时,你需要注意你正在处理的对象类型。实例是引擎盖下的用户数据,因此仅仅因为两个对象具有相同的名称,它们并不总是相等的。