HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Struct Arrays

01-26-2008, 05:46 PM#1
TEC_Ghost
Hey I was working on some Unit Indexing stuff, and I need to keep a bunch of info about the unit which I have in a Struct, but I'm not sure of a way to recall the struct data for that certain unit if needed later. Is there anyway to create and array of struct data?
01-26-2008, 06:07 PM#2
Silvenon
Collapse JASS:
struct data
    unit u
endstruct

function Blabla takes nothing returns nothing
    local data array ar
endfunction

Or:

Collapse JASS:
struct data
    unit u
endstruct

globals
    data array ar
globals
01-26-2008, 11:50 PM#3
moyack
The easiest way, but you probably won't learn how to do it, is using PUI

the difficult, but worth it way is getting the idea :)

for the simple fact of creating a struct, vJASS creates internally an array of data, so we should take advantage of this fact.

Collapse In vJASS:
struct Data
   unit u
   real v1
   real v2
endstruct
Converts into
something like this...
=>
Collapse Internally in your map:
globals //it would be something like this...
   unit array Data__u
   real array Data__v1
   real array Data__v2
endglobals

So, if you need to deal with the array, you should use the integer(DATA) command to retrieve the index related to the Data struct. An example:

Collapse JASS:
struct Data
    unit u
    real v1
    real v2
    
    static method create takes unit u, real v1, real v2 returns Data // method used to create and add the data in one line of code
        local Data D = Data.allocate()
        set D.u = u
        set D.v1 = v1
        set D.v2 = v2
        return D
    endmethod
endstruct

function Otherfunction takes unit u returns nothing
    local Data D = Data( GetUnitUserData(u) ) //retrieves the data from the unit
    // do your stuff...
endfunction

function Actions takes nothing returns nothing
    local Data D = Data.create(GetSpellTargetUnit(), 100., 45.)
    local unit U = GetTriggerUnit()
    call SetUnitUserData(U, integer(D)) // here, you attach the struct to the unit by setting the Data index to the unit's userdata.
    call Otherfunction(U)
    set U = null
endfunction

Of course there's some issues for example if the unit dies then the struct gets created and all that shit, but this is something that can be prevented by developing a code that detects that stuff and destroy the Data. How to do it?? is up to you. The important thing is that you get the idea about how it could work.

I hope it helps you.
01-27-2008, 02:28 AM#4
TEC_Ghost
Thanks guys, I actually had it figured out after Silv's post. But I have a better understanding of how it all works now thanks to you Moyack :)

Not the prettiest or best written code, but it works for me :)

Here's what I did.

Collapse JASS:
function AddUnitToSystem takes unit U returns nothing
 
    if GetUnitUserData(U) == 0 then
        set i = i + 1
        set OD[i] = ObjectData.create()
 
        set OD[i].Unit = U
        set UnitIndex[i] = OD[i].Unit
        call SetUnitUserData( OD[i].Unit, i )
        set OD[i].UnitIndex = i
 
        call GroupAddUnitSimple(OD[i].Unit,System_Objects)
 
        call DisplayTextToForce( GetPlayersAll(), GetUnitName(OD[i].Unit) + " added to index" )
        call DisplayTextToForce( GetPlayersAll(), "Index number = " + I2S(OD[i].UnitIndex) )
    else
 
        call DisplayTextToForce( GetPlayersAll(), "Unit already indexed: " + I2S(GetUnitUserData(U)) )
 
    endif
 
endfunction