• 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.

[JASS] Trying to condense a really long item-acquire trigger

Status
Not open for further replies.
Level 5
Joined
Sep 19, 2006
Messages
152
I have a really long trigger that basically reads if you acquire item of type X, do Y. However, there are like 200 items and it seems that some sort of function array would be more practical, if I could make it work. Can someone steer me in the right direction?

Old System

JASS:
function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item           = GetManipulatedItem ()
    local integer trig_itemJ      = GetItemTypeId (trig_item)
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_unitQ      = GetPlayerId (GetOwningPlayer (trig_unit))


//  Tarba's Hood

    if trig_itemJ == 'hbth' then
        set udg_PlayerGoldProt [trig_unitQ] = udg_PlayerGoldProt [trig_unitQ] + 1
        set trig_unit = null
        set trig_item = null
        return
    endif
endfunction

"Newish" system (before the trig_itemJ's and f's are assigned as arrays:

JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    set udg_PlayerGoldProt [trig_unitQ] = udg_PlayerGoldProt [trig_unitQ] + V
endfunction

function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item           = GetManipulatedItem ()
    local integer trig_itemJ      = GetItemTypeId (trig_item)
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_unitQ      = GetPlayerId (GetOwningPlayer (trig_unit))
    local string f


//  Tarba's Hood

    if trig_itemJ == 'hbth' then
        set f = "GoldProtection"
        call f (trig_unitQ, 1)
        set trig_unit = null
        set trig_item = null
        set f = null
        return
    endif
endfunction

My ultimate goal is to set (at Initialization) a set of arrays like the following:

set FunctionArray1 ['hbth'] = "GoldProtection"
set IntegerArray2 ['hbth'] = 1

function GoldProtection takes integer trig_unitQ, integer V returns nothing
set udg_PlayerGoldProt [trig_unitQ] = udg_PlayerGoldProt [trig_unitQ] + V
endfunction

function AcquiresAnItem_Action01 takes nothing returns nothing
local item trig_item = GetManipulatedItem ()
local integer trig_itemJ = GetItemTypeId (trig_item)
local unit trig_unit = GetManipulatingUnit ()
local integer trig_unitQ = GetPlayerId (GetOwningPlayer (trig_unit))


call Array1 [trig_itemJ] (trig_unitQ, Array2 [trig_itemJ])

set trig_unit = null
set trig_item = null
endfunction
[/code]
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
i use item level like array index

lets say axe item level is 100, sword item level 110, bow item level 120 then i store at map init everything like this

item_lv_requiment[item level]=x

example

item_lv_requiment[100]=10

when i use a item just 1 if needed

if item_lv_requiment[item level of manipulated item] > herolv then
//do this
endif

but u can use hashtable at map init, key1 lets say the item raw code :p
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
JASS:
function Create takes integer id, code c, integer amount returns nothing
    local trigger action = CreateTrigger()

    call SaveInteger(TABLE, id, AMOUNT_KEY, amount)
    call SaveTriggerHandle(TABLE, id, ACTION_KEY, t)
    call TriggerAddCondition(action, Condition(c))

    set action = null
endfunction

TABLE = InitHashtable()

call Create('hbth', function GoldProtection, 1)

JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    local integer amount = GLOBAL_AMOUNT
    local integer owningPlayerId = GLOBAL_OWNER_ID

    set udg_PlayerGoldProt [owningPlayerId] = udg_PlayerGoldProt [owningPlayerId] + amount
endfunction

function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item           = GetManipulatedItem ()
    local integer trig_itemJ      = GetItemTypeId (trig_item)
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_unitQ      = GetPlayerId (GetOwningPlayer (trig_unit))

    local trigger action = LoadTriggerHandle(TABLE, trig_itemJ, ACTION_KEY)
    local integer amount = LoadInteger(TABLE, trig_itemJ, AMOUNT_KEY)

    set GLOBAL_AMOUNT = amount
    set GLOBAL_OWNER_ID = trig_unitQ

    call TriggerEvaluate(action)

    set action = null
    set trig_item = null
    set trig_unit = null
endfunction
 
Level 5
Joined
Sep 19, 2006
Messages
152
Thank you for the response. I have a few questions.

JASS:
function Create takes integer id, code c, integer amount returns nothing
    local trigger action = CreateTrigger()

    call SaveInteger(TABLE, id, AMOUNT_KEY, amount)
    call SaveTriggerHandle(TABLE, id, ACTION_KEY, t)
    call TriggerAddCondition(action, Condition(c))

    set action = null
endfunction

TABLE = InitHashtable()

call Create('hbth', function GoldProtection, 1)

1. In the above, I'm guessing this appears in the map initialization trigger?
2. In the above, to what does the "t" refer?

JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    local integer amount = GLOBAL_AMOUNT
    local integer owningPlayerId = GLOBAL_OWNER_ID

    set udg_PlayerGoldProt [owningPlayerId] = udg_PlayerGoldProt [owningPlayerId] + amount
endfunction

function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item           = GetManipulatedItem ()
    local integer trig_itemJ      = GetItemTypeId (trig_item)
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_unitQ      = GetPlayerId (GetOwningPlayer (trig_unit))

    local trigger action = LoadTriggerHandle(TABLE, trig_itemJ, ACTION_KEY)
    local integer amount = LoadInteger(TABLE, trig_itemJ, AMOUNT_KEY)

    set GLOBAL_AMOUNT = amount
    set GLOBAL_OWNER_ID = trig_unitQ

    call TriggerEvaluate(action)

    set action = null
    set trig_item = null
    set trig_unit = null
endfunction

Couldn't this
JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    local integer amount = GLOBAL_AMOUNT
    local integer owningPlayerId = GLOBAL_OWNER_ID

    set udg_PlayerGoldProt [owningPlayerId] = udg_PlayerGoldProt [owningPlayerId] + amount
endfunction
be written as the following?
JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    set udg_PlayerGoldProt [trig_unitQ] = udg_PlayerGoldProt [trig_unitQ] + V
endfunction

Can this system be used for multiple assignments for an item, or for only 1 assignment (i.e. set udg_x = udg_x + 3)?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
1. In the above, I'm guessing this appears in the map initialization trigger?

InitHashtable() and the creation of the item data has to be done once before you want to use it, yes.

2. In the above, to what does the "t" refer?

Guess. I just renamed the local trigger variable later on and made a mistake.

Couldn't this
JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    local integer amount = GLOBAL_AMOUNT
    local integer owningPlayerId = GLOBAL_OWNER_ID

    set udg_PlayerGoldProt [owningPlayerId] = udg_PlayerGoldProt [owningPlayerId] + amount
endfunction
be written as the following?
JASS:
function GoldProtection takes integer trig_unitQ, integer V returns nothing
    set udg_PlayerGoldProt [trig_unitQ] = udg_PlayerGoldProt [trig_unitQ] + V
endfunction

Function arguments like you have written can only be passed directly via a call <function>-line. The function you want to run is determined dynamically by TriggerEvaluate. You cannot pass arguments via this function that it would again need to delegate to functions run by the trigger object. And that's why I have transfered the data indirectly using globals. I have also forgotten this, functions run by triggers are not allowed to take any arguments at all, so change head to
function GoldProtection takes nothing returns nothing

Can this system be used for multiple assignments for an item, or for only 1 assignment (i.e. set udg_x = udg_x + 3)?

Well, would it not be more senceful to split the creation line then in order to optionally add more values to the items.

Either you combine all assignments in one function

JASS:
function ApplyAssignments takes nothing returns nothing
    local integer GoldProtectionAdd = GetItemData_GoldProtectionAdd(PASSED_ITEM_TYPE_ID)
    local integer xAdd = GetItemData_xAdd(PASSED_ITEM_TYPE_ID)
    local integer yAdd = GetItemData_yAdd(PASSED_ITEM_TYPE_ID)
    ...

    set GoldProtection = GoldProtection + GoldProtectionAdd
    set x = x + xAdd
    set y = y + yAdd
    ...
endfunction

and store default zeroes on GoldProtectionAdd, xAdd, yAdd, ... for each item. Then you actually do not need the dynamic function call at all.

Or you have the assignments separate and add multiples of them/multiple function to run and parameters to your items.

JASS:
function ApplyGoldProtection takes nothing returns nothing
    local integer GoldProtectionAdd = GetItemData_GoldProtectionAdd(PASSED_ITEM_TYPE_ID)

    set GoldProtection = GoldProtection + GoldProtectionAdd
endfunction

function Applyx takes nothing returns nothing
    local integer xAdd = GetItemData_xAdd(PASSED_ITEM_TYPE_ID)

    set x = x + xAdd
endfunction

function Applyy takes nothing returns nothing
    local integer yAdd = GetItemData_yAdd(PASSED_ITEM_TYPE_ID)

    set y = y + yAdd
endfunction

...

Yeah, just pass the item type and retrieve the data in the target function for simplification.
 
Level 5
Joined
Sep 19, 2006
Messages
152
Hmm. Are ACTION_KEY and AMOUNT_KEY some kind of manufactured global? If so, what do I set them to initially? I can't get the following lines to clear (expected a name):
JASS:
    local trigger action = LoadTriggerHandle(TABLE, trig_itemJ, ACTION_KEY)
    local integer amount = LoadInteger(TABLE, trig_itemJ, AMOUNT_KEY)
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
It was not meant to be copied but understood in concepts. The hashtables in wc3 store a value under two integer keys. I decided to use the item type id as the first key and the second represents different attributes for each item. You can freely set ACTION_KEY and AMOUNT_KEY but they should not be the same in order to not overwrite each other. The JNGP-editor provides a functionality "key"

JASS:
globals
    key ABC
endglobals

That declares a constant integer variable and each key gets another value.
 
Level 5
Joined
Sep 19, 2006
Messages
152
Bah, I knew that. Thank you for clearing it up. However, the "call ItemProperties" line in my map initialization is returning an "expected function name" error. Any suggestions? (I really appreciate your patience.)

JASS:
function ItemProperties takes integer itemJ, code c, integer itemV returns nothing
    local trigger T                       = CreateTrigger ()

    call SaveInteger (udg_ItemHash, itemJ, 2, itemV)
    call SaveTriggerHandle (udg_ItemHash, itemJ, 1, T)
    call TriggerAddCondition (T, Condition (c))
    set T = null
endfunction

function Initialization_Action01 takes nothing returns nothing
    set udg_ItemHash                = InitHashtable ()
    set udg_ItemValue               = 0
    set udg_MasterHash              = InitHashtable ()
    set udg_PlayerNumber            = 0

    call ItemProperties ('hbth', function GoldProtection, 1)  //  Tarba's Hood
endfunction

function InitTrig_Initialization takes nothing returns nothing
    set gg_trg_Initialization = CreateTrigger ()
    call TriggerAddAction (gg_trg_Initialization, function Initialization_Action01)
endfunction

JASS:
function GoldProtection takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerGoldProt [playerQ] = udg_PlayerGoldProt [playerQ] + itemV
endfunction

function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item             = GetManipulatedItem ()
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_itemD         = GetItemUserData (trig_item)
    local itemtype trig_itemT        = GetItemType (trig_item)
    local integer trig_unitQ         = GetPlayerId (GetOwningPlayer (trig_unit))
    local integer trig_itemE         = GetItemLevel (trig_item)
    local integer trig_itemC         = GetItemCharges (trig_item)
    local integer trig_itemJ         = GetItemTypeId (trig_item)
    local trigger trig_itemA         = LoadTriggerHandle (udg_ItemHash, trig_itemJ, 1)
    local integer trig_itemV         = LoadInteger (udg_ItemHash, trig_itemJ, 2)
    local integer P_HeroCode         = udg_PlayerHero_Code [trig_unitQ]

    set udg_ItemValue = trig_itemV
    set udg_PlayerNumber = trig_unitQ
    call TriggerEvaluate (trig_itemA)
    set trig_itemA = null

endfunction

function InitTrig_AcquiresAnItem takes nothing returns nothing
    set gg_trg_AcquiresAnItem = CreateTrigger ()
    call DisableTrigger (gg_trg_AcquiresAnItem)
    call TriggerAddAction (gg_trg_AcquiresAnItem, function AcquiresAnItem_Action01)
endfunction
 
Level 5
Joined
Sep 19, 2006
Messages
152
So, move the following function into the initialization trigger?!

JASS:
function GoldProtection takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerGoldProt [playerQ] = udg_PlayerGoldProt [playerQ] + itemV
endfunction
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
So, move the following function into the initialization trigger?!

JASS:
function GoldProtection takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerGoldProt [playerQ] = udg_PlayerGoldProt [playerQ] + itemV
endfunction

or to map header.

when u get "expected function name" error then its mean or u wrote wrong function name or function dont was declared before you already called it.

i noticed functions are declared in order, same triggers
Triggers:
Map Header
Map init1
Map init2
Map Init3
Another functions

i mean with this important the function order inside your map header or in triggers also triggers will be compiled in order (from top to bottom).

i think u can move it map header because map header got the 1st priority in declaring functions, also what u want call more times during the game, u can put to map header :)
 
