• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece!🔗 Click here to enter!

[JASS] TableArray corruption

Status
Not open for further replies.
Level 19
Joined
Aug 8, 2007
Messages
2,765
Im using Bribe's NewTable system and I've been having an issue with a Recipe system and I found the cause of it, but I cant figure out why its happening

JASS:
    struct RecipeGroup
        private Table recipes
        private integer recIndex
        public static method create takes nothing returns RecipeGroup
            local thistype this = thistype.allocate()
            set recipes = Table.create()
            set recIndex = 0
            return this
        endmethod
        public method add takes Recipe2 rec returns nothing
            set recipes[recIndex] = rec
            set recIndex = recIndex + 1
        endmethod
        public method check takes unit u, InventoryButton root returns nothing
            local integer i = 0
            local Recipe2 r
            local TableArray t
            loop    
                call print("rec0:"+I2S(recipes[0]))
                call print("rec1:"+I2S(recipes[1]))
                exitwhen recipes[i] == 0
                //set r = recipes[i]
                set t =  TableArray[2]
               // call r.check(u,root)
                set i = i + 1
            endloop
        endmethod
    endstruct

This ends up printing

rec0:1
rec1:2
rec0:2
rec1:0

I dont know whats going on... If i run it again

rec0:2
rec1:0
rec0:3
rec1:0

and again

rec0:3
rec1:0
rec0:3
rec1:0

...

Any ideas?
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
the recipe Table should only be created once.
JASS:
set recipes = Table.create()
i have a working item recipe system in the my sig if u want to use that.

if its only being created once can u plz show us the whole trigger

Also this is vJass not jass lol

make sure rec is not returning the wrong value.

Huh?

its only created once.

the recipe system is for a custom fullscreen inventory so it wont be compatible

