HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Recycling - how would you do it?

08-06-2008, 08:26 AM#1
Vestras
Yeah, how would you do it? I'm making a "Custom Value" system, and I need to know how to recycle. This is what I've got so far, and I think it's works: (system = undone)

Collapse JASS:
library ArrayIntegers

globals
  private integer DummyID = 'e000'
  // Raw code of the dummy
endglobals

//! textmacro Recycle takes INSTANCE,UNIT
   if(IsUnitType($UNIT$,UNIT_TYPE_DEAD)==true)then
     set Integer=GetUnitUserData(DummyUnit[GetUnitUserData($UNIT$)])
   endif
//! endtextmacro

globals
  private unit array ArrayUnit
  private unit array DummyUnit
  private integer Integer=0
endglobals

function GetUnitArrayInteger takes unit whichUnit,boolean showAsDebug returns integer
 debug if(showAsDebug==true)then
  debug call BJDebugMsg(I2S(GetUnitUserData(whichUnit)))
 debug endif
return GetUnitUserData(whichUnit)
endfunction

function SetUnitArrayInteger takes unit whichUnit,integer customValue returns nothing
  set Integer=Integer+1
  set ArrayUnit[Integer]=whichUnit
  
   set DummyUnit[customValue]=CreateUnit(GetOwningPlayer(whichUnit), DummyID, 0, 0, 0)
   //! runtextmacro Recycle("Integer","ArrayUnit[Integer]")
   call SetUnitUserData(ArrayUnit[Integer],customValue)
endfunction

endlibrary

Another method I might use; (Not done either)
Collapse JASS:
library ArrayIntegers

globals
 private unit array Unit
 private unit array Selected
 private integer array Index
 private integer MaxIndex = 0
 private integer Indexes = 0
 private integer RecycleIndex = 0
 private integer Array = 0
 private player SPlayer
endglobals

function GetUnitArrayInteger takes unit whichUnit returns integer
  return GetUnitUserData(whichUnit)
endfunction

private function RecyclePeriodic takes nothing returns nothing
 local integer i = 0
  
  loop
   exitwhen i > Indexes
    if GetWidgetLife(Unit[i]) < .405 then
     set Array = GetUnitUserData(Unit[i]) - 1
     set RecycleIndex = RecycleIndex + 1
     debug call BJDebugMsg("|c00008080ArrayIntegers:|r Recycling.. (" + I2S(RecycleIndex) + " times recycled.)")
    endif
   set i = i + 1
  endloop
endfunction

function SetUnitArrayInteger takes unit whichUnit, integer CustomValue returns nothing
  set Array = Array + 1
  set Unit[Array] = whichUnit
  set Index[Array] = CustomValue
  set Indexes = Indexes + 1
  if CustomValue > MaxIndex then
   set MaxIndex = CustomValue
  endif
  call SetUnitUserData(Unit[Array], CustomValue)
endfunction

private function SetSelectedGroup takes nothing returns nothing
    set Selected[GetPlayerId(SPlayer)] = GetEnumUnit()
endfunction

private function SetSelected takes nothing returns boolean
    local group g = CreateGroup()
    set SPlayer = GetTriggerPlayer()
    call EnumUnitsSelected(GetTriggerPlayer(), null, function SetSelectedGroup)
    set SPlayer = null
    return false
endfunction

//************************
// Debuging stuff        *
//************************

function DisplayInt takes unit whichUnit returns nothing
// This function displays the unit you want's ArrayInteger
  call BJDebugMsg(I2S(GetUnitArrayInteger(whichUnit)))
endfunction

function DisplayStats takes nothing returns nothing
// This function displays all stats of the game
   call BJDebugMsg("|c00008080*******************************************|r")
   call BJDebugMsg("|c00008080Biggest index ever:|r " + I2S(MaxIndex))
   call BJDebugMsg("|c00008080Indexes in use:|r " + I2S(Indexes))
   call BJDebugMsg("|c00008080Times Recycled:|r " + I2S(RecycleIndex))
   call BJDebugMsg("|c00008080*******************************************|r")
endfunction

function DisplaySelectedInt takes player Owner returns nothing
// This function displays the player's selected unit's ArrayInteger
local integer I = GetPlayerId(Owner)
  call BJDebugMsg(I2S(GetUnitArrayInteger(Selected[i])))
endfunction

//************************
// End of debug stuff    *
//************************

function InitTrig_ArrayIntegers takes nothing returns nothing
   local trigger t = CreateTrigger()
   local integer i = 0
   
   call TimerStart(CreateTimer(), .50, true, function RecyclePeriodic)
   call TriggerRegisterPlayerSelectionEventBJ(t, Player(i), true)
   call TriggerAddCondition(t, Condition(function SetSelected))
endfunction

endlibrary
08-06-2008, 02:06 PM#2
the-thingy
Not 100% sure on what exactly you're doing - you mean when a unit needs to be removed from the system, all entries related to that unit will be cleared and the unit and the current number of in-use arrays will be reduced by (number of entries that said unit took up)? If so, you might wanna take a look at TT, it should be of help (or this post)

Anyway, not really sure why you're running your recycle macro within the Set function (since it's highly unlikely they will ever be recycled in that case). And you don't need that showasDebug argument on the Get function (you already have the debug prefix so it's a bit pointless)

EDIT: Also, you shouldn't recycle the units when they are dead - what if the unit dies and gets revived by Resurrection/some other spell (or Reincarnation, possibly, not fully sure how it works)