Jass documentation Database

JASS Documentation Database REFORGED

Results for the query "GetLocalPlayer" as a "function"

The search keyword must be a single word (no spaces).

Function GetLocalPlayer

Line: 3109
Location: common.j
Constant? yes
Type: native
Arguments: nothing
Returns: player


Extra spaces may have been removed from the original file to show the function in a comfortable way.
  1. constant native GetLocalPlayer takes nothing returns player

Information about GetLocalPlayer

Information contributed by City17
on April 07, 2022, 06:34:13 PM.
returns reference to local player, as such it always points to yourself. Every other player in the game gets his player respectively.

Do not use this function until you understand it fully and know how to avoid desyncs! Always test your map in LAN multiplayer after changing code around it.

Warcraft 3 has the lock-step network/execution model. This means the game simulation and code run on all players' computers with the exact same data at any point in time. Look at this example:
Code: Lua
  1. function whoami_command()
  2. -- all players know who entered this command, equal value on every computer
  3. local player_who_entered_command = GetTriggerPlayer()
  4. -- this always points to you!
  5. local myself = GetLocalPlayer()
  7. local command_player_name = GetPlayerName(player_who_entered_command)
  8. local my_player_name = GetPlayerName(myself)
  9.  -- everybody will see this player's name
  10. DisplayTextToForce(GetPlayersAll(), "Player ".. command_player_name .." entered the whoami command!")
  11. -- everybody will see their own name!
  12. DisplayTextToForce(GetPlayersAll(), "But my name is: ".. my_player_name)
  13. end

This function is always used when you want something only to happen to a certain player. However if you aren't careful, you will cause a desync: where one player's game state is different from everybody else!
For example, you can apply camera position locally, this is how SetCameraPositionForPlayer works:
Code: jass
  1. if (GetLocalPlayer() == whichPlayer) then
  2.    // Use only local code (no net traffic) within this block to avoid desyncs.
  3.    call SetCameraPosition(x, y)
  4. endif
Basic rule: anything that's only visual (like unit color) will not desync... unless your code relies on the exact value later in the game: if cameraX is 123 then ... different players will have different camera positions here.

On the other hand, manipulating handles or creating units locally, changing their health, attack, invisibility etc. - anything that changes the game will desync:
Code: jass
  1. if (GetLocalPlayer() == whichPlayer) then
  2.    // INSTANTLY DESYNCS! The unit was only killed on one player's screen! Others think it's alive
  3.    KillUnit(someUnit)
  4. endif
Page loaded in 0.2009 seconds.
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...