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: 235

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 The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top