Level 17
Joined
Nov 13, 2006
Messages
1,814

Attachments

  • 1.jpg
    1.jpg
    72.4 KB · Views: 90
Level 26
Joined
Aug 18, 2009
Messages
4,097
Nothing should be in the root. Rather make a folder, name it "Header" and place it above other folders. This gives you the opportunity to structure your header rather than stuffing everything in one text box. In normal editor, you cannot even use the search function there. You have to reload the map though to get your triggers sorted according to the structure on the left (which can be avoided however with some features of vJass).

It would also advise to take proper variable names (what does itemJ/itemV stand for?). In a scope that only deals with items, you can leave out "item" altogether. That's only redundant.
 
Level 5
Joined
Sep 19, 2006
Messages
152
1. I made a folder called Header and placed it above the Initialization folder. In that folder I created a "blank" trigger, named it "Map Start," and copied into it the "GoldProtection" function provided above. However, the editor is telling me that a init_initialization blah, blah must be applied to that blank trigger.

2. I use things like itemJ, etc. because I've found that using the search feature for something like J can be very frustrating, as it finds every single J everywhere. In addition, itemJ or trig_unitX tell me at a glance to what that variable is referring.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
1. JGNP has a feature to avoid that. Else make an empty function

JASS:
function InitTrig_<triggerPageName> takes nothing returns nothing
endfunction

