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

[Solved] problem w indexing

Status
Not open for further replies.
hello i have a problem w indexing. its supposed to store the struct integer into the Table. i need this asap hopefully someone could find my mistake. the problem is that it displays the correct index values when storing
here it is

the first loop through both of the bjdebugmsg's that have the 'ratc' in it display 1 / 1 then when next recipe is added it displays 2 / 2 which doesnt make sense since i never change the first ones value...... ?

JASS:
loop
                exitwhen L > 5
                set index = 0
                if i[L] != 0 then
                    set this.ttlItemsForRecipe = this.ttlItemsForRecipe + 1
                    set index = 0
                    set index = indexTable[ i[L]]
                    if index > 0 then
                        set index = index + 1
                        set recipeTable[ i[L]][index] = this
                        set indexTable[ i[L]] = index
                    else
                        set index = 1
                        set recipeTable[ i[L]][index] = this
                        set indexTable[ i[L]] = index
                    endif
                endif
                set L = L + 1
            endloop
            call BJDebugMsg( I2S( recipeTable[ 'ratc'][ 1]))
            call BJDebugMsg( I2S( recipeTable[ 'ratc'][ 2]))

when i try to call this it should display 1 then 2 but it displays 2 and 2 can anyone tell me what im doing wrong ?
note: it does display the correct value for the struct but it seems to somehow overwrite the first index w the next struct.
JASS:
                    call BJDebugMsg( I2S( recipeTable[ id][ 1]))
                    call BJDebugMsg( I2S( recipeTable[ id][ 2]))

JASS:
library ItemRecipeSystem /* version 1.0.3.0
================================ Requirements =================================
               Requires JNGP 
    */uses Table, /* [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url] by bribe
    *      Alloc  * [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-alternative-221493/[/url] by nestharus

*************************************************************************************
*
*   Used to easily create item recipes.
*
************************************************************************************
*
*   Installing
*       STEP 1: Install JNGP first if you dont have JNGP.
*       STEP 2: Install Table by bribe the link is above.
*       STEP 3: Install Alloc by nestharus the link is above. You may be able to use some other alloc systems.
*       STEP 4: Copy and paste this library to your map.
*
************************************************************************************
*
*       Description
*       -----------------------
*           This is a very easy recipe system it only requires you to make recipes by using the create function.
*           After that it checks for a recipe everytime an item is picked up or bought.
*           Note: removes the items and adds the new item to the unit when the last item needed for the recipe is picked up by the unit.
*
*       Capabilities
*       -----------------------
*           
*           Allows the user to easily create an item recipe.  
*               -   example as done in map  -  local RecipeList data = RecipeList.create( 'ratc', 'rat6', 'rat9', 0, 0, 0, 'I000')
*               -   this recipe only uses 3 items. 
*
******************************************************************************************************************************************************************************************************

/ ******************************************************************************************************************************************************************************************************
/ *
/ *       CREATORS / DESTRUCTORS
/ *       -----------------------
/ *
/ ******************************************************************************************************************************************************************************************************
/ *     
/ *     struct Recipe extends array
/ *  
/ *         static method create takes integer i1, integer i2, integer i3, integer i4, integer i5, integer i6, integer itmToBeMade returns thistype
/ *             -   this is all you need to create an itemRecipe. If you do not want a 6 item recipe just put in a 0 for the integers.
/ *
/ *         method destroy takes nothing returns nothing
/ *             -   to destroy an item recipe if wanted. just store the struct into an integer variable to destroy the item Recipe
/ *     
/ *         static method disAssemble takes item itm, unit u returns nothing
/ *             -   u can use this to disAssemble an item made through a recipe
/ *
/ *
/ *         FIELDS
/ *         ------
/ *       
/ *         static method pause takes nothing returns nothing
/ *             -   pauses the recipe system
/ *            
/ *         static method resume takes nothing returns nothing
/ *             -   resumes the recipe system
/ * 
/ *       
/ *         private static method turnOffOtherSystems takes nothing returns nothing
/ *             -   add systems to turn off when the recipe system is running
/ *        
/ *         private static method turnOnOtherSystems takes nothing returns nothing
/ *             -   the systems u turned off in the above method turn them on here
/ *
/ ********************************************************************************************************************************/ 

        /*Don't Change Anything in the block below*/

