• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Can't set variables inside pick unit group?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
The variable udg_Town_resource_cur[] is not increasing???
udg_Town_resource_cap[] = 100 by default.
and udg_Town_resource_prod[900] = 25

so at the end of thee trigger I should be given The variable udg_Town_resource_cur[900] = 25 and next cycle 50... and when it crosses 100 it shouldn't increase anymore, but for some reason it doesn't work?

There is currently only 1 unit in the Town_gr and its custom value is 0.

Can I not set integers inside a picked unit group function?

JASS:
function GetTownGroup takes nothing returns nothing
    local integer unit_d = GetUnitUserData(GetEnumUnit())
    local integer player_i = GetConvertedPlayerId(GetOwningPlayer(GetEnumUnit()))
    local integer x = udg_Town_array_multiplier // x = 300
    local integer z = udg_Player_array_multiplier // z = 15
    local integer i = 0
    local integer waste

// Takes the produced resource and adds it to the current resource variable.
// Array; Town   0-299 Gold, 300-599 Lumber, 600-899 Iron, 900-1199 Food, 1200-1499 Recruits, 1500-1799 Weapons
// Above is used for production and local resource.
// 
// Array; Player 0-14 Gold, 15-29 Lumber, 30-44 Iron 45-59 Food, 60-74 Recruits, 75-89.
// Above is used for global resources and counting.

    loop
        exitwhen i > 5
        if i < 3 then 

        //Global Resources
        set udg_Player_resources[player_i + i * z] = udg_Player_resources[player_i + i * z] + udg_Town_resource_prod[unit_d + i * x]
        
        else

        //Local Resources
        if (udg_Town_resource_prod[unit_d + i * x] + udg_Town_resource_cur[unit_d + i * x]) < udg_Town_resource_cap[unit_d + i * x] then
        // Cap not breached
        set udg_Town_resource_cur[unit_d + i * x] = udg_Town_resource_cur[unit_d + i * x] + udg_Town_resource_prod[unit_d + i * x]
        else
        // Cap breached
        set waste = udg_Town_resource_prod[unit_d + i * x] + udg_Town_resource_cur[unit_d + i * x] - udg_Town_resource_cap[unit_d + i * x]
        set udg_Town_resource_cur[unit_d + i * x] = udg_Town_resource_prod[unit_d + i * x] - waste
        endif
        endif
        set i = i + 1
    endloop
endfunction

function Trig_Give_Actions takes nothing returns nothing
set udg_Town_resource_prod[900] = 25 
call ForGroupBJ( udg_Town_gr, function GetTownGroup)
call BJDebugMsg("Current food: " + I2S(udg_Town_resource_cur[3 * 300]))
call BJDebugMsg("Produced food: " + I2S(udg_Town_resource_prod[3 * 300]))
endfunction

//===========================================================================
function InitTrig_Give_Resources takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEventPeriodic(t, 10.00)
    call TriggerAddAction(t, function Trig_Give_Actions)
    set t = null
endfunction

Here is the setup:
  • Setup
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: local integer i = 0
      • Set Town[0] = Town Hall 0000 <gen>
      • Set Town[1] = Town Hall 0002 <gen>
      • Custom script: loop
      • Custom script: exitwhen i > 1
      • Custom script: set udg_Town_alive[i] = true
      • Custom script: call SetUnitUserData(udg_Town[i], i)
      • Custom script: call GroupAddUnitSimple(udg_Town[i], udg_Town_gr)
      • Custom script: set i = i + 1
      • Custom script: endloop
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
You do not increment the loop variable i and you print the cap value instead of cur. Why do you take the constant index 900.

GAH! Why? well, I was testing the food value of Town[0] and food array is between 900 and 1199 for Town[0] it is 900 and for Town[1] it would be 901.

Added "i = i + 1" still not solved.

Whats the difference between looping through a unit group and looping through a unit array performance wise?

Could I do this instead:

JASS:
local integer a = 0
local integer b
loop 
    // This instead of picked unit. and integer a would be the same as unit_d from above. 
    exitwhen a > udg_Town_max 
    set b = 0
    loop
        exitwhen b > 5
        //Do same as above here.
        set b = b + 1
    endloop
    set a = a + 1
endloop

Lets pretend there would be 150 Town Units (probably less) but then I would have 150*6 loops.... Would this be an issue?

Also whats wrong with this line? How do I use variables "inside functions"?

JASS:
function CheckSelectedUnit takes unit u returns boolean
    if IsUnitInGroup(u, udg_Town_gr) == true or IsUnitInGroup(u, udg_Camp_gr) == true then
        return true
    endif
endfunction

// This Line
    call TriggerAddCondition(t, Condition(function CheckSelectedUnit(GetTriggerUnit())))
 
Last edited:
u can also change this

JASS:
local integer player_i = GetConvertedPlayerId(GetOwningPlayer(GetEnumUnit()))

for this

JASS:
local integer player_i = GetPlayerId( GetTriggerPlayer) )

also change this

JASS:
call ForGroupBJ( udg_Town_gr, function GetTownGroup)

for this

JASS:
call ForGroup( udg_Town_gr, function GetTownGroup)

and this

JASS:
call TriggerRegisterTimerEventPeriodic(t, 10.00)

for this

JASS:
call TriggerRegisterTimerEvent( t, 10.00, true )

as for ur other question about looping through unit array or unit group i would think unit array is faster but im not 100 percent sure lastly ur other question change this

JASS:
function CheckSelectedUnit takes unit u returns boolean
    if IsUnitInGroup(u, udg_Town_gr) == true or IsUnitInGroup(u, udg_Camp_gr) == true then
        return true
    endif
endfunction

// This Line
    call TriggerAddCondition(t, Condition(function CheckSelectedUnit(GetTriggerUnit())))

to this and i think it will work

JASS:
function CheckSelectedUnit takes unit u returns boolean
    if IsUnitInGroup(u, udg_Town_gr) == true or IsUnitInGroup(u, udg_Camp_gr) == true then
        return true
    endif
// This Line
    call TriggerAddCondition(t, Condition(function CheckSelectedUnit(GetTriggerUnit())))
endfunction

also nested loops do not work as expected try putting some debug messages in the loops to paste the value of a and b to see what u get it may be different from what ur trying to do w it
 
Status
Not open for further replies.
Top