HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

CSData, attaching integers to units

07-03-2008, 03:08 AM#1
chobibo
Is it safe to attach data to units, I'm asking this because I think it's unsafe for units. If you made 5 spells which all use CSData and these spells all attach data for a single unit, would it not overwrite the previously attached integer? 1 integer per handle id right? So if I were to attach to a unit, I would need to use a dedicated CSData or Unit Custom Value attachment.
I'm not sure that's why I'm asking, also I don't know how to test this. Thanks for any help.
07-03-2008, 03:18 AM#2
Vexorian
CSData is not meant for "public objects" , it means you should only use it for things only your code is able to touch.

If you used UserData it would be the same problem, I would personally use a unit group if what you want to attach is a boolean or gamecache otherwise...
07-03-2008, 03:46 AM#3
grim001
DataSystem uses the CSData method but allows you to use textmacros to generate as many copies of it as you wish. That means you can attach any number and type of handles with customized function names. This makes it ideal for "public objects" since you can make the function name match your system.

Of course there are a billion other attachment methods, but the fact remains: this is the fastest attachment method (other than userdata), period.

Collapse JASS:
library DataSystem

public function H2I takes handle h returns integer
    return h
    return 0
endfunction

//! textmacro DataDeclareFast takes NAME, TYPE
globals
    $TYPE$ array DS_$NAME$
endglobals

function Set$NAME$ takes handle h, $TYPE$ v returns nothing
    set DS_$NAME$[DataSystem_H2I(h) - 0x100000] = v
endfunction

function Get$NAME$ takes handle h returns $TYPE$
    return DS_$NAME$[DataSystem_H2I(h) - 0x100000]
endfunction
//! endtextmacro

//! textmacro DataDeclare takes NAME, TYPE
globals
    $TYPE$ array DS_$NAME$[40955]
endglobals

function Set$NAME$ takes handle h, $TYPE$ v returns nothing
    set DS_$NAME$[DataSystem_H2I(h) - 0x100000] = v
endfunction

function Get$NAME$ takes handle h returns $TYPE$
    return DS_$NAME$[DataSystem_H2I(h) - 0x100000]
endfunction
//! endtextmacro

//! runtextmacro DataDeclare("Struct", "integer")

endlibrary
07-03-2008, 03:57 AM#4
chobibo
Thanks guys! I'm not looking for any other attachment systems, I prefer gamecache and both of your approach, I was just confirming if my analysis was correct, just asked for the sake of learning lol. Thanks again!
07-03-2008, 03:43 PM#5
Themerion
PUI (by Cohadar) is _teh_ way to do this. It uses UnitData, which makes it really fast.

If you use DataSystem, each instance will have to use quite a lot of arrays (unless your coding is perfect, and you do not intend for anybody else to use your code). This is because 1 failed nulling or 1 failure to destroy a point/group/effect/etc. will make the handle count rise (H2I will return a larger number).

PUI always maps the units into a unique low number, meaning you can have ~8100 units on the map before the "attachment" system breaks. Thus, you only need 1 array for each "instance".

The generated code is shorter too (if you use sufficiently many systems. Let's say 4 or more). For each instance with the DataSystem, you will get 2 more function calls. PUI doesn't add any extra function calls for adding more instances at all. It is as simple as this:

Collapse JASS:
globals
    MyDataType array MyData
endglobals

// GetUnitIndex is unique for each unit
set MyData[GetUnitIndex(u)]=MyDataValue
07-03-2008, 04:16 PM#6
grim001
Quote:
Originally Posted by Themerion
If you use DataSystem, each instance will have to use quite a lot of arrays (unless your coding is perfect, and you do not intend for anybody else to use your code).
The actual amount of memory that arrays consume is extremely negligible and will never have any observable impact on performance, even if you declare 1000 unnecessary arrays.

Quote:
Originally Posted by Themerion
This is because 1 failed nulling or 1 failure to destroy a point/group/effect/etc. will make the handle count rise (H2I will return a larger number).
Handle leaks will never cause it to fail unless you have already leaked so many handles that the map is on the verge of freezing.

Quote:
Originally Posted by Themerion
PUI doesn't add any extra function calls for adding more instances at all. It is as simple as this:
The existence of more functions in the script does cause performance issues in any noticeable way even if you declared 1000 unnecessary functions.

DS and PUI overlap in the fact that you could use either of attach an integer to a unit. If you only want to do that, you can use PUI, since it is great for that specific purpose.

DataSystem is for when you want to provide a function call for users to get some specific kind of data from your units, or for attaching to things other than units.

Anyway DataSystem is so simple you could call it a coding method rather than a system. Personally I never actually use DataSystem since I don't want to make my libraries require it; I just type out the generated functions longhand.