在 Lua Roblox API 中,当使用 Roblox Lua API 对象并执行概念上类似的算术时,为什么 1 个表达式产生 false 而 1 个表达式产生 true?

问题描述 投票:0回答:1

背景:

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

问题

为什么一个表达式(具有“string”类型的对象)产生“true”,而另一个表达式(具有“Instance”类型的对象)产生“false”?

代码:

--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
lua roblox
1个回答
2
投票

你的变量名使得这个非常难以阅读。但通过将

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)

一天结束时,您正在比较两个不同的对象。当有人加入游戏时,他们的玩家会添加到玩家服务中,并且他们的角色会添加到工作区中。

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

在比较方面,重要的是要比较什么类型。 Lua 原语(如 bool、int、string 和 nil)将按值进行比较。表和用户数据对象将通过指针引用进行比较。因此,当涉及到 Roblox lua 时,您需要注意您正在处理的对象类型。实例是底层的用户数据,因此仅仅因为两个对象具有相同的名称,它们并不总是相等。

© www.soinside.com 2019 - 2024. All rights reserved.