HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Wrong Usage of Structs?

01-31-2009, 04:51 AM#1
Joker
I decided to keep my players stats in a struct to keep them organized, but they don't seem to be working the way I want to.

Collapse JASS:
library PlayerVars initializer Init 

    globals
        Stats array PlayerStats[5]
    endglobals
    
    //=======================================================
    struct Stats
        integer CreepKills
        integer Current
        integer HeroKills
        integer Deaths
        integer Bounty
        integer Stage
        integer Streak
        integer Team
        integer Row
        
        static method create takes nothing returns Stats
            local Stats dat = Stats.allocate()
            
              set dat.CreepKills = 0
              set dat.Current = 0
              set dat.HeroKills = 0
              set dat.Deaths = 0
              set dat.Bounty = STARTING_GOLD
              set dat.Stage = 1
              set dat.Streak = 0
              set dat.Team = 0
              set dat.Row = 0
              call BJDebugMsg(I2S(dat.Bounty)) //Shows 550(Starting gold)
            
            return dat
        endmethod
        
        method Refresh takes nothing returns nothing
            set .Current = 0
        endmethod
        
        method RaiseCreepKills takes nothing returns nothing
            set .CreepKills = .CreepKills + 1
            
            if UnitCount[.Team] < 30 then
                set .Current = .Current + 1
                call MultiboardSetItemValueBJ( mb, 2, .Row, I2S( .Current ) )
            endif
            
            call MultiboardSetItemValueBJ( mb, 3, .Row, I2S( .CreepKills ) )
        endmethod
        
        method RaiseBounty takes integer bounty returns nothing
            set .Bounty = .Bounty + bounty
            
            call BJDebugMsg(I2S(.Bounty)) //.Bounty was set back to 0...
            call MultiboardSetItemValueBJ( mb, 4, .Row, I2S( .Bounty ) )
        endmethod
        
        method GetHelper takes integer team returns player
            return Player(.Team + 9)
        endmethod
    endstruct
    
    //=======================================================
    private function Init takes nothing returns nothing
        local integer i = 0
        
        loop
            call PlayerStats[i].create()
            set i = i + 1
            exitwhen i >= 6
        endloop
    endfunction

endlibrary
01-31-2009, 05:04 AM#2
TEC_Ghost
You are confusing man, just do this.

Collapse JASS:
library PlayerVars initializer Init 

    globals
        Stats array PlayerStats[5]
    endglobals
    
    //=======================================================
    struct Stats
        integer CreepKills
        integer Current
        integer HeroKills
        integer Deaths
        integer Bounty
        integer Stage
        integer Streak
        integer Team
        integer Row
        
        static method create takes player p returns nothing
              local integer i = GetConvertedPlayerId(p)

              set Stats[i].CreepKills = 0
              set Stats[i].Current = 0
              set Stats[i].HeroKills = 0
              set Stats[i].Deaths = 0
              set Stats[i].Bounty = STARTING_GOLD
              set Stats[i].Stage = 1
              set Stats[i].Streak = 0
              set Stats[i].Team = 0
              set Stats[i].Row = 0
              call BJDebugMsg(I2S(Stats[i].Bounty)) //Shows 550(Starting gold)
        endmethod


Then you can just refer to each from their player number.

Stats[ConvertedPlayerNumber].StructMember
01-31-2009, 05:17 AM#3
Joker
Why use the converted id?
01-31-2009, 10:52 AM#4
akolyt0r
DONT !
..personally i would not keep that player data inside of structs...why not just use normal arrays ?
Structs are lil slower since they need an extra array lookup....
01-31-2009, 11:25 AM#5
Captain Griffen
Wtf are you talking about...?
01-31-2009, 12:07 PM#6
Vexorian
Using ConvertedPlayer Id is incredibly lame.

The world is 0-indexed, get used to it!


This should fix the scary performance drop:
Collapse JASS:
library PlayerVars initializer Init 

    //=======================================================
    struct PlayerStats extends array
        integer CreepKills
        integer Current
        integer HeroKills
        integer Deaths
        integer Bounty
        integer Stage
        integer Streak
        integer Team
        integer Row
        
        method setupd takes nothing returns nothing
              set dat.CreepKills = 0
              set dat.Current = 0
              set dat.HeroKills = 0
              set dat.Deaths = 0
              set dat.Bounty = STARTING_GOLD
              set dat.Stage = 1
              set dat.Streak = 0
              set dat.Team = 0
              set dat.Row = 0
              call BJDebugMsg(I2S(dat.Bounty)) //Shows 550(Starting gold)
        endmethod
        
        method Refresh takes nothing returns nothing
            set .Current = 0
        endmethod
        
        method RaiseCreepKills takes nothing returns nothing
            set .CreepKills = .CreepKills + 1
            
            if UnitCount[.Team] < 30 then
                set .Current = .Current + 1
                call MultiboardSetItemValueBJ( mb, 2, .Row, I2S( .Current ) )
            endif
            
            call MultiboardSetItemValueBJ( mb, 3, .Row, I2S( .CreepKills ) )
        endmethod
        
        method RaiseBounty takes integer bounty returns nothing
            set .Bounty = .Bounty + bounty
            
            call BJDebugMsg(I2S(.Bounty)) //.Bounty was set back to 0...
            call MultiboardSetItemValueBJ( mb, 4, .Row, I2S( .Bounty ) )
        endmethod
        
        method GetHelper takes integer team returns player
            return Player(.Team + 9)
        endmethod
    endstruct
    
    //=======================================================
    private function Init takes nothing returns nothing
        local integer i = 0
        
        loop
            exitwhen i >= 7
            call PlayerStats[i].setup()
            set i = i + 1
        endloop
    endfunction

