kts.GetPlayerID

Name

kts.GetPlayerID – return a string uniquely identifying a player

Synopsis

result = kts.GetPlayerID(player)

Description

This function takes one parameter, which can either be a Player object (as returned from kts.GetPlayer for example), or a Creature object representing a knight. A string that uniquely identifies the player (e.g. via their Steam ID) is returned.

Return Value

The return value is a string, which is currently either in the format "123456789@steam" (if the game is compiled with Steam support, and we are in either a LAN Game or Online Game); or in the format "Player Name@name" (otherwise).

The exact format of the string is not guaranteed and may change from one release of Knights to the next.

Errors

If the input is not either a valid Player or a valid Creature representing a knight, then an error is raised.

Notes

The reason why this function is needed is as follows:

In Knights, when you start a new Online Game, LAN Game, Single Player Game or Split Screen game, a new instance of a "Knights server" is created (albeit not a server that's connected to any network, in the case of single player or split screen games). While this Knights server is running, multiple games (quests) can be played. The Lua state persists between quests on the same server, although it is lost once the server is destroyed (i.e. all players disconnect/leave).

Basically, what this means is that Lua code can persist information from one quest to the next, if desired. This works for as long as players are connected to the same server instance – saving information across different server instances is not yet supported.

However, a problem is that the Player objects (e.g. as returned from kts.GetPlayer) are destroyed and recreated for each new quest. Therefore, whilst it's valid to use a Player object as a table key within a single quest, this will not work if you want to persist information (such as player scores, money, etc.) from one quest to the next.

Instead, you can call kts.GetPlayerID which will give you a unique string identifying each player. You can then use this string as a table key, to identify that specific player, and it will work as expected.

See Also

kts.GetAllPlayers

kts.GetPlayer