HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

What is wrong with this?

02-11-2008, 10:58 AM#1
Gwypaas
Currently im making a minigame where it spawns towers and you click on them to destroy them but it doesn't work.
This is the main core that spawns towers and checks if they are selected.
Collapse JASS:
scope CheckIfSelected
globals
    private group Towers = CreateGroup()
    private real HowOften = 3
    private integer WhatTower = 'h000'
    private rect Region = gg_rct_Region_000 // The region it creates towers in.
endglobals
private function NewTower takes nothing returns nothing
    local real x = GetRandomReal(GetRectMinX(Region), GetRectMaxX(Region))
    local real y = GetRandomReal(GetRectMinY(Region), GetRectMaxY(Region))
    local unit u = CreateUnit(Player(11), WhatTower, x, y , 270.0)
    call GroupAddUnit(Towers,u)
    //call BJDebugMsg("NewTower Running.")
endfunction
private function Actions takes nothing returns nothing
    local group g
    local group g2
    local unit u 
    local integer i = 0
    set g = Towers
    set g2 = g
    //call BJDebugMsg("Check if unit is selected running.")
    loop
        exitwhen (g == null)
        set u = FirstOfGroup(g)
        loop
            exitwhen (i >=10)
            if IsUnitSelected(u, Player(i)) == true then
                call BJDebugMsg("Kill unit running")
                call GroupRemoveUnit(Towers, u)
                call GroupRemoveUnit(g, u)
                call GroupRemoveUnit(g2, u)
                call KillUnit(u)
            endif
            set i = i+1
        endloop
        call GroupAddUnit(g2, u)
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g = null
    call DestroyGroup(g2)
    set g2 = null
endfunction

//===========================================================================
public function InitTrig takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    local trigger CreateTowers = CreateTrigger()
    call TriggerRegisterTimerEvent( Trig, 0.5, true )
    call TriggerAddAction( Trig, function Actions )
    call TriggerRegisterTimerEvent(CreateTowers, HowOften, true)
    call TriggerAddAction( CreateTowers, function NewTower)
endfunction

endscope



If I click on a tower nothing happens but if I change:
Collapse JASS:
if IsUnitSelected(u, Player(i)) == true then
to
Collapse JASS:
if IsUnitSelected(u, Player(i)) == false then
then I can block make so it doesn't dies by selecting it.
02-11-2008, 11:27 AM#2
Themerion
Are you not supposed to check if the unit is null? Currently you're checking the group...

Collapse JASS:
//exitwhen (g == null)
//set u = FirstOfGroup(g)

// should be

set u = FirstOfGroup(g)
exitwhen (u == null)

And keep in mind that when you do set g=Towers, you make them the same. This means that removing a unit from g will remove it from Towers, since they are the same group. I think you should do like this:

Collapse JASS:
local group g=CreateGroup()
call GroupAddGroup(Towers,g)

You won't need the g2 group either...

And also, I'm curious why you sometimes null your local variables, and sometimes not.
02-11-2008, 12:52 PM#3
Gwypaas
Now ive done the changes you pointed out so the trigger looks like this:
Collapse JASS:
scope CheckIfSelected
globals
    private group Towers = CreateGroup()
    private real HowOften = 3
    private integer WhatTower = 'h000'
    private rect Region = gg_rct_Region_000 // The region it creates towers in.
endglobals
private function NewTower takes nothing returns nothing
    local real x = GetRandomReal(GetRectMinX(Region), GetRectMaxX(Region))
    local real y = GetRandomReal(GetRectMinY(Region), GetRectMaxY(Region))
    local unit u = CreateUnit(Player(11), WhatTower, x, y , 270.0)
    call GroupAddUnit(Towers,u)
    //call BJDebugMsg("NewTower Running.")
endfunction
private function Actions takes nothing returns nothing
    local group g = CreateGroup()
    local unit u 
    local integer i = 0
    call GroupAddGroup(Towers, g)

    //call BJDebugMsg("Check if unit is selected running.")
    loop
        exitwhen (u == null)
        set u = FirstOfGroup(g)
        loop
            exitwhen (i >=10)
            if IsUnitSelected(u, Player(i)) == true then
                call BJDebugMsg("Kill unit running")
                call GroupRemoveUnit(Towers, u)
                call GroupRemoveUnit(g, u)
                call KillUnit(u)
            endif
            set i = i+1
        endloop
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g = null
endfunction

//===========================================================================
public function InitTrig takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    local trigger CreateTowers = CreateTrigger()
    call TriggerRegisterTimerEvent( Trig, 0.5, true )
    call TriggerAddAction( Trig, function Actions )
    call TriggerRegisterTimerEvent(CreateTowers, HowOften, true)
    call TriggerAddAction( CreateTowers, function NewTower)
endfunction

endscope

But it still doesn't work.

edit: It works now what I didn't do was giving a value to u before the loop started.

edit2: It works fine until the unit group becomes larger. If I wait so 10 towers spawns I can't kill them by selecting
02-11-2008, 12:53 PM#4
Themerion
You have to make sure these two lines are exactly in this order.

Collapse JASS:
set u = FirstOfGroup(g)
exitwhen (u == null)
02-11-2008, 01:08 PM#5
Gwypaas
That works too. But it still bugs up if more then 7-8 towers spawns.
02-11-2008, 02:47 PM#6
Themerion
Quote:
edit: It works now what I didn't do was giving a value to u before the loop started.

Which is exactly why the order of
Collapse JASS:
set u = FirstOfGroup(g)
exitwhen (u == null)
was important :)

Quote:
But it still bugs up if more then 7-8 towers spawns.

I do know that the selection event is somewhat slow, but there should not be such issues...

You could add this to speed things up, but I don't know if it will matter much:
Collapse JASS:
//if IsUnitSelected(u, Player(i)) == true then
        //        call BJDebugMsg("Kill unit running")
        //        call GroupRemoveUnit(Towers, u)
        //        call GroupRemoveUnit(g, u)
        //        call KillUnit(u)
                exitwhen true
//    endif

---------- ------------

By the way, why aren't you just using a selection event?

Collapse JASS:
function Actions takes nothing returns nothing
    if GetUnitTypeId(GetTriggerUnit())=='hhou' then
        call KillUnit( GetTriggerUnit() )
    endif
endfunction

//===========================================================================
function Init takes nothing returns nothing
    local trigger trg = CreateTrigger()
    local integer i=0
    loop
        exitwhen i>=10
        call TriggerRegisterPlayerUnitEvent(trg, Player(i), EVENT_PLAYER_UNIT_SELECTED, null)
        set i=i+1
    endloop
    call TriggerAddAction( trg, function Actions )
endfunction
02-11-2008, 05:22 PM#7
Gwypaas
Thanks that works and it's fast. I'm not really sure about all different events yet that was why I used the IsUnitSelected function.
+rep!

I have 1 other question where do you change so the random numbers isn't constant?
02-11-2008, 06:17 PM#8
MaD[Lion]
somewhere in game interface i suppose ^^
02-11-2008, 06:31 PM#9
Captain Griffen
WE interface somewhere. It's not actually constant ingame except when testing and an option isn't set in WE (unless you go into cinematic mode, in which case it sets it to seed 0, but I think it sets it back after you exit cinematic mode.
02-11-2008, 06:57 PM#10
Gwypaas
I saw my misstake when I set the rect "Region = gg_rct_SomeRegion" in the global it didn't get the values anyways so if I changed to functions to work with the gg_rct it worked.
02-12-2008, 05:33 AM#11
Pyrogasm
The option is called "Use Fixed Random Seed" in the World Editor preference pane.