Azylaminaz
Vox Populi
- Reaction score
- 91
Well, someone told me it is better to use arrays of say players rather than forces, or use arrays of units rather than groups simply because it is more efficient..
Say I wanted to make a sliding and death trigger for an ice maze. This is how I would do so using groups:
With arrays, I would do:
With the arrays, though, I would need to make a way to handle the global array for allUnits (and any other array system, as well, same with forces). I would approach this two ways, either by making it index at the front (which is how the above functions treat it) or by making the empty indexes point towards a flag, in which instead of exiting loops, the flag positions would be ignored and the loop would loop 0-8191, doing basically nothing on every empty index.
Making all the units fill the front slots would require me to loop to find an open index every time a unit is created. Every time a unit is removed, though, I would need to loop through the entire index shifting every position to the front.
A flag system would start out every index pointing towards a specific unit (one that isn't anywhere or w/e, basically imaginary). When a unit is created, it would be stored at the first location in which the flag unit is found. When a unit is removed, all I would need to do is set the slot to the flag unit again.
I'm just wondering, how is the most efficient way to go about this? I never took time to wonder how efficient my coding really was, just as long as it worked. Is there a way to gauge how much power is required to run a function? IE, counting the number of micro-ops of the function (including calls to other functions).
I don't know, I'm just bored. Also, I don't know if the actual code will work at all.. It might have a bit a typos, didn't run it through a compiler or anything. >.<; But that isn't the point.
Edit:
I don't like using ForForce or ForGroup because you can't pass locals between them, which is something I generally am heavily dependent on.
Say I wanted to make a sliding and death trigger for an ice maze. This is how I would do so using groups:
JASS:
globals
group mazers
constant real speed = 5
constant real radius = 32
endglobals
function Trig_SlideandDeath2_Actions takes nothing returns nothing
local group g = CreateGroup()
local group g2
local unit u
local unit u2
local real x
local real y
call GroupAddGroup(mazers, g)
loop
set u = FirstOfGroup(g)
exitwhen u = null
if GetUnitState(u, UNIT_STATE_LIFE) > 0 then
set x = GetUnitX(u) + speed * Cos(GetUnitFacing(u) * bj_DEGTORAD)
set y = GetUnitY(u) + speed * Sin(GetUnitFacing(u) * bj_DEGTORAD)
if GetTerrainType(x, y) == 039;Nice039; then
call KillUnit(u)
else
call SetUnitPosition(u, x, y)
set g2 = CreateGroup()
call GroupEnumUnitsInRange(g2, x, y, radius, null)
loop
set u2 = FirstOfGroup(g2)
exitwhen u2 = null
if Not(IsUnitAlly(u2, GetOwningPlayer(u))) then
call KillUnit(u)
exitwhen 1 == 1
endif
call GroupRemoveUnit(g2, u2)
set u2 = null
endloop
call DestroyGroup(g2)
set g2 = null
endif
endif
call GroupRemoveUnit(g, u)
set u = null
endloop
call DestroyGroup(g)
set g = null
endfunction
//===========================================================================
function InitTrig_SlideandDeath2 takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEvent(t, .03, true)
call TriggerAddAction(t, function Trig_SlideandDeath2_Actions)
endfunction
With arrays, I would do:
JASS:
globals
unit array heroes
unit array allUnits
constant real radius = 32
constant real speed = 5
endglobals
function Trig_SlideAndDeath_Actions takes nothing returns nothing
local real x
local real y
local integer i = 0
local integer i2
loop
exitwhen heroes<i> = null
if GetUnitState(heroes<i>, UNIT_STATE_LIFE) > 0 then
set x = GetUnitX(heroes<i>) + speed * Cos(GetUnitFacing(heroes<i>) * bj_DEGTORAD)
set y = GetUnitY(heroes<i>) + speed * Sin(GetUnitFacing(heroes<i>) * bj_DEGTORAD)
if GetTerrainType(x, y) == 039;Nice039; then
call KillUnit(heroes<i>)
else
call SetUnitPosition(heroes<i>, x, y)
set i2 = 0
loop
exitwhen allUnits<i> == null
if SquareRoot((GetUnitX(allUnits[i2]) - x) * (GetUnitX(allUnits[i2]) - x) + (GetUnitY(allUnits[i2]) - y) * (GetUnitY(allUnits[i2]) - y)) < radius and Not(IsUnitAlly(allUnits[i2], GetOwningPlayer(heroes<i>))) then
call KillUnit(heroes<i>)
exitwhen 1 == 1
endif
set i2 = i2 + 1
endloop
endif
endif
set i = i + 1
endloop
endfunction
//===========================================================================
function InitTrig_SlideAndDeath takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerAddAction(t, function Trig_SlideAndDeath_Actions)
endfunction</i></i></i></i></i></i></i></i></i></i></i>
With the arrays, though, I would need to make a way to handle the global array for allUnits (and any other array system, as well, same with forces). I would approach this two ways, either by making it index at the front (which is how the above functions treat it) or by making the empty indexes point towards a flag, in which instead of exiting loops, the flag positions would be ignored and the loop would loop 0-8191, doing basically nothing on every empty index.
Making all the units fill the front slots would require me to loop to find an open index every time a unit is created. Every time a unit is removed, though, I would need to loop through the entire index shifting every position to the front.
A flag system would start out every index pointing towards a specific unit (one that isn't anywhere or w/e, basically imaginary). When a unit is created, it would be stored at the first location in which the flag unit is found. When a unit is removed, all I would need to do is set the slot to the flag unit again.
I'm just wondering, how is the most efficient way to go about this? I never took time to wonder how efficient my coding really was, just as long as it worked. Is there a way to gauge how much power is required to run a function? IE, counting the number of micro-ops of the function (including calls to other functions).
I don't know, I'm just bored. Also, I don't know if the actual code will work at all.. It might have a bit a typos, didn't run it through a compiler or anything. >.<; But that isn't the point.
Edit:
I don't like using ForForce or ForGroup because you can't pass locals between them, which is something I generally am heavily dependent on.