Special characters are replaced by underscores.

2. Yeah, because just "J" lacks meaning. I guess the V in itemV stands for value, so name the variable value or val. The scope implicits it has to do with items, so the "item" prefix does not add more information. You should also approach JNGP and vJass to acquire better techniques of organizing your code.
 
Level 5
Joined
Sep 19, 2006
Messages
152
Yeah, I caught that one after I posted it. Still, it doesn't seem to be working. I'm using DeBug messages right now to try to isolate the problem(s).
 
Level 5
Joined
Sep 19, 2006
Messages
152
I think it's finally working! I have the function information in the map initialization trigger for now, so I'll have to remedy that in the future; but otherwise, all is good. Thanks to you all for your help and patience.
 
Level 5
Joined
Sep 19, 2006
Messages
152
Can you show what you have now?

Indeed I can! Here is an abridged version of the triggers. (I am still in the process of replacing the bj arrays with udg's. Also, ignore the lack of an EVENT in the "acquires an item" trigger.)
I did post a new thread concerning the "acquires an item" trigger; by all means, take a gander.

JASS:
function Initialization_FilterAction01 takes nothing returns nothing
    local destructable filt_unit     = GetEnumDestructable ()
    local integer filt_unitJ         = GetDestructableTypeId (filt_unit)
//
    if filt_unitJ == 'DTlv' then
        call TriggerRegisterDeathEvent (gg_trg_ChestsDeath, filt_unit)
    elseif filt_unitJ == 'LTcr' then
        call TriggerRegisterDeathEvent (gg_trg_CratesDeath, filt_unit)
    endif
    set filt_unit = null
endfunction

function ChanceItemBoost takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerChance_ItemBoost [playerQ] = udg_PlayerChance_ItemBoost [playerQ] + itemV
endfunction

function GoldProtection takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerGoldProt [playerQ] = udg_PlayerGoldProt [playerQ] + itemV
endfunction

function ItemSuncatchers takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local unit hero_unit             = udg_ItemUnit
    local integer itemV              = udg_ItemValue

    set udg_PlayerItem_Suncatchers [playerQ] = udg_PlayerItem_Suncatchers [playerQ] + itemV
    if udg_TimeIsDay == true then
        call SetUnitAbilityLevel (hero_unit, 'A03V', 2)
    endif
    set hero_unit = null
endfunction

function LevelupDamage takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set bj_randDistID [playerQ + 2520] = bj_randDistID [playerQ + 2520] + itemV
endfunction

function LevelupLife takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set bj_randDistID [playerQ + 2500] = bj_randDistID [playerQ + 2500] + itemV
endfunction

function LevelupMana takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set bj_randDistID [playerQ + 2540] = bj_randDistID [playerQ + 2540] + itemV
endfunction

function VitalityDrain takes nothing returns nothing
    local integer playerQ            = udg_PlayerNumber
    local integer itemV              = udg_ItemValue

    set udg_PlayerVitality_Drain [playerQ] = udg_PlayerVitality_Drain [playerQ] + itemV
endfunction

function ItemProperties takes integer itemJ, code c, integer itemV returns nothing
    local trigger T                       = CreateTrigger ()

    call SaveTriggerHandle (udg_ItemHash, itemJ, 0, T)
    call SaveInteger (udg_ItemHash, itemJ, 1, itemV)
    call SaveInteger (udg_ItemHash, itemJ, 2, itemV * -1)
    call TriggerAddCondition (T, Condition (c))
    set T = null
endfunction

function Initialization_Action01 takes nothing returns nothing
    set udg_ItemHash                = InitHashtable ()
    set udg_ItemUnit                = null
    set udg_ItemValue               = 0


//  Assign Item Functions

    call ItemProperties ('fgsk', function ChanceItemBoost,  3)  //  Blessings Cape
    call ItemProperties ('mnst', function ChanceItemBoost,  4)  //  Blessings Cape
    call ItemProperties ('bgst', function LevelupLife,     30)  //  Brilliancy Band
    call ItemProperties ('pinv', function LevelupLife,      6)  //  Chain of Stalwart
    call ItemProperties ('ram1', function VitalityDrain,   10)  //  Crooked Eye Charm
    call ItemProperties ('pghe', function LevelupLife,     15)  //  Jade Ring of Mysticism
    call ItemProperties ('rhe2', function LevelupLife,      1)  //  Magical Tome
    call ItemProperties ('stwa', function LevelupLife,      1)  //  Magical Tome
    call ItemProperties ('rde2', function GoldProtection,   1)  //  Pendant of Sanctuary
    call ItemProperties ('sfog', function GoldProtection,   1)  //  Shimmering Crystal Fragment
    call ItemProperties ('schl', function GoldProtection,   1)  //  Shimmering Crystal Fragment
    call ItemProperties ('rnsp', function GoldProtection,   1)  //  Shimmering Crystal Fragment
    call ItemProperties ('mcou', function VitalityDrain,    1)  //  Sickstone Fragment
    call ItemProperties ('sman', function VitalityDrain,    1)  //  Sickstone Fragment
    call ItemProperties ('mlst', function VitalityDrain,    1)  //  Sickstone Fragment
    call ItemProperties ('tlum', function LevelupDamage,    1)  //  Stone of Aggression
    call ItemProperties ('rspl', function LevelupDamage,    3)  //  Stone of Aggression
    call ItemProperties ('totw', function LevelupLife,      2)  //  Strength of Titus Helmet
    call ItemProperties ('sror', function LevelupLife,      2)  //  Strength of Titus Helmet
    call ItemProperties ('wswd', function LevelupLife,      2)  //  Strength of Titus Helmet
    call ItemProperties ('rre1', function ItemSuncatchers,  1)  //  Suncatchers
    call ItemProperties ('hbth', function GoldProtection,   1)  //  Tarba's Hood


//  call DestroyTrigger (gg_trg_Initialization)
endfunction

function InitTrig_Initialization takes nothing returns nothing
    set gg_trg_Initialization = CreateTrigger ()
    call TriggerAddAction (gg_trg_Initialization, function Initialization_Action01)
endfunction

JASS:
function AcquiresAnItem_Action01 takes nothing returns nothing
    local item trig_item             = GetManipulatedItem ()
    local unit trig_unit             = GetManipulatingUnit ()
    local integer trig_unitQ         = GetPlayerId (GetOwningPlayer (trig_unit))
    local integer trig_itemJ         = GetItemTypeId (trig_item)
    local trigger trig_itemA         = LoadTriggerHandle (udg_ItemHash, trig_itemJ, 0)
    local integer trig_itemV         = LoadInteger (udg_ItemHash, trig_itemJ, 1)


    set udg_ItemValue = trig_itemV
    set udg_ItemUnit = trig_unit
    set udg_PlayerNumber = trig_unitQ
    call TriggerEvaluate (trig_itemA)
    set trig_itemA = null
    set trig_item = null
    set trig_unit = null
endfunction

function InitTrig_AcquiresAnItem takes nothing returns nothing
    set gg_trg_AcquiresAnItem = CreateTrigger ()
//  call DisableTrigger (gg_trg_AcquiresAnItem)
    call TriggerAddAction (gg_trg_AcquiresAnItem, function AcquiresAnItem_Action01)
endfunction
 
Status
Not open for further replies.
Top