• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Vexorian Table 3.1 in a 2D array, need help!

Status
Not open for further replies.
Level 21
Joined
Aug 9, 2006
Messages
2,384
Hello, I am currently trying to make a system that saves a certain ressource to the Table 3.1 made by vexorian.

JASS:
library MatrixCoreMechanic requires Table

globals
    private Table tMatrixInformation
    private constant integer iMaximumCore = 100
endglobals

    private keyword Initialization
    public keyword SetMatrixCoreForUnit
  
    public function MatrixCoreMaxReached takes unit CoreUnit returns boolean
        local integer Temp
        set Temp = tMatrixInformation["TEST"][GetHandleId(CoreUnit)]
        return Temp >= iMaximumCore
    endfunction
  
    public function MatrixCoreZeroReached takes unit CoreUnit returns boolean
        return tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] <= 0
    endfunction
  
    public function InitMatrixCoreForUnit takes unit CoreUnit returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
    endfunction
  
    public function AddToMatrixCore takes unit CoreUnit, integer AddedSparks returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] + AddedSparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
        endif
    endfunction
  
    public function SetMatrixCore takes unit CoreUnit, integer Sparks returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = Sparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
        endif
    endfunction
  
    private module Initialization
        private static method onInit takes nothing returns nothing
            set tMatrixInformation = Table.create()
        endmethod
    endmodule
  
  
  
endlibrary

This gives me many errors in the areas where the 2D Table Array is used.

I have searched high and low in the web and I have not found a proper manual on Tables 3.1 or an tutorial.

I used the exact syntax described in the documentation of vexorian.

If someone could help me, I would really appreciate that!
 
Last edited by a moderator:
JASS:
library MatrixCoreMechanic requires Table

globals
    private Table tMatrixInformation
    private constant integer iMaximumCore = 100
endglobals

    private keyword Initialization
    public keyword SetMatrixCoreForUnit
 
    public function MatrixCoreMaxReached takes unit CoreUnit returns boolean
        local integer Temp
        set Temp = tMatrixInformation["TEST"][GetHandleId(CoreUnit)]
        return Temp >= iMaximumCore
    endfunction
 
    public function MatrixCoreZeroReached takes unit CoreUnit returns boolean
        return tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] <= 0
    endfunction
 
    public function InitMatrixCoreForUnit takes unit CoreUnit returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
    endfunction
 
    public function AddToMatrixCore takes unit CoreUnit, integer AddedSparks returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] + AddedSparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
        endif
    endfunction
 
    public function SetMatrixCore takes unit CoreUnit, integer Sparks returns nothing
        set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = Sparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0
        endif
    endfunction
 
    private module Initialization
        private static method onInit takes nothing returns nothing
            set tMatrixInformation = Table.create()
        endmethod
    endmodule
 
endlibrary

It would appear to be the case that you are treating the Table instance as a class object. A simple solution is to omit the string key from the Table instance tMatrixInformation.

It would end up looking like this:

JASS:
library MatrixCoreMechanic requires Table

globals
    private Table tMatrixInformation
    private constant integer iMaximumCore = 100
endglobals

    private keyword Initialization
    public keyword SetMatrixCoreForUnit
 
    public function MatrixCoreMaxReached takes unit CoreUnit returns boolean
        local integer Temp
        set Temp = Table["TEST"][GetHandleId(CoreUnit)]
        return Temp >= iMaximumCore
    endfunction
 
    public function MatrixCoreZeroReached takes unit CoreUnit returns boolean
        return tMatrixInformation[GetHandleId(CoreUnit)] <= 0
    endfunction
 
    public function InitMatrixCoreForUnit takes unit CoreUnit returns nothing
        set tMatrixInformation[GetHandleId(CoreUnit)] = 0
    endfunction
 
    public function AddToMatrixCore takes unit CoreUnit, integer AddedSparks returns nothing
        set tMatrixInformation[GetHandleId(CoreUnit)] = tMatrixInformation[GetHandleId(CoreUnit)] + AddedSparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation[GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation[GetHandleId(CoreUnit)] = 0
        endif
    endfunction
 
    public function SetMatrixCore takes unit CoreUnit, integer Sparks returns nothing
        set tMatrixInformation[GetHandleId(CoreUnit)] = Sparks
        if MatrixCoreMaxReached(CoreUnit) then
            set tMatrixInformation[GetHandleId(CoreUnit)] = iMaximumCore
        endif
        if MatrixCoreZeroReached(CoreUnit) then
            set tMatrixInformation[GetHandleId(CoreUnit)] = 0
        endif
    endfunction
 
    private module Initialization
        private static method onInit takes nothing returns nothing
            set tMatrixInformation = Table.create()
        endmethod
    endmodule

    // A dummy struct to carry out the instantiation (extends array so that it does not generate alloc and dealloc methods, which are unneeded here)
    private struct Initializer extends array
        implement Initialization
    endstruct
endlibrary

In another note, I could instead recommend another Table resource: [Snippet] New Table
 
The 2D array syntax only works when you are using the Table class itself, rather than a specific instance. For example, this is what your code looks like:
JASS:
set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0

However, Table's 2D syntax is designed for it to work like this:
JASS:
set Table["Sparks"][GetHandleId(CoreUnit)] = 0

The reason has to do with how Table is implemented. Table is basically a wrapper for the built-in hashtable, which have two keys: a parent key and a child key. When you make a specific instance, e.g. tMatrixInformation, it acts as a specific parent key for whatever you want to store, and deals with that part for you. So then all you have at your disposal is a child key. That's why it is only designed to support one-dimension assignment for a specific table instance.

When you use the 2D syntax, you're basically saying, "let me worry about both the parent key and the child key, I'll handle it myself". So you get access to both the parent key and the child key, as long as you're working on the Table class itself rather than a specific instance. Fun fact: internally, when you do Table["Sparks"][GetHandleId(CoreUnit)], the Table["Sparks"] gets treated as a fake Table instance, and then it goes through the normal process to access the second index (CoreUnit's handle ID).

The system that MyPad linked supports 2D arrays for specific instances (look at the TableArray class), but idk if it is really necessary in most cases. For your purposes, it seems like the handle ID should be unique enough. :)
 
Level 21
Joined
Aug 9, 2006
Messages
2,384
The 2D array syntax only works when you are using the Table class itself, rather than a specific instance. For example, this is what your code looks like:
JASS:
set tMatrixInformation["Sparks"][GetHandleId(CoreUnit)] = 0

However, Table's 2D syntax is designed for it to work like this:
JASS:
set Table["Sparks"][GetHandleId(CoreUnit)] = 0

The reason has to do with how Table is implemented. Table is basically a wrapper for the built-in hashtable, which have two keys: a parent key and a child key. When you make a specific instance, e.g. tMatrixInformation, it acts as a specific parent key for whatever you want to store, and deals with that part for you. So then all you have at your disposal is a child key. That's why it is only designed to support one-dimension assignment for a specific table instance.

When you use the 2D syntax, you're basically saying, "let me worry about both the parent key and the child key, I'll handle it myself". So you get access to both the parent key and the child key, as long as you're working on the Table class itself rather than a specific instance. Fun fact: internally, when you do Table["Sparks"][GetHandleId(CoreUnit)], the Table["Sparks"] gets treated as a fake Table instance, and then it goes through the normal process to access the second index (CoreUnit's handle ID).

The system that MyPad linked supports 2D arrays for specific instances (look at the TableArray class), but idk if it is really necessary in most cases. For your purposes, it seems like the handle ID should be unique enough. :)

Thanks alot.

This seems like a really curious syntax :D

I will try it.
 
Status
Not open for further replies.
Top