Using Player(Integer A) in JASS

Jesus4Lyf

Good Idea™
Reaction score
397
>So I have to null every variable related to an object I want to remove from the game?
>Like say.. a dummy unit?
Yes.

>Furthermore, all local variables
And global variables... (I sincerely hope.)

(And hell, a parameter can probably be in an infinite TSA loop to leak a handle too! :D)

>Actually, globals don't need to be nulled.
In a perfect world, globals should be nulled immediately when objects are destroyed as well, due to the persistent references leak. But that's outside of the community's common practises, so whatever.
 

LearningCode

New Member
Reaction score
24
You know..
I have been trying out all the suggestions..

And none of them return syntax errors..
But..

Nothing happens in game ._.

This is the code I'm using now:
JASS:
function creation takes nothing returns nothing
    local integer i=8
    local player p
    local real c
    local real d
    loop
        exitwhen i==0
        set i=i-1
        set p=Player(i)
        if GetPlayerSlotState(p)==PLAYER_SLOT_STATE_PLAYING then
            set c = GetPlayerStartLocationX(p)
            set d = GetPlayerStartLocationY(p)
            call SelectUnitForPlayerSingle(CreateUnit(p,'e008',c,d,0),p)
            set c = 0.00
            set d = 0.00
        endif
    endloop
endfunction

function CreateBuilder takes nothing returns nothing
   local trigger t
   set t= CreateTrigger()
   call TriggerRegisterTimerEventSingle( t, 2.00 )
   call TriggerAddAction(t, (function creation))
endfunction


But nothing is going on..
=/

[edit]
Wait, I know why.

Player 1 is Player 0.

It exits when it reaches 0 and never creates a unit for zero =.=

[Edit 2]
Nope, still fails

ARGH!!
JASS IS HARD!!!
 

Nestharus

o-o
Reaction score
84
JASS:

function Creation takes nothing returns nothing
    local integer i=8 //guessing 8 players?
    local player curPlayer //need better names mate
    local unit curUnit
    loop
        set i = i - 1
        set curPlayer = Player(i)
        if GetPlayerTeam(curPlayer) != -1 then //means they are in game
            set curUnit = CreateUnit(curPlayer, 'e008', GetStartLocationX(i), GetStartLocationY(i), 0)
            if GetLocalPlayer() == curPlayer then //only run for curPlayer
                call SelectUnit(curUnit, true)
            endif
        endif
        exitwhen i==0
    endloop
    set curUnit = null //local
    call DestroyTimer(GetExpiredTimer())
endfunction

function CreateBuilder takes nothing returns nothing
    call TimerStart(CreateTimer(), 2, false, function Creation)
endfunction
 

LearningCode

New Member
Reaction score
24
WHAT THE EFF!?

This is the SECOND time this has happened today =.=


Trigger will not work until I make some remote changes.
Thanks, Nestharus, but I got a working code at last, just looking for optimization now.

Comparing Failed attempt:
JASS:
function creation takes nothing returns nothing
    local integer i=8
    local player p
    local real c
    local real d
    loop
        exitwhen i==0
        set i=i-1
        set p=Player(i)
        if GetPlayerSlotState(p)==PLAYER_SLOT_STATE_PLAYING then
            set c = GetPlayerStartLocationX(p)
            set d = GetPlayerStartLocationY(p)
            call SelectUnitForPlayerSingle(CreateUnit(p,'e008',c,d,0),p)
            set c = 0.00
            set d = 0.00
        endif
    endloop
endfunction

function CreateBuilder takes nothing returns nothing
   local trigger t
   set t= CreateTrigger()
   call TriggerRegisterTimerEventSingle( t, 2.00 )
   call TriggerAddAction(t, (function creation))
endfunction


And succeeded attempt:
JASS:
function Action takes nothing returns nothing
   local integer i=0
   local player b
   local location l
   call KillUnit( gg_unit_nC15_0011 )
      loop
         exitwhen (i>8)
         set b=Player(i)
         if GetPlayerSlotState(b) == PLAYER_SLOT_STATE_PLAYING then
            set l = GetPlayerStartLocationLoc(b)
            call DisplayTextToForce( GetPlayersAll(), "Hello")
            call CreateNUnitsAtLoc( 1, 'e008', b, l, bj_UNIT_FACING )
            call SelectUnitForPlayerSingle( GetLastCreatedUnit(), b )
            set l = null
         endif
         set i=i+1
      endloop

