JASS: GetLocalPlayer()

Andrewgosu

The Silent Pandaren Helper
Reaction score
715
This tutorial has an excellent layout, it's nicely written and easily understandable.

the misusage of "GetLocalPlayer()" function is one of the most common reasons of desyncs
and I doubt anyone will argue me if I state this tutorials explains very well how
to get around using the function properly.

Approved.
 

Strilanc

Veteran Scripter
Reaction score
42
I got a bunch of rep for this post, so people seemed to like the description. Maybe you'll find it useful.

Unlike almost all the other functions, GetLocalPlayer returns a different value for each player. On player 1's computer the function returns player 1, etc. That means you can do things like:
JASS:
if GetLocalPlayer() == Player(0) then //0 is the first player, player 1
  call CreateUnit(Player(0), 'hfoo', 0, 0, 0) //create a footman at the center
endif

which will create a footman for player 1, but only if you're player 1!

Suppose the game allowed that. Player 1 orders the new footman to attack. All the other players' computers go "huh? order what footman to attack? there is no footman!". So they ignore the command to the non-existent footman and whatever the footman was attacking only dies for player 1. This effect (called desyncing) gets progressively worse as the effects of the footman spread. Eventually player 1 is looking at a game totally different from player 2, and nothing either of them does makes sense to the other.

To avoid that whole song and dance, desynced players are just instantly removed from the game.

When replays were introduced in starcraft they wouldn't always play out exactly how the game went. The replay was (essentially) desyncing. Orders would sometimes be done a few milliseconds later in the replay than in the game [or something like that], and the consequences of that late order would grow and grow until the replay didn't match the original game at all. This is actually the reason your orders always take a small amount of time before being done [lag]. Commands must be communicated to the other players so they are performed at the same game time. FPS games also have this command lag, but they hide it by re-interpreting what happened after the fact [oh, turns out you got shot a second ago] [doing this is NOT viable in RTS games].

So the first thing you should think of when using GetLocalPlayer: will the computers still be able to understand each other if I only do this on one of them? Displaying text, hiding floating text, playing sounds, changing unit scales, and a bunch of other things are all fine. Some things you might expect to be fine are not. For example creating floating text (or special effects, or unit groups, or any other handle type) on only one computer changes future handle ids, meaning one computer might say "order footman 26 to x,y" and the others interpret that as "order floating text 26 to x,y", and a desync happens.
 

pablo1517

New Member
Reaction score
1
is GetLocalPlayer() a way to for eg. play a music theme just for 1 player?
Like let's say: Player 3 got to the forest, i want to play forest theme just by player 3, is this possible?
 

Romek

Super Moderator
Reaction score
963
is GetLocalPlayer() a way to for eg. play a music theme just for 1 player?
Like let's say: Player 3 got to the forest, i want to play forest theme just by player 3, is this possible?
Yes. Although I don't think you can create sound variables for local players, you should be able to start/stop the sound without any risks :)
 

pablo1517

New Member
Reaction score
1
I would like to make something like - if player is in a tavern for example, then he hears tavern music, but only the player which is in tavern actually :D. I hope I'll get it. Thx.
 

Light Alkmst

New Member
Reaction score
20
Maybe I missed something in the tutorial, but what are the usages of GetLocalPlayer(), and not just telling all of the computers about what you're doing to one computer? Is it a way to reduce potential code?

Code:
if (GetLocalPlayer() == GetTriggerPlayer()) then
     // Would this work for getting the triggering player only to run this code?
endif

also, (slightly off-topic) if you have a trigger that sends one player a message without using GetLocalPlayer(), would it send the message only once or once per player processing it?

EDIT: another question: if there is a desync caused by GetLocalPlayer(), will it only boot the desynced player, or everyone in the game?

scenario:
Code:
// Let's say this code runs on some normally undetectable unit being spotted
function Some_Handwritten_Anti_MH_Code_Actions takes nothing returns nothing
     if (GetLocalPlayer() == TriggeringPlayer()) then
          call CreateUnitAtLoc(GetLocalPlayer(), 'A000', Location(0.0, 0.0))
     endif
endfunction
// Would it desync only the hacking player?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
> Would this work for getting the triggering player only to run this code?

That is the entire point of that function...

> would it send the message only once or once per player processing it?

Game - Display to ... the text: "Now you're over-reacting"
Using GetLocalPlayer() requires care. Nothing more. Paranoia is not required.

> will it only boot the desynced player, or everyone in the game

Nice try.
 

Nexor

...
Reaction score
74
I'm trying to make a local weather system, but I can't get it working. here's the code:

JASS:
function Actions takes nothing returns nothing

    local integer i
    local string s
    set s = SubString(GetEventPlayerChatString(), 10, StringLength(GetEventPlayerChatString()))
    call EnableWeatherEffect( GetLastCreatedWeatherEffect(), false )
    if GetLocalPlayer() == GetTriggerPlayer()  then
        if     s == "rain" then
            set i = 'RAlr'
        elseif s == "snow" then
            set i = 'SNbs'
        elseif s == "off" then
            set i = 0
            
        endif
    endif
    
    call AddWeatherEffectSaveLast( GetPlayableMapRect(), i )
    call EnableWeatherEffect( GetLastCreatedWeatherEffect(), true )
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing

    local trigger t = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( t, Player(0), "-weather", false )
    call TriggerAddAction( t, function Actions )
    set t = null

endfunction


What's wrong here? there's no dsyncing (i think) but the effect won't show :( pls help me
 

tooltiperror

Super Moderator
Reaction score
231
Thanks, I always wondered why you did x100000 or whatever.
 
Reaction score
7
I have seen desyncs on my DS when my brother used Action Replay to cheat in multiplayer. It seems that the game (which in this case was New Super Mario Bros.) sends details about button presses to the other consoles, so if one player uses AR, and the others don't, it will desync.
He used the 'always have item' cheat, so his console thought he had the item, and my console didn't.

Eventually he killed 'me' on his game and his console thought he won, and my console thought the game was still going, so his game hanged on the score screen waiting for mine to finish.
Then when we started the next round, we were looking at different courses!

LMAO; obviously this game doesn't realise it had desynched and cut it off like Warcraft etc. do.

If we hadn't turned off the game before then, eventually his console would show him winning 3 games to nil and disconnect, and my console would be showing something different and report a connection failure.

P.S. Someone please tell me how I can change the text on my account where it says 'Junior User'.
 

jackall

You can change this now in User CP.
Reaction score
37
Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • (Entered chat string) Equal to !show kills
    • Then - Actions
      • Custom script: if GetTriggerPlayer()==GetLocalPlayer() then
      • Leaderboard - Show KillLDB
      • Custom script: endif
    • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Entered chat string) Equal to !hide kills
        • Then - Actions
          • Custom script: if GetTriggerPlayer()==GetLocalPlayer() then
          • Leaderboard - Hide KillLDB
          • Custom script: endif
        • Else - Actions

Does it cause a desync if i use it like that?
 

luorax

Invasion in Duskwood
Reaction score
67
Showing/Hiding quests, multiboards, texttags locally won't cause desyncs, I don't think hiding a leaderboard would do it either.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top