endlibrary
01-31-2009, 12:26 PM#7
akolyt0r
Quote:
Originally Posted by Captain Griffen
Wtf are you talking about...?
you mean me ?
Collapse JASS:
vJass struct array...gets compiled to something like:
Creepkills[PlayerStats[PlayerId]] //extra array lookup

//normal jass:
CreepKills[PlayerId] //...
01-31-2009, 02:20 PM#8
Captain Griffen
Quote:
Originally Posted by akolyt0r
you mean me ?
Collapse JASS:
vJass struct array...gets compiled to something like:
Creepkills[PlayerStats[PlayerId]] //extra array lookup

//normal jass:
CreepKills[PlayerId] //...

Only when you code it badly. Custom create returning the player id or what vex did, rather than dropping structs.
01-31-2009, 02:59 PM#9
akolyt0r
Quote:
Originally Posted by Captain Griffen
Only when you code it badly. Custom create returning the player id or what vex did, rather than dropping structs.

yeah thats pretty interesting (beside the errors) what he did... gonna check that out
Expand JASS:
01-31-2009, 07:10 PM#10
Joker
What does that
Collapse JASS:
extends array
do?

Edit: I replaced mine with Vex's and the Rows seem to be messed up somehow.


The only place I call the
Collapse JASS:
RaiseCreepKills
method is when a unit dies.
01-31-2009, 10:26 PM#11
Fireeye
If you're refering to the code snippet in your first post.
Where do you set .row? The initial value is 0 so it means ALL rows.
01-31-2009, 10:31 PM#12
Joker
Here
Collapse JASS:
loop
            if GetPlayerSlotState( Player(i) ) == PLAYER_SLOT_STATE_PLAYING then
                set id = GetPlayerId( Player(i) )
                call ForceAddPlayer( ALL_PLAYERS, Player(i) )
                call SetPlayerState( Player(i), PLAYER_STATE_RESOURCE_GOLD, STARTING_GOLD )
                
                if i < 2 then
                    call ForceAddPlayer( TEAM[0], Player(i) )
                    set PlayerStats[id].Team = 0 
                    set PlayerStats[id].Row = id + 2
                
                    call SetPlayerAllianceStateBJ( Player(i), Player(9), bj_ALLIANCE_ALLIED_VISION )
                    call SetPlayerAllianceStateBJ( Player(9), Player(i), bj_ALLIANCE_ALLIED_VISION )
                elseif i < 4 then
                    call ForceAddPlayer( TEAM[1], Player(i) )
                    set PlayerStats[id].Team = 1
                    set PlayerStats[id].Row = id + 3
                    
                    call SetPlayerAllianceStateBJ( Player(i), Player(10), bj_ALLIANCE_ALLIED_VISION )
                    call SetPlayerAllianceStateBJ( Player(10), Player(i), bj_ALLIANCE_ALLIED_VISION )
                else
                    call ForceAddPlayer( TEAM[2], Player(i) )
                    set PlayerStats[id].Team = 2
                    set PlayerStats[id].Row = id + 4
                    
                    call SetPlayerAllianceStateBJ( Player(i), Player(11), bj_ALLIANCE_ALLIED_VISION )
                    call SetPlayerAllianceStateBJ( Player(11), Player(i), bj_ALLIANCE_ALLIED_VISION )
                endif
            endif
            set i = i + 1
            exitwhen i >= 6
        endloop
02-04-2009, 08:17 PM#13
Joker
bump. :/
02-04-2009, 09:30 PM#14
Fireeye
I took a look at the init and the only thing left (i guess) is that you're calling an undeclared array index, therefore the value would be 0.
This line is a bit akward too:
"set id = GetPlayerId( Player(i) )"
When you get the Player ID of Player(i), the id will be equal to i, so this line is quite useless.
But still i can't see any problem atm for the code snippet you provided, maybe i'm blind or the error lies somewhere else.
02-05-2009, 01:45 AM#15
Joker
If I do a
Collapse JASS:
call BJDebugMsg( I2S( PlayerStats[0].Rows ) )
In my loop, it shows 2, but when I do

Collapse JASS:
call BJDebugMsg( I2S( .Rows ) )
At the setup, it shows 0...