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

Best/Easy way to store lots of info on a unit

Status
Not open for further replies.
Level 4
Joined
Dec 10, 2005
Messages
73
I want to store a couple of different data "onto" the unit like custom value. What's the best way to do this?
 
Level 8
Joined
Jul 25, 2006
Messages
177
Use arrays if your using GUI. Set the unit's user date to the index for that unit's information. For example we have a footman, we set his custom value to 1, his variables will all be InfoA[1] InfoB[1]. At the beginning of the game we select all units in the map and give them each their own integer value in their custom value. You can do this by using a loop, for example.

GlobalIndex is an integer.

Pick every unit in group playable map area and loop actions
set unit's custom value = GlobalIndex
set InfoA[GlobalIndex] = some info
set InfoB[GlobalIndex] = some info
set GlobalIndex = GlobalIndex + 1
end of looping actions

you can also easily use the same trigger for new units entering the map

Event
Unit enter map area
Action
set trigger unit's custom value = GlobalIndex
set InfoA[GlobalIndex] = some info
set InfoB[GlobalIndex] = some info
set GlobalIndex = GlobalIndex + 1
end of actions


To refer to a specific data variables we have for the unit we just do

InfoA[Get {w/e unit's} Custom Value]
InfoB[Get {w/e unit's} Custom Value]
 
Level 19
Joined
Aug 24, 2007
Messages
2,888
vJASS
JASS:
struct DATA // a struct I named as DATA
integer variable1 // an integer variable of that struct
integer variable2 // another integer variable of that struct
integer variable3 // another integer variable of that struct
endstruct // end of struct

function DarthPlagueis takes nothing returns nothing // some random function
local DATA a = DATA.create() // Here I create index for struct and assing it to "a" variable
call SetUnitUserData(<unit>,a) // Here I set units custom value to that index I created
a.variable1 = 500 // here I set first variable of struct for index I created to 500
a.varable2 = 1000
a.variable3 = 1337
endfunction // end of function

function AnotherFunc takes nothing returns nothing // another random function
local DATA b = GetUnitUserData(<unit>) // here I read the value stored inside unit and assing "b" variable to it (it may be anything you want a,b,lol,wtfboom doesnt matter)
b.variable1 = 101 // here I set first variable of that index to 101
b.variable2 = 412412
b.varaible3 = 2512
endfunction // end of function

Expainations:

In this code there is a struct (struct is like a variable group)
I named it as DATA
DATA.create() gives you a free index for struct which is not used currently

In function named DarthPlagueis I create an index and set it as Unit's custom value
and for that index I set values of 3 variables you see there

and in AnotherFunc I read the custom value of unit which is struct index I stored in previous function
and I set another variables
and when unit dies or you no more want it to have variables
you say
JASS:
local DATA a = GetUnitUserData(<unit>)
a.destroy() //frees that index

Its a little complicated but its nothing when you get used to it
 
Level 14
Joined
Nov 18, 2007
Messages
816
Basically, what everyone is suggesting is: Use some Unit Indexing library. There are at least two decent ones out there. One made by Rising_Dusk (UnitIndexingUtilities), the other one made by cohadar (PerfectUnitIndexing).

@M0RT: LHV is inferior to vJass in every way possible. Unit Indexing is the way to go nowadays.
 
Level 8
Joined
Aug 6, 2008
Messages
451
Struct are cool because you can store a lot of data to them.

Unit Indexing is the way to go, it allows you to easily attach your struct to unit. Check those systems Deaod mentioned.

Alternatively you can also use H2I to convert units handle id to array index or store your struct to gamecache ( Table is a pretty nice system, check it out ).
 
Level 4
Joined
Dec 10, 2005
Messages
73
Thanks guys.

I was inclined to do it the way SinFinite has suggested, but that would make my variable list SUPER messy. I have enough funny stuff in there. I'm not tempted to start using the game cache yet. Looks a bit scary.

I really like the struct method, guess it's time. Good bye GUI.

(some time passed)

I've just got and setup Newjasspack and wow. I wish I knew this early

When I use the struct method, when I destroy an Index, would all it's members get destroyed or recycled?
 
Last edited by a moderator:
Level 19
Joined
Aug 24, 2007
Messages
2,888
Variables inside it wont get destroyed (like if it have unit groups) and variables wont be nulled or changed
It just says: "Hello Im not using this index anymore so you can take it for another create()"
 
Level 4
Joined
Dec 10, 2005
Messages
73
ok, I must remember to null/set to zero to all members before destroying.

How does vJass assign free indexes? Does it have some boolean to know if a index is free or not? ie Search through from 1 to 3( or highest instance) , if true, this index is free, pass this index to create.

EDIT: nevermind, I got confused of what I was doing with structs.
 
Last edited:
Level 19
Joined
Aug 24, 2007
Messages
2,888
So you say

struct hello
group a
endstruct

when I say
local hello f = hello.create()
f.a = CreateGroup()
f.destroy()

and you say also f.a is destroyed ?
Just keep playing your DotA and Vice City
 
Status
Not open for further replies.
Top