HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Can someone Check my code.

08-02-2006, 09:16 AM#1
n13astra
What ive tried to do is create a dummy unit which follows the main unit. Its invisible, cant be attacked and can carry items. Anyways, i have this function which adds the targeted item of the cast ability and gives it to the dummy unit. The problem im having is this:

SOMETIMES!!! when i use the ability during an online game, it does not work and the item that was targeted is lost. i have an array of size 60 (udg_Item_Book_Inventory) of type "item-type". It is size 60 because we have 10 players and each dummy unit can carry 6 items, hence 10 * 6 = 60.

I cant figure out whats going wrong. Can someone have a look at the code for me, and maybe even look at my map.

Thanks again.

This Code adds an item.
Collapse JASS:
function Trig_Add_Weapon_Conditions takes nothing returns boolean
    if (not(GetSpellAbilityId() == 'A04G')) then
        return false
    endif
    return true
endfunction

function Trig_Add_Weapon_Actions takes nothing returns nothing
    local integer in_index = 0
    local integer in_i = 0

    if (udg_Item_Book_Assimilate_Unit[GetConvertedPlayerId(GetOwningPlayer(GetSpellAbilityUnit()))] == null) then
        call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), "|cffff0000ERROR (001)|r - Assimilate System.")
        call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), "Please Report |cffff0000ERRORCODE (001)|r to http://btanksmg.no-ip.org")
    endif

    if (GetItemType(GetSpellTargetItem()) == ITEM_TYPE_ARTIFACT) then
        if (UnitHasItem(GetSpellAbilityUnit(), GetSpellTargetItem()) == true ) then
            set in_i = 1
            loop
                exitwhen(in_i > 6)
                set in_index = (((GetConvertedPlayerId(GetOwningPlayer(GetSpellAbilityUnit())) - 1 ) * 6 ) + in_i)
                if (udg_Item_Book_Inventory[in_index] == GetItemTypeId(null)) then
                    set udg_Item_Book_Inventory[in_index] = GetItemTypeId(GetSpellTargetItem())
                    call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), (( "|c00ffff00" + GetItemName(GetSpellTargetItem())) + "|r has been assimilated."))
                    call UnitAddItemByIdSwapped(GetItemTypeId(GetSpellTargetItem()), udg_Item_Book_Assimilate_Unit[GetConvertedPlayerId(GetOwningPlayer(GetSpellAbilityUnit()))])
                    call RemoveItem(GetSpellTargetItem())
                    return
                else
                    if (in_i == 6) then
                        call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), "TRIGSTR_014")
                        call SetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G'), (GetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G')) + 1))
                    else
                    endif
                endif
                set in_i = in_i + 1
            endloop
        else
            call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), "TRIGSTR_512" )
            call SetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G'), (GetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G')) +1))
        endif
    else
        call DisplayTextToForce(GetForceOfPlayer(GetOwningPlayer(GetSpellAbilityUnit())), "TRIGSTR_539" )
        call SetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G'), (GetItemCharges(GetItemOfTypeFromUnitBJ(GetSpellAbilityUnit(), 'I02G')) + 1))
    endif
endfunction

//===========================================================================
function InitTrig_Add_Weapon takes nothing returns nothing
    set gg_trg_Add_Weapon = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_Add_Weapon, EVENT_PLAYER_UNIT_SPELL_CAST)
    call TriggerAddCondition(gg_trg_Add_Weapon, Condition(function Trig_Add_Weapon_Conditions))
    call TriggerAddAction(gg_trg_Add_Weapon, function Trig_Add_Weapon_Actions)
endfunction

This code is run on map init, which creates the dummy unit for each playing player.
Collapse JASS:
function Trig_Create_Assimilate_Unit_Actions takes nothing returns nothing
    local integer in_i
    
    set in_i = 1
    loop
        exitwhen(in_i > 10)
        if (GetPlayerSlotState(ConvertedPlayer(in_i)) == PLAYER_SLOT_STATE_PLAYING) then
            call CreateNUnitsAtLoc( 1, 'h01B', ConvertedPlayer(in_i), GetRectCenter(gg_rct_Dead_Assim_Unit), bj_UNIT_FACING )
            set udg_Item_Book_Assimilate_Unit[in_i] = GetLastCreatedUnit()
        else
            set udg_Item_Book_Assimilate_Unit[in_i] = null
        endif
        set in_i = in_i + 1
    endloop
    call DestroyTrigger(GetTriggeringTrigger())
endfunction

//===========================================================================
function InitTrig_Create_Assimilate_Unit takes nothing returns nothing
    set gg_trg_Create_Assimilate_Unit = CreateTrigger(  )
    call DisableTrigger( gg_trg_Create_Assimilate_Unit )
    call TriggerAddAction( gg_trg_Create_Assimilate_Unit, function Trig_Create_Assimilate_Unit_Actions )
endfunction
08-02-2006, 09:32 AM#2
PipeDream
I don't see why it shouldn't work online. However I can't read it closely at all, 6 indentations in is insanity. Try breaking it up into smaller chunks which are each individually understandable. If there's a problem you should find it. Worst case is your code becomes maintainable when someone reports a bug in it two weeks from now.