/********************************************************************************************************************************/    

    globals
        private Table recipeTable
        private Table disAssembleTable
        private Table indexTable
    endglobals
    
    struct Recipe extends array
        implement Alloc

        private static rect recipeRect
        
/********************************************************************************************************************************/    

        /*Add systems to be turned off in the methods below this*/
        
/********************************************************************************************************************************/  
        
        private static method turnOffOtherSystems takes nothing returns nothing
        endmethod
        
        private static method turnOnOtherSystems takes nothing returns nothing
        endmethod
        
        private static method setRect takes nothing returns nothing
            set recipeRect = Rect( 32, 32, 0, 0)
        endmethod
        
/********************************************************************************************************************************/
/********************************************************************************************************************************/           
        
        private static boolean combineItems = true
        private integer i1
        private integer i2
        private integer i3
        private integer i4
        private integer i5
        private integer i6
        private integer itmToBeMade
        private integer ttlItemsForRecipe
        private boolean pickup
        
        static method disAssemble takes item itm, unit u returns nothing
            local integer id = GetItemTypeId( itm)
            local thistype this = disAssembleTable[id]
            set combineItems = false
            call thistype.turnOffOtherSystems()
            call RemoveItem( itm)
            call UnitAddItemById( u, this.i1)
            call UnitAddItemById( u, this.i2)
            call UnitAddItemById( u, this.i3)
            call UnitAddItemById( u, this.i4)
            call UnitAddItemById( u, this.i5)
            call UnitAddItemById( u, this.i6)
            set itm = null
            call thistype.turnOnOtherSystems()
            set combineItems = true
        endmethod
        
        static method pause takes nothing returns nothing
            set combineItems = false
        endmethod
        
        static method resume takes nothing returns nothing
            set combineItems = true
        endmethod
        
        static method create takes integer id1, integer id2, integer id3, integer id4, integer id5, integer id6, integer itmToBeMade, boolean pickupRecipe returns thistype
            local thistype this = thistype.allocate()
            local integer array i
            local integer L = 0
            local integer index
            set this.i1 = id1
            set this.i2 = id2
            set this.i3 = id3
            set this.i4 = id4
            set this.i5 = id5
            set this.i6 = id6
            set this.itmToBeMade = itmToBeMade
            set this.pickup = pickupRecipe
            set i[0] = id1
            set i[1] = id2
            set i[2] = id3
            set i[3] = id4
            set i[4] = id5
            set i[5] = id6
            set this.ttlItemsForRecipe = 0
            set disAssembleTable[itmToBeMade] = this
            loop
                exitwhen L > 5
                set index = 0
                if i[L] != 0 then
                    set this.ttlItemsForRecipe = this.ttlItemsForRecipe + 1
                    set index = 0
                    set index = indexTable[ i[L]]
                    if index > 0 then
                        set index = index + 1
                        set recipeTable[ i[L]][index] = this
                        set indexTable[ i[L]] = index
                    else
                        set index = 1
                        set recipeTable[ i[L]][index] = this
                        set indexTable[ i[L]] = index
                    endif
                endif
                set L = L + 1
            endloop
            call BJDebugMsg( I2S( recipeTable[ 'ratc'][ 1]))
            call BJDebugMsg( I2S( recipeTable[ 'ratc'][ 2]))
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.deallocate()
        endmethod
        
        private static method checkingRecipes takes integer id, unit u returns nothing
            local thistype this
            local integer index = indexTable[id]
            local integer array i
            local integer ttlItemsUnitHas = 0
            local boolean b
            local item array itm
            local boolean array isItm
            local integer L = 1
            local integer L1 = 1 // used to check through the recipes tht go w that specific item
            local integer L2 = 1
            local integer r = 1 // used for removing items from the slots
            if combineItems then
                call thistype.turnOffOtherSystems()
                loop
                    exitwhen L1 > index
                    set this = recipeTable[ id][ L1]
                    call BJDebugMsg( I2S( this))
                    if this.pickup then
                        call BJDebugMsg( "running" )
                        set i[1] = this.i1
                        set i[2] = this.i2
                        set i[3] = this.i3
                        set i[4] = this.i4
                        set i[5] = this.i5
                        set i[6] = this.i6
                        set isItm[1] = false
                        set isItm[2] = false
                        set isItm[3] = false
                        set isItm[4] = false
                        set isItm[5] = false
                        set isItm[6] = false
                        set ttlItemsUnitHas = 0
                        loop
                            exitwhen L > 6
                            set itm[L] = UnitItemInSlot( u, (L - 1))
                            loop
                                exitwhen L2 > 6
                                if isItm[L2] == false then
                                    if itm[L] != null and GetItemTypeId( itm[L]) == i[L2] then
                                        set isItm[L2] = true
                                        set ttlItemsUnitHas = ttlItemsUnitHas + 1
                                        call BJDebugMsg( "ttl = " + I2S( ttlItemsUnitHas))
                                        exitwhen isItm[L2]
                                    endif
                                endif
                            set L2 = L2 + 1
                            endloop
                            set L2 = 1
                            set L = L + 1
                        endloop
                        if ttlItemsUnitHas == this.ttlItemsForRecipe then
                            loop
                                exitwhen r > 6
                                if isItm[r] then
                                    call RemoveItem( itm[r])
                                endif
                                set itm[r] = null
                                set r = r + 1
                            endloop
                            call UnitAddItemById( u, this.itmToBeMade)
                        endif
                    endif
                    set L1 = L1 + 1
                endloop
                call thistype.turnOnOtherSystems()
            endif
        endmethod
        
        private static method recipeCheck takes nothing returns boolean
            local integer id = GetItemTypeId( GetManipulatedItem())
            local unit u = GetTriggerUnit()
            call thistype.checkingRecipes( id, u)
            set u = null
            return false
        endmethod
        
        private static method inRect takes nothing returns boolean
            local unit u = GetTriggerUnit()
            local integer id = GetItemTypeId( UnitItemInSlot( u, 0))
            call thistype.checkingRecipes( id, u)
            set u = null
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            local trigger t1 = CreateTrigger()
            local region r = CreateRegion()
            set recipeTable = TableArray[0x2000]
            set disAssembleTable = Table.create()
            set indexTable = Table.create()
            call setRect()
            call RegionAddRect( r, recipeRect)
            call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
            call TriggerAddCondition( t, function thistype.recipeCheck)
            call TriggerRegisterEnterRegion( t1, r, null)
            call TriggerAddCondition( t1, function thistype.inRect)
            set r = null
            set t = null
            set t1 = null
        endmethod
    
    endstruct

endlibrary
 
Last edited:
I've never had a problem using handle Ids or type ids in the table array. It does save the value but it seems to overwrite it when I load it . Which shouldn't be possible.

well my only other option is to loop through all the structs which is a lot less efficient but i think im gonna have to i wonder y it works fine until i try to add more than one it makes no sense to me. when i go to add the second which should increase the index and set it at the next index array thats when it messes up so ill leave this up here to see if anyone can find a better solution
 
Last edited:
ik that wasnt the problem. struct 1 was being created and rewriting struct 2 because the itemTypeId was 2 high of a value and thus it returned 0 for value so they got overwritten each time it ran. the way i was doing it set the struct value for each item but the item type id was too high so that killed that idea. also it was run twice so struct 2 overwrote struct 1 for the values. thx for trying tho. this post is also solved
 
I do use debug messages. They don't always solve the problem tho. Just show u wats wrong. I only noticed the indexing for spring the values was getting set to 0 because I was going over the array size limit. The debug messages all showed everything to work how I wanted but the loading and saving was messed up that's y I needed help. Plus I haven't posted many help me triggers lol.
 
Status
Not open for further replies.
Top