the purpose of the TableArray[2] thing is because thats what the recipe.check() function creates and causes the issue (see the commented like that says like r.check(a,b,c)

no purpose in showing you the rest of the trigger, thats all of the vital information.
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
set t = TableArray[2]

You have not created the table array, nor have you stored anyhing there.

As stated above, thats simply a placeholder for the effect within the "r.check(u,root)" function that does indeed create a TableArray[2] and interact with it.

e/ "you have not created the table array" what do you mean?
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
is rec the desired value or is it storing the wrong value ?

also check this integer recIndex b4 u store the value to make sure it is the right value.

its hard to tell y this isnt working when we cant even see the script ur using to store the stuff or any other actions that may be altering something wrong.

he is creating the table array correctly. because he is making a local table array then setting it to its value like this. set t = TableArray[2]
although it is an odd way to do it. i dont think table arrays are meant to be local
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
is rec the desired value or is it storing the wrong value ?

also check this integer recIndex b4 u store the value to make sure it is the right value.

its hard to tell y this isnt working when we cant even see the script ur using to store the stuff or any other actions that may be altering something wrong.

he is creating the table array correctly. because he is making a local table array then setting it to its value like this. set t = TableArray[2]
although it is an odd way to do it. i dont think table arrays are meant to be local

*sigh* i knew this was too complex to ask for help.. time to rewrite table
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
It's too complex to help you because you aren't being specific enough about what's wrong.

Is setting the local TableArray variable in the loop the problem? (As in, if you remove set t = TableArray[2] does the correct output happen?)
This ends up printing
How are you testing the system? (As in, supply the actual code you're using to get the output)
I dont know whats going on... If i run it again
When you mean run it again, do you mean you've performed the same check after the first output or do you mean you start up your map again and get different answers?
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
@arhowk
This is wat I was tryin to get at lol. This is by no means complicated.

It's too complex to help you because you aren't being specific enough about what's wrong.

Is setting the local TableArray variable in the loop the problem? (As in, if you remove set t = TableArray[2] does the correct output happen?)
How are you testing the system? (As in, supply the actual code you're using to get the output)
When you mean run it again, do you mean you've performed the same check after the first output or do you mean you start up your map again and get different answers?
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
It's too complex to help you because you aren't being specific enough about what's wrong.

Is setting the local TableArray variable in the loop the problem? (As in, if you remove set t = TableArray[2] does the correct output happen?)
How are you testing the system? (As in, supply the actual code you're using to get the output)
When you mean run it again, do you mean you've performed the same check after the first output or do you mean you start up your map again and get different answers?

JASS:
local RecipeGroup r = RecipeGroup.create()
call r.add(1)
call r.add(2)
call r.check(GetTriggerUnit(), (#forgot to remove this argument#))

The setting of the local variable is whats causing the issue, though i tried to debug the TableArray constructor and found nothing. The correct output should be

rec0:1
rec1:2
rec2:0

rec0:1
rec1:2
rec2:0

by logic (because recipe[0] is set to 1, recipe[1] is set to 2, and recipe[2] is never set)

by run it again, i mean running the same code in the same map (without exiting and restarting)
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
I've tried to reproduce your problem with more simplified code, but everything is working as it should be. It looks like something else is affecting your code.
From your first post, I find it strange that you only get 4 lines of output when you should be getting 6 lines based on your loop (which prints debug messages and then exits when i == 2).

Test Code:
JASS:
scope TableTest initializer Init

  private struct Data
  
    private Table list
    private integer listIndex
    
    static method create takes nothing returns thistype
      local thistype this = thistype.allocate()
      
      set this.list = Table.create()
      set this.listIndex = 0
      
      return this
    endmethod
    
    public method add takes integer element returns nothing
      set this.list[this.listIndex] = element
      set this.listIndex = this.listIndex + 1
    endmethod
    
    public method check takes nothing returns nothing
      local integer index = 0
      local TableArray t
      
      loop
        call BJDebugMsg("list[0]: " + I2S(this.list[0]))
        call BJDebugMsg("list[1]: " + I2S(this.list[1]))
        exitwhen this.list[index] == 0
        //call BJDebugMsg("list[" + I2S(index) + "]: " + I2S(this.list[index]))        
        set t = TableArray[2]
        //call t.destroy()
        set index = index + 1
      endloop
    endmethod
    
    method destroy takes nothing returns nothing
      call this.list.destroy()
      call this.deallocate()
    endmethod
    
  endstruct

  private function TableTest takes nothing returns nothing
    local Data d = Data.create()
    call d.add(1)
    call d.add(2)
    call d.check()
    //call d.destroy()
  endfunction

  private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    
    call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
    call TriggerAddAction(t, function TableTest)
  endfunction
  
endscope
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
I think it has something to do with the garbage collector, changing this

(in the TableArray [] constructor)

JASS:
    static method operator [] takes integer array_size returns TableArray
        local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
        local TableArray this = tb[0]         //The last-destroyed TableArray that had this array size
        
        if array_size <= 0 then
            call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
            return 0
        endif
        
        if this == 0 then 
            set this = less - array_size
            set less = this
        else
            call print("LastDest was before:"+I2S(tb[0]))
            set tb[0] = tb[this]  //Set the last destroyed to the last-last destroyed
            call tb.remove(this)  //Clear hashed memory
        endif
        
        set dex.size[this] = array_size //This remembers the array size
        return this
    endmethod

to

JASS:
    static method operator [] takes integer array_size returns TableArray
        local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
        local TableArray this = tb[0]         //The last-destroyed TableArray that had this array size
        
        if array_size <= 0 then
            call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
            return 0
        endif
        
        //if this == 0 then 
            set this = less - array_size
            set less = this
       // else
        //    call print("LastDest was before:"+I2S(tb[0]))
        //    set tb[0] = tb[this]  //Set the last destroyed to the last-last destroyed
        //    call tb.remove(this)  //Clear hashed memory
        //endif
        
        set dex.size[this] = array_size //This remembers the array size
        return this
    endmethod

seems to stop it
 
Status
Not open for further replies.
Top