endfunction

//===========================================================================
function InitTrig_BUILD takes nothing returns nothing
    local trigger t
    set t = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( t, 2.00 )
    call TriggerAddAction( t, function Action )
endfunction


[Edit]
8 means 9 players xD
0 is player 1
 

Nestharus

o-o
Reaction score
84
Use the code I put up... that's optimized to a large degree. Any more optimization and people would call you crazy, plus I don't know if you are using vJASS : P


No, 8 means 8 players because you decrease i at the start.

Finally, I highly suggest you get the addons for WE ;p
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
wat

JASS:
function thegame takes game whichGame, boolean hasLost returns disappointment
    local player p = Player(0)
    // Do we need to null p?
    set p = null

    // We should totally have smilies in JASS code...
    return :banghead:
endfunction


EDIT: oh, I just read jesus' post on leaks in the other thread

So basically I don't need to null p, because I do not ever need (or should) destroy a player. So its not the local variable p that leaks, but the handle itself when it is destroyed and there are still variables pointing at it.
 

LearningCode

New Member
Reaction score
24
I want to be using codes that I understand =x
Or else it wouldn't be learning xD

JASS:
function Action takes nothing returns nothing
   local integer i=0
   local player b
   local real x
   local real y
   call KillUnit( gg_unit_nC15_0011 )
      loop
         exitwhen (i>8)
         set b=Player(i)
         if GetPlayerSlotState(b) == PLAYER_SLOT_STATE_PLAYING then
            set x = GetStartLocationX(i)
            set y = GetStartLocationY(i)
            call SelectUnitForPlayerSingle(CreateUnit(b,'e008',x,y,0),b)
         endif
         set i=i+1
      endloop

endfunction

//===========================================================================
function InitTrig_BUILD takes nothing returns nothing
    local trigger t
    set t = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( t, 2.00 )
    call TriggerAddAction( t, function Action )
endfunction


This code works.
Is there anything unnecessary?

I don't understand half your codes, Nestharus T.T
And me haz 9players.

@Narks
I'll send 4mudkipz at j0o face, watch out.
All your face are belongz to teh mudkipz.

[EDIT]
Ignore this part of the code:

JASS:
   call KillUnit( gg_unit_nC15_0011 )


It's for something else.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
i think indenting convention when it comes to loops is:
JASS:
function a
    call KillUnit(u)
    loop
        call Stuff()
    endloop


compared to:
JASS:
function a
    call KillUnit(u)
        loop
            call Stuff()
        endloop
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
sorry, I had to edit it because it wouldn't display properly

when it comes to indenting, generally programmers would do

JASS:

call a()
loop
    stuff
endloop


rather then

JASS:

call a()
    loop
        stuff
    endloop
 

LearningCode

New Member
Reaction score
24
Does it affect the speed of the trigger or is it to improve readability of the code for other programmers?
 

Nestharus

o-o
Reaction score
84
Actually it's-

JASS:

call a()
loop
    //stuff
endloop


I've never seen in any language a convention that did what you specified Narks

Also, LearningCode, the code I wrote is about as optimal as can be. I really suggest you use it, and I'll explain what parts you don't understand ; ).

JASS:

