• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[Solved] Trouble with usage GetLocalPlayer()

Status
Not open for further replies.
Level 13
Joined
Mar 24, 2013
Messages
1,105
JASS:
function PreventPlay takes nothing returns nothing
    local integer i = udg_PlayerNumberOfWhoseTurn
    local integer i2 = 0
    local player p2
    loop
    exitwhen i2 > 11
    set p2 = Player(i2)
    if GetLocalPlayer() == p2 and i2 == i then
        call EnableSelect(true, true)
        call BJDebugMsg("Allowed playing for " + GetPlayerName(p2))
    else
        call EnableSelect(false, true)
        call ClearSelection()
        call SelectUnit(udg_Builders[GetPlayerId(p2)], true)
       call BJDebugMsg("Blocked for " + GetPlayerName(p2))
    endif
    set i2 = i2 + 1
    endloop

set p2 = null

endfunction

So the goal of the code is to check which player's turn it is by looking at the global, then if it is their turn allow them to click on on things. If it isn't, don't allow them to click on things, select their builder. Currently, it just makes it so no one is able to click on anything.

When it was player 1's turn. It printed to player 1 that player 1 was allowed to move, it printed to player 2, player 1 was not allowed to move.

I'm assuming that there is a problem with my usage of GetLocalPlayer. Any feedback would be great!
 
Last edited:
Level 13
Joined
Mar 24, 2013
Messages
1,105
Okay! I'll keep that in mind, I was just using those to try and Debug. I tried prior without the messages and still experienced the problem (I think anyway) but I shall check and see. Thanks.

Edit: While that may have been an additional problem, no overall fix from msg removal

Edit2:
JASS:
function PreventPlay takes nothing returns nothing

    local integer i = udg_PlayerNumberOfWhoseTurn
    local integer i2 = 0
    local force f = CreateForce()
    local player p
    loop
    exitwhen i2 > 11
        set p = Player(i2)
        if i2 == i then
            call ForceAddPlayer(f, p)
        else
        endif
        set i2 = i2 + 1
    endloop      
  
    if (IsPlayerInForce(GetLocalPlayer(), f)) then
            call EnableSelect(true, true)
    else
            call EnableSelect(false, true)
            call ClearSelection()
            call SelectUnit(udg_Builders[GetPlayerId(p)], true)
    endif
    call DestroyForce(f)
    set f = null
    set p = null

  
endfunction

Just rewrote it a little and it seems to be working, haven't tested extensively though, not sure why this would and the other wouldn't though.
So the EnableSelect is working in so far as the player who should be able to play is able to click and others are not. The selection of the builder is failing however.
 
Last edited:
Level 20
Joined
Aug 13, 2013
Messages
1,696
If the selecting unit doesn't work, maybe try to remove the clear selection() from above of it?, there's a native where you can select units for specific player use it ( SelectUnitAddForPlayer( builder, GetLocalPlayer() ). You can debug the builders to check if it is not nulled after the if block, inside the local else block just add if builder != null then set debugMessage = " exists " else set debugMessage = " "
 
Last edited:
Level 11
Joined
Dec 19, 2012
Messages
411
Well, your first trigger does look fine for me. There only 1 thing make it doesn't work correctly :

This line GetLocalPlayer() == p2 and i2 == i is the main problem.
Imagine player 2 (blue) is the player who got the turn. Your trigger will run, and condition would looks like this :
GetLocalPlayer() == Player(2) and i2 == 1, the condition will return false as a loop and a wrong condition are used.




So, the changes you would need to make would be :

1. GetLocalPlayer() will run for each player, so a loop is not needed.

2. GetLocalPlayer() == p2 and i2 == i should be GetLocalPlayer() == Player(udg_PlayerNumberOfWhoseTurn)

3. change BJDebugMsg() to DisplayTextToPlayer() or DisplayTimedTextToPlayer() if you want to have control over time.

After those changes, your trigger should be working as i see.
 
Level 13
Joined
Mar 24, 2013
Messages
1,105
JASS:
function PreventPlay takes nothing returns nothing

    local integer i = udg_PlayerNumberOfWhoseTurn
    local integer i2 = 0
    local force f = CreateForce()
    local player p
    loop
    exitwhen i2 > 11
        set p = Player(i2)
        if i2 == i then
            call ForceAddPlayer(f, p)
        else
        endif
        set i2 = i2 + 1
    endloop      
 
    if (IsPlayerInForce(GetLocalPlayer(), f)) then
            call EnableSelect(true, true)
    else
            call EnableSelect(false, true)
    endif
    set i2 = 0
    loop
    exitwhen i2 > 11
    set p = Player(i2)
    call SelectUnitForPlayerSingle(udg_Builders[GetPlayerId(p)], p)
    set i2 = i2 + 1
    endloop
    call DestroyForce(f)
    set f = null
    set p = null

 
endfunction

Could still be optimized, but currently is working as intended.

Yeah the rewrite got rid of the loop which DD_legion explains was unneeded.
All the feedback was so helpful in figuring it out. Thanks +reps
 
Last edited:
Status
Not open for further replies.
Top