Sorry, we don't support your browser.  Install a modern browser

Player instances becoming "stale"#681

Hello,
Code example:

local playerInstance

function onLoad()
print(‘onLoad!’)
for _, player in ipairs(Player.getPlayers()) do
playerInstance = player
end
end

function onPlayerChangeColor(player_color)
player = GetPlayerByColor(player_color)

if playerInstance == player then
    print("Always has been")
else
    print("New player?")
end

end

function GetPlayerByColor(player_color)
for _, player in ipairs(Player.getPlayers()) do
if player.color == player_color then
return player
end
end
end
–(Obviously only works for one player games)

(Excuse the formatting, I don’t see code tags here.)

In this scenario, when loaded up with only one player, the player instances won’t match.
This is supposedly not intended(?). I was told I could report this here. I’m quoting benjamin_dobell for the explanation:

“Yeah, Player is rather wonky. Players returned from GetPlayers() and GetSpectators() exhibit different behaviour than Player[color].

You probably don’t want to hang onto player references returned from these two methods.

Player references retrieved by a colour on the other hand are a bit easier to reason about. They essentially represent a seat (rather than a player). If someone sits in the colour, the player reference will reflect the new player sitting there (their Steam ID).”

“GetPlayers() and GetSpectators() tries to more accurately represent a real player, rather than a seat. But they become stale. By this I mean, they will always return the same steam name. If the player moves seat, your reference will still continue to return that player’s steam name. The issue is though is that color becomes stale on these references, when you move seat.”

“They’re not separate classes. I believe things ended up this way due to backward compatibility, and now the APIs are a bit wonky. If people want to file some Nolt issues about this, then that’d give me an excuse to work on cleaning these APIs up”

Weird to give excuses so something is being done, rather than not.
Life is odd sometimes…

9 days ago

Use playerInstanceColor insted of playerInstance ;) It should be a color name (a string), not a pointer.

local playerInstanceColor
playerInstanceColor = "White";
playerInstanceColor = player.color;
Player[playerInstanceColor] -- instead of playerInstance
9 days ago
B

Weird to give excuses so something is being done, rather than not. Life is odd sometimes…

It’s not that odd.

There’s the professional aspect. Like most jobs, I can’t just go off working on whatever I feel like without getting approval or documenting what I’m doing!

However, personal professionalism aside, there’s competing priorities and it’s important to focus development efforts on what will most benefit players. Particularly in cases like this where it’s a feature request rather than a bug report (the behavior is strange, not broken) then it’s important to get a gauge on how many people are after improvements in this area.

8 days ago