System Attach (Item Attachment Management System)

Reaction score
341
Attach v0.01
TriggerHappy187


If it weren't for the (Item Attachment Management System) in the title you would've came here thinking this was another Struct/Timer attachment system. Well , its not even close to one. But then what is Attach? It's basically an item attachment management system. Have you ever wanted to create a Role Playing Game that using item attachments but you had two heroes with different attacking hands.

For Example , you attach a sword to the villager and he attacks with his left hand. Then the orc picks up the same sword but he attacks with his right hand. This system can specify what hand is the units weapon and which is his offhand easily.

Demo Usage

Trigger:
  • AS Grab Item
    • Events
      • Unit - A unit Acquires an item
    • Conditions
    • Actions
      • Custom script: local Attach a
      • Custom script: if IsUnitRegistered(GetTriggerUnit()) == true then
      • Custom script: call AttachEffect(GetTriggerUnit(), GetManipulatedItem())
      • Custom script: else
      • Custom script: call a.create(GetTriggerUnit())
      • Custom script: call AttachEffect(GetTriggerUnit(), GetManipulatedItem())
      • Custom script: endif


Other Uses

Setting Items
Trigger:
  • AS Set Items
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • -------- Claws of attack --------
      • Custom script: call AddItemEffect('ratf',"Objects\\InventoryItems\\HumanCaptureFlag\\HumanCaptureFlag.mdl", "weapon")


Dropping Items
Trigger:
  • AS Drop Item
    • Events
      • Unit - A unit Loses an item
    • Conditions
    • Actions
      • Custom script: local Attach a
      • Custom script: if IsUnitRegistered(GetTriggerUnit()) == true then
      • Custom script: call DettachEffect(GetTriggerUnit(), GetManipulatedItem())
      • Custom script: else
      • Custom script: call a.create(GetTriggerUnit())
      • Custom script: call DettachEffect(GetTriggerUnit(), GetManipulatedItem())
      • Custom script: endif

Source Code :

JASS:
library Attach

globals
    private integer COUNT = 0
    private integer ITEM_COUNT = 0
    private integer array ITEM
    private string array ITEM_TYPE
    private string array ITEM_EFFECT
endglobals

struct Attach
    unit u
    string weapon       = "left hand"
    string offhand      = "right hand"
    effect cur_weapon
    effect cur_offhand
    
    static method create takes unit u returns Attach
        local Attach a = Attach.allocate()
        set a.u = u
        set COUNT = COUNT + 1
        return a
    endmethod
    
endstruct

    function IsUnitRegistered takes unit u returns boolean
        local integer i = 0
        local Attach a
        loop
            exitwhen i > COUNT
            set a = i
            if a.u == u then
                return true
            endif
            set i = i + 1
        endloop   
        return false
    endfunction
    
    function SetWeapon takes unit u, string weapon returns nothing
        local integer i = 0
        local Attach a
        loop
            exitwhen i > COUNT
            set a = i
            if a.u == u then
                set a.weapon = weapon
            endif
            set i = i + 1
        endloop
    endfunction
    
    function SetOffhand takes unit u, string offhand returns nothing
        local integer i = 0
        local Attach a
        loop
            exitwhen i > COUNT
            set a = i
            if a.u == u then
                set a.weapon = offhand
            endif
            set i = i + 1
        endloop
    endfunction
    
    function AttachEffect takes unit u, item itm returns nothing
        local integer i = 0
        local integer i2 = 0
        local Attach a
        loop
            exitwhen i > COUNT
            set a = i
            if a.u == u then
                loop
                    exitwhen i2 > COUNT
                    if GetItemTypeId(itm) == ITEM[i2] then
                        if ITEM_TYPE[i2] == "weapon" then
                            set a.cur_weapon = AddSpecialEffectTarget(ITEM_EFFECT[i2], a.u, a.weapon)
                        elseif ITEM_TYPE[i2] == "offhand" then
                            set a.cur_offhand = AddSpecialEffectTarget(ITEM_EFFECT[i2], a.u, a.offhand)
                        endif
                    endif
                    set i2 = i2 + 1
                endloop
            endif
            set i = i + 1
        endloop
    endfunction
    
    function DettachEffect takes unit u, item itm returns nothing
        local integer i = 0
        local integer i2 = 0
        local Attach a
        loop
            exitwhen i > COUNT
            set a = i
            if a.u == u then
                loop
                    exitwhen i2 > COUNT
                    if GetItemTypeId(itm) == ITEM[i2] then
                        if ITEM_TYPE[i2] == "weapon" then
                            call DestroyEffect(a.cur_weapon)
                        elseif ITEM_TYPE[i2] == "offhand" then
                            call DestroyEffect(a.cur_offhand)
                        endif
                    endif
                    set i2 = i2 + 1
                endloop
            endif
            set i = i + 1
        endloop
    endfunction

    function SetItemEffect takes integer itm, string fx returns nothing
        local integer i = 0
        loop
            exitwhen i > ITEM_COUNT
            if itm == ITEM<i> then
                set ITEM<i> = itm
                set ITEM_EFFECT<i> = fx
            endif
            set i = i + 1
        endloop
    endfunction

    function AddItemEffect takes integer itm, string fx, string attachtype returns nothing
        set ITEM[ITEM_COUNT] = itm
        set ITEM_EFFECT[ITEM_COUNT] = fx
        if attachtype == &quot;weapon&quot; then
            set ITEM_TYPE[ITEM_COUNT] = attachtype
        elseif attachtype == &quot;offhand&quot; then
            set ITEM_TYPE[ITEM_COUNT] = attachtype
        endif
        set COUNT = COUNT + 1
    endfunction

