HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Struct Questions

07-22-2007, 10:58 AM#1
Toink
Sup, I have a question for you

Well, to whoever reads this thread...

What does struct.create() and struct.destroy() really do? I really need to find that out, it determines if my system will work or fail miserably.

And also, can I use a H2I call for struct indexes? Like for example

Collapse JASS:
function vJASS takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer i = CS_H2I(u)
local structdata dat = structdata(i) //I Still don't know what struct.create() really does
07-22-2007, 12:02 PM#2
Alexander244
This is the relevant generated code (with a few extra comments), if that helps you at all...

Collapse JASS:
globals
  integer array si__STRUCTNAME_V
  integer si__STRUCTNAME_F = 0
  integer si__STRUCTNAME_I = 0
endglobals

//pointer.destroy()
function s__STRUCTNAME_destroy takes integer this returns nothing
    if this==null then
        return
    elseif (si__STRUCTNAME_V[this] != -1) then
        return
    endif
    set si__STRUCTNAME_V[this] = si__STRUCTNAME_F
    set si__STRUCTNAME_F = this
endfunction

//STRUCTNAME.create()
function s__STRUCTNAME__allocate takes nothing returns integer
    local integer this = si__STRUCTNAME_F
        if (this != 0) then
            set si__STRUCTNAME_F = si__STRUCTNAME_V[this]
        else
            set si__STRUCTNAME_I = si__STRUCTNAME_I + 1
            set this = si__STRUCTNAME_I
        endif
    if (this > 8190) then
        return 0
    endif
    
    //ints all the variables in the struct, if you set them to something inside the struct
    
    set si__STRUCTNAME_V[this]= -1
    return this
endfunction

function something takes nothing returns nothing
    local STRUCTNAME pointer = STRUCTNAME.create()
    //local integer pointer = s__STRUCTNAME__allocate()

    call pointer.destroy()
    //call s__STUCTNAME_destroy(pointer)
endfunction
07-22-2007, 12:05 PM#3
Anitarf
You could make a blank file with a struct, save it and then export and open the map's .j file to see what kind of Jass got generated and how structs actualy work.

Essentialy, a struct type is a bunch of paralel arrays and each index in those arrays is one struct. Whenever you call struct.create() it will alocate an index for you to use and whenever you call struct.destroy it will release that index, allowing it to be allocated again later. This is important because array only go up to index 8191 so if you didn't destroy structs which you no longer needed and kept creating new ones then you would eventualy run out of room for them.
07-22-2007, 12:07 PM#4
Toink
Thanks!!!!! So the first index of a struct is 1 not 0, that greatly helps.

Reppy + <3
07-22-2007, 01:21 PM#5
Vexorian
Collapse JASS:
function vJASS takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer i = CS_H2I(u)
local structdata dat = structdata(i-0x100000)
This is actually possible to do if you substract the null handle index from the integer, you are sure that there won't be more than 8190 handles and you don't ever use create or destroy for that struct.
07-22-2007, 09:37 PM#6
Toadcop
Collapse JASS:
local structdata dat = structdata(ModuloInteger(i,8191))
it's more universal... the only feature is what it can simply overwrite data ^^ if there are difference in 8190 between handles id.
07-23-2007, 04:15 AM#7
Vexorian
I'd just love to see a world in which we don't have to face arbitrary array size limits and stuff like that.

I am not sure if a collision is better than a thread crash, well let people decide between undefined code and crashing code! awesome.
07-23-2007, 07:30 AM#8
Toadcop
Quote:
well let people decide between undefined code and crashing code
it's life ^^ the modulo feature is more flexible. but needs a bit longer to complete.
07-23-2007, 10:31 AM#9
MaD[Lion]
i will chop myself in half if i understand what u aliens talk about XD
07-23-2007, 01:34 PM#10
Vexorian
After so many C++ coding I found myself alergic to undefined code, although I guess that if there was a way to detect collisions it would be a good method...
07-23-2007, 05:03 PM#11
Toadcop
Vexorian well it can be used in linked arrays... so it will collide only after 32K for example... (4 arrays needed) and to 32K it's really hard ^^ in normal maps. + vJass preprocesor allows simple create such linked arrays in masses. so it's very cool =)