HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Trigger(JASS) help

01-09-2006, 12:26 AM#1
PCPharaoh
Collapse JASS:
function Matching takes nothing returns boolean
    return ( IsUnitType(GetTriggerUnit(), UNIT_TYPE_SUMMONED) == true )
endfunction

function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
    local group g = GetUnitsOfPlayerMatching(GetTriggerPlayer(), Condition(function Matching))
    local unit u
    local string s = SubStringBJ(GetEventPlayerChatString(), 10, StringLength(GetEventPlayerChatString()))
    if S2I(s) >= 0 and S2I(s) <= 100 then
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            if GetUnitLifePercent(u) <= S2R(s) then
                call KillUnit(u)
            endif
            call GroupRemoveUnit(g, u)
        endloop
    else
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            if s == GetUnitName(u) then
                call KillUnit(u)
            endif
            call GroupRemoveUnit(g, u)
        endloop
    endif
    call DestroyGroup(g)
    set g = null
endfunction

//===========================================================================
function InitTrig_Destroy takes nothing returns nothing
    set gg_trg_Destroy = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(0), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(1), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(2), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(3), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(4), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(5), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(6), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(7), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(8), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(9), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(10), "-Destroy", false )
    call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(11), "-Destroy", false )
    call TriggerAddAction( gg_trg_Destroy, function Trig_Untitled_Trigger_001_Actions )
endfunction

Alright, that's the entire code. What is supposed to happen is a player types "-destroy (Unit Name)" or "-destroy (integer)". If the person specifies an integer, the trigger kills all units with a health percentage at or below the specified value. That part works perfectly. If a person specifies a unit name, the trigger is supposed to kill all units of that type owned by the triggering player. The problem is, the unit-name part doesn't work, whatsoever. So any help I can get would be greatly appreciated.
01-09-2006, 12:58 AM#2
Vexorian
Check case sensitiveness, could be that the player is typing footman and the unit is called Footman , you can convert both strigns to lower case before comparing them
01-09-2006, 01:22 AM#3
PCPharaoh
Nope, that's not the solution. I also checked typing in the COde, which also didn't work...
01-09-2006, 01:29 AM#4
Vexorian
Add
Collapse JASS:
call BJDebugMsg("s=["+s+"]")
call BJDebugMsg("s=["+GetUnitName(u)+"]")

before the comparission so you can see what's the problem
01-09-2006, 01:58 AM#5
PCPharaoh
So the unit group, g, is empty... in other words,
Collapse JASS:
function Matching takes nothing returns boolean
    return ( IsUnitType(GetTriggerUnit(), UNIT_TYPE_SUMMONED) == true )
endfunction

function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
    local group g = GetUnitsOfPlayerMatching(GetTriggerPlayer(), Condition(function Matching))
doesn't work. Any idea why? I can't figure it out...

EDIT: And earlier I said the kill units by % health part was working... I guess I lied. :P
01-09-2006, 02:43 AM#6
PCPharaoh
So I rebuilt the trigger in GUI, and the f***ing thing still doesn't work... well, the integer part does, but the destroy unit by unit-type doesn't work. Here's the GUI trig:

Trigger:
DestoyGUI
Collapse Events
Player - Player 1 (Red) types a chat message containing -destroy as A substring
Conditions
Collapse Actions
Set unitgroup = (Units owned by (Triggering player) matching (((Matching unit) is Summoned) Equal to True))
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
((Integer((Substring((Entered chat string), 10, (Length of (Entered chat string)))))) Greater than or equal to 0) and ((Integer((Substring((Entered chat string), 10, (Length of (Entered chat string)))))) Less than or equal to 100)
Collapse Then - Actions
Collapse Unit Group - Pick every unit in unitgroup and do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Percentage life of (Picked unit)) Less than or equal to (Real((Substring((Entered chat string), 10, (Length of (Entered chat string))))))
Collapse Then - Actions
Unit - Kill (Picked unit)
Collapse Else - Actions
Do nothing
Collapse Else - Actions
Collapse Unit Group - Pick every unit in unitgroup and do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Substring((Entered chat string), 10, (Length of (Entered chat string)))) Equal to (Name of (Picked unit))
Collapse Then - Actions
Unit - Kill (Picked unit)
Collapse Else - Actions
Do nothing
01-09-2006, 04:20 AM#7
PCPharaoh
Alright, I found the problem :D (rep myself. )

Apparently, the first If Condition, where it says if Integer(substring(entered chat string))) etc.... If the String isn't a integer, it returns 0... so the then would ALWAYS occur.

So I just changed the >= 0 to a > 0, and it should be working. Going to test right now.

And that didn't work... this trigger is turning into a real pain in the arse... More debugging I guess :/

EDIT: Alright, I think I found the final problem with the command. The units I'm trying to delete are named "001 Carrion Beetle" "002 Skeleton" etc, makes them be in order by their level in object editor... So I need a way around that...

EDIT2: Well, the GUI trigger works absolutely perfectly now. Now all I have to do is optimize/de-leak it. Thanks for your help Vex. One of the problems with the trigger was case-sensitivity, so I used your convert to lowercase idea.
01-09-2006, 05:21 AM#8
aaero
If your naming schema is consistent, then you could do a string comparison check to see if the first two characters are 00, or maybe just 0. This would mean that -destroy 01 wouldn't work (they'd have to just type -destroy 1) but other than that it should always works perfectly. If that wouldn't work, you could always

a) make two separate commands, -destroyn [number] and -destroyu [unitname]

b) always run both sets of actions. As long as you don't have any units whose name is just an integer you would be ok. so instead of

Collapse JASS:
if (S2I) > 0 then
   // destroy some number of units
else
  // destroy all units with the given name
end if

make it
Collapse JASS:
if (S2I) > 0 then
   // destroy some number of units
endif

// destroy all units with the given name

c) change your naming schema on the units


Also, just a pet peeve, but I think it's cleaner to have

Collapse JASS:
function InitTrig_Destroy takes nothing returns nothing
    local int i = 0
    set gg_trg_Destroy = CreateTrigger(  )
    loop
         exitwhen i > 11
         call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(i), "-Destroy", false )
    endloop
    call TriggerAddAction( gg_trg_Destroy, function Trig_Untitled_Trigger_001_Actions )
endfunction

That's equivalent to what you had before. It's actually slightly worse from an efficiency standpoint, but the difference is pretty minute. You don't have to use this if you don't want to of course.
01-09-2006, 09:47 PM#9
PCPharaoh
Quote:
Originally Posted by aaero
Also, just a pet peeve, but I think it's cleaner to have

Collapse JASS:
function InitTrig_Destroy takes nothing returns nothing
    local int i = 0
    set gg_trg_Destroy = CreateTrigger(  )
    loop
         exitwhen i > 11
         call TriggerRegisterPlayerChatEvent( gg_trg_Destroy, Player(i), "-Destroy", false )
    endloop
    call TriggerAddAction( gg_trg_Destroy, function Trig_Untitled_Trigger_001_Actions )
endfunction

That's equivalent to what you had before. It's actually slightly worse from an efficiency standpoint, but the difference is pretty minute. You don't have to use this if you don't want to of course.

Heh, didn't know you could do that in Jass. Learn something new every day. If it's more efficient, I think I'll stick with what I have now, then. And I already got the trigger working, but thanks for your help.