endlibrary
</i></i></i>
 

Attachments

  • Attach v0.01.w3x
    10.4 KB · Views: 226

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
When is the struct even destroyed? When are the structs ever recycled?
 

Flare

Stops copies me!
Reaction score
662
Hmmm
JASS:
        loop
            exitwhen i &gt; COUNT
            set a = i
            if a.u == u then
//sdgsbfdsghdfsgjhgsg
                loop
                    exitwhen i2 &gt; COUNT
                    if GetItemTypeId(itm) == ITEM[i2] then
                        if ITEM_TYPE[i2] == &quot;weapon&quot; then
                            set a.cur_weapon = AddSpecialEffectTarget(ITEM_EFFECT[i2], a.u, a.weapon)
                        elseif ITEM_TYPE[i2] == &quot;offhand&quot; then
                            set a.cur_offhand = AddSpecialEffectTarget(ITEM_EFFECT[i2], a.u, a.offhand)
                        endif
                    endif
                    set i2 = i2 + 1
                endloop
            endif
            set i = i + 1
        endloop

The inner loop won't work - once the first iteration of the outer loop is complete, i2 will be (COUNT + 1), so the condition (i2 > COUNT) will always be false, so the loop will never be handled anymore - set i2 to 0 before the start of your second loop (where the commented line is)

EDIT: On further inspection, that'll only apply if you .create for the same unit twice - still, it's better to be safe than sorry

JASS:
    function AddItemEffect takes integer itm, string fx, string attachtype returns nothing
        set ITEM[ITEM_COUNT] = itm
        set ITEM_EFFECT[ITEM_COUNT] = fx
        if attachtype == &quot;weapon&quot; then
            set ITEM_TYPE[ITEM_COUNT] = attachtype
        elseif attachtype == &quot;offhand&quot; then
            set ITEM_TYPE[ITEM_COUNT] = attachtype
        endif
        set COUNT = COUNT + 1
    endfunction

Shouldn't you be incrementing ITEM_COUNT, and not COUNT there?

(example-related, so it's not overly important)
Trigger:
  • Custom script: local Attach a
    • Custom script: if IsUnitRegistered(GetTriggerUnit()) == true then
    • Custom script: call DettachEffect(GetTriggerUnit(), GetManipulatedItem())
    • Custom script: else
    • Custom script: call a.create(GetTriggerUnit())
    • Custom script: call DettachEffect(GetTriggerUnit(), GetManipulatedItem())
    • Custom script: endif

Hmmm - if the unit isn't registered with the system, it shouldn't have an effect to begin with? That, and creating data for the unit won't magically give it an effect to be removed :p

Will be back shortly with more stuff, it's much easier to read code in NewGen than the JASS tag :p

EDIT: Also, this would be much better (efficiency-wise and just for simplifying the code a bit) if you used PUI and either (a) a PUI variant modified for use with items (would physically require the items to be present on map for it to work) or (b) a gamecache so you could store values by the item ID e.g.
JASS:
call StoreString (myCache, I2S (itemId), weaponString, modelFile)
//The above is best with 2 dynamic keys, since you want to be able to acquire both main and off-hand modelfiles
//This one (below) needs one static key (e.g. &quot;weaponslot&quot;)
call StoreString (myCache, I2S (itemId), &quot;weaponslot&quot;, weaponString)
...
set whichHand = GetStoredString (myCache, I2S (itemId), &quot;weaponslot&quot;)
if whichHand == &quot;weapon&quot; then
  set a.cur_weapon = AddSpecialEffectTarget (GetStoredString (myCache, I2S (itemId), &quot;weapon&quot;), whichUnit, attachPoint
//And just set a few vars to reduce all those calls, like the I2S itemId

(the gamecache may be slow, but it's probably favourable to repeated O(n) searches, and it's absolutely ideal for storing those values by item ID's)

EDIT: Also, why do you try and go out of your way to make your systems GUI friendly? vJASS systems are rarely going to be GUI user friendly (especially if you have structs, textmacros, or that kinda stuff) - if you're going to make a JASS system, you should probably aim for making it as JASS user-friendly as possible before moving onto GUI user-friendly, since it's never going to be really GUI friendly, no matter how hard you try
 

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
At first I thought it was some item indexing/attaching system that would help me create manipulation with items. Why not make such one in addition? This could help in making of item mute or stuff like that.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top