//Capitalize functions ><
function Creation takes nothing returns nothing
    local integer i=9 //for 9 players
    local player curPlayer //need better names mate //player var
    local unit curUnit //unit var
    loop //enter loop
        set i = i - 1 //every iteration, dec i by 1. This means it starts on player 9, or Player(8) because of offset
        set curPlayer = Player(i) //set the curPlayer var to Player(i), start at Player(8)
        if GetPlayerTeam(curPlayer) != -1 then
        /*if the player is in a team, continue. All players start out initially in teams incrementing with each player. First player is in team 0, 
           second in team 1, etc. This means that if the team of the player is not -1, that player is in the game. It's about 2x faster than checking
           for SLOT_STATE_PLAYING.*/

            set curUnit = CreateUnit(curPlayer, 'e008', GetStartLocationX(i), GetStartLocationY(i), 0)
            /*This will just set curUnit to a new unit. GetStartLocationX/Y are the actual start loc natives. Each start location has an index
               equal to the player it is for. GetStartLocationX(0) returns the start location x coord of player 0 (Player(0)) */

            if GetLocalPlayer() == curPlayer then
            /*This is a little more advanced. GetLocalPlayer() returns the player on the current running machine. When JASS code runs, it
               runs on every single machine. Each machine has its own local player var. In essence, this is an if statement to make sure the
               player on the machine is equal to the player that the code is running for. This is normally used for local UI. What this means is that
               the code in this if statement will only run for the machine that curPlayer is on, for example if curPlayer was Player(0), this code
               would only run for Player(0). */

                call SelectUnit(curUnit, true)
                //Simple SelectUnit native. It's normally meant to be used in local network code (GetLocalPlayer()). If it isn't, it runs for all players
            endif
        endif
        exitwhen i==0 //if player is player 0, then you don't need to enter the loop again
    endloop
    set curUnit = null //so that the handle id is recycled.
    call DestroyTimer(GetExpiredTimer()) //this destroys the timer that the code is running on
endfunction

function CreateBuilder takes nothing returns nothing
    call TimerStart(CreateTimer(), 2, false, function Creation)
    /*This will first create a new timer. Timers run code every x seconds or after x seconds pass. This timer does not repeat, so it
       will only run once. In essence, the Creation function will run after 2 seconds ;p. CreateTimer() is passed as an argument, meanning
       that it just passes in a new timer without setting it to a variable ; ). Yes, you can do that. */
endfunction


Ok, I've nearly every single line, so you have no choice but to use it ;p
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
Actually it's-

JASS:

call a()
loop
    //stuff
endloop


I've never seen in any language a convention that did what you specified Narks

you read my post the wrong way around
 

LearningCode

New Member
Reaction score
24
@Nestharus

I concede, lol.

I WILL use it, because it has everything.
Even things I would never even dream of xD

and 2x faster does sound good to me ears.

But just cos' I understand your code doesn't mean I've memorized it or can apply it yet =/
It'll come with practise, I guess..
 

Nestharus

o-o
Reaction score
84
Sry, I had an error in the if statement

It should be != -1 ><

You can just look in the code again, I fixed it

My explanation on GetPlayerTeam() was also wrong : )

Btw, the guide i'm writing explains just as in-depth as above, so you should read it >: P
 

LearningCode

New Member
Reaction score
24
Yea, I tried to implement the code and it failed to create any units.

BUT NO SYNTAX ERRORS AT ALL!?
Gawd, that's gay (In a good way)


Each team.. ?

So if we had 2 teams..
We can have 2 Player(0) ?

First player of team 1 and First player of team 2?

Or am I thinking too much xD
 

Nestharus

o-o
Reaction score
84
Er, GetPlayerTeam does not relate to forces.

Each player is always on a unique team. It only runs for active players in the map (computers and humans) in the order that it finds them.

If you have 6 players in the map, teams will go from 0-5 regardless of whether one player is player 0 and another player is player 8. The ids are assigned to each player in that order

First player in the game (the lowest player) is always 0
Second is always 1
Third is always 2

etc

Players that aren't in the game have an id of -1

If you have cJASS and vJASS, you can run this code in your map to see for yourself ; )

JASS:

scope Demo initializer Initialization {
    private void Initialization() {
        int i = 11
        do {
            printf(I2S(i)+&quot; &quot; + I2S(GetPlayerTeam(Player(i--))))
        } whilenot i==0
        DestroyTimer(GetExpiredTimer())
    }
}


I suggest that you have a comp in there ;p. For example, make like players 1-4 users and player 5 a comp, then just test map. You'll see that you are 0 and the comp is 1, all the rest are -1 ; )
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top