HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

BJ/Native Question.

03-27-2009, 06:30 PM#1
ZugZugZealot
So I was looking through what BJs do when you call them, and I came across one or two that do similar to this one...

Collapse JASS:
function SetCameraFieldForPlayer takes player whichPlayer, camerafield whichField, real value, real duration returns nothing
    if (GetLocalPlayer() == whichPlayer) then
        // Use only local code (no net traffic) within this block to avoid desyncs.
        call SetCameraField(whichField, value, duration)
    endif
endfunction

My question is, how does it work exactly? SetCameraFieldForPlayer takes the arguments Player, camera field, value, and transition time, it calls a function that uses every one of those arguments, except for player. The player argument gets used in a player comparison statement. Does this mean that the function fires for all players in the game at once when called for even just one player's event firing, and thus the if statement?
03-27-2009, 06:39 PM#2
Ammorth
Wrong place entirely. Try the development section.

ps. You can actually post in here?
03-27-2009, 06:46 PM#3
ZugZugZealot
This was showing as a sub forum for...
Wc3campaigns > Warcraft III Modding > Developer's Corner

But looking at it now, I see that it just redirects to resources. -_-
03-27-2009, 06:52 PM#4
Rising_Dusk
How in the world did you actually start this topic? Moved.
03-27-2009, 07:25 PM#5
ZugZugZealot
So I wasn't suppose to be capable of posting in the last forum? >_>
03-28-2009, 12:16 AM#6
Zerzax
I interpret the if statement as "only execute once the target player has been localized". So the native probably already loops through each player and performs the specified action unless the if block limits said action to the local player. This is conjecture, but I'm pretty sure that's what happens.
03-28-2009, 12:42 AM#7
Alevice
The local player conditional is to prevent said function (call SetCameraField(whichField, value, duration)) to be executedfor all players, and it will only run for the player passed as parameter.
03-28-2009, 12:34 PM#8
ToukoAozaki
Let me explain in technical terms.

When a map is loaded and played by multiple players, each computer -not only the one of host- executes a local copy of the script code. What GetLocalPlayer() does is returning the player which belongs to the computer running the code. Thus, calling GetLocalPlayer() produces a value different for every players.

So by comparing GetLocalPlayer() with a player variable, you can control the code to be executed for specific computers. For instance:

Collapse JASS:
if GetLocalPlayer() == player_a then
    // do something
endif

Assuming player_a is a human player, the code inside the if-block will be executed only on player_a's computer. You easily can do the opposite:

Collapse JASS:
if GetLocalPlayer() != player_a then
    // do something
endif

Or something like this:
Collapse JASS:
if IsPlayerInForce(GetLocalPlayer(), force_b) then
    // do something
endif

What SetCameraField(whichField, value, duration) does is setting camera field on the very computer where the function is called. Unfortunately, there is no parameter for specifying a player. In this case, you can control it by putting the code inside if-blocks with conditions using GetLocalPlayer().

While this construct is extremely useful, there is one important thing to keep in mind when using it: affect only local properties that don't affect the game state. There are properties that affect the core gameplay and that does not. For instance, camera field is a local property and never shared, while a unit is a shared property. Once a shared property is altered, different results may be produced for each computer. That is why desync may happen when you change such synchronized properties for gameplay inside GetLocalPlayer() block.

Added:
Modifying UI-related properties are generally considered to be safe. However, always be cautions with handles; creating and destroying most of the handle types would cause desync.