HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Am I Using Structs Right?

05-21-2007, 03:46 AM#1
Joker
Collapse JASS:
struct test
unit a
endstruct

function def takes nothing returns nothing
    local timer t = GetExpiredTimer()
    call SetUnitState(dat.a, UNIT_STATE_LIFE, GetUnitState(dat.a, UNIT_STATE_LIFE) -1)
    call DisplayTextToPlayer( GetOwningPlayer(dat.a), 0,0,R2S(GetUnitState(dat.a, UNIT_STATE_LIFE)))
endfunction

function abc takes nothing returns nothing
    local unit a = GetTriggerUnit()
    local timer t = CreateTimer()
    dat.a = a
    call TimerStart( t, 1, true, function def )
    set a = null
endfunction

The script makes no sense (I know), but would it work? (Assuming that I had the TriggerEvent)

P.S. Sry for all these threads...I think i should just create 1 question thread for myself.
05-21-2007, 03:52 AM#2
zen87
no, it will not.

your both your function def and abc don't define the struct in the first place, it will show an error of something like *what is dat?*
05-21-2007, 03:53 AM#3
Vexorian
where do you get dat from in that def function? or in the abc funciton? what are you doing? seriously!
05-21-2007, 04:40 AM#4
Toink
You need to declare a local struct variable, something like local <yourstructname> <var name> should be local test dat = test.create()
To get an attached struct use call <structname>(GetAttachedInt(t, <struct label>)) and it should be something like call test(GetAttachedInt(t, "dat"))
05-21-2007, 07:41 PM#5
Joker
Hey it was my fist attempt =P. Heres another attempt...Why do you need to attach the struct? Couldnt you just directly use it in another function without attaching?
Collapse JASS:
struct test
unit a
endstruct

function def takes nothing returns nothing
    local timer t = GetExpiredTimer()
    call SetUnitState(dat.a, UNIT_STATE_LIFE, GetUnitState(dat.a, UNIT_STATE_LIFE) -1)
    call DisplayTextToPlayer( GetOwningPlayer(dat.a), 0,0,R2S(GetUnitState(dat.a, UNIT_STATE_LIFE)))
endfunction

function abc takes nothing returns nothing
    local test dat = test.create()
    local unit a = GetTriggerUnit()
    local timer t = CreateTimer()
    dat.a = a
    call TimerStart( t, 1, true, function def )
    set a = null
endfunction
05-22-2007, 07:49 PM#6
Joker
bump?
05-22-2007, 08:08 PM#7
wyrmlord
First off, how do you get 'dat' in the callback function? You can't refer to a local variable in another function. When you're creating a struct, it's like creating a new variable type that you're able to use (or at least you can think of it that way).
05-22-2007, 08:11 PM#8
Joker
so the only way to move the variable is attaching it?
05-22-2007, 08:22 PM#9
1337D00D
Collapse JASS:
struct test
unit a
endstruct

function def takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local test dat = test(GetHandleInt( t, "struct"))
    call SetUnitState(dat.a, UNIT_STATE_LIFE, GetUnitState(dat.a, UNIT_STATE_LIFE) -1)
    call DisplayTextToPlayer( GetOwningPlayer(dat.a), 0,0,R2S(GetUnitState(dat.a, UNIT_STATE_LIFE)))
endfunction

function abc takes nothing returns nothing
    local test dat = test.create()
    local unit a = GetTriggerUnit()
    local timer t = CreateTimer()
    dat.a = a
    call SetHandleInt( t, "struct", dat)
    call TimerStart( t, 1, true, function def )
    set t = null
    set a = null
endfunction
^ ^
This script has a few leaks still, and you might want to destroy the struct when you are done, but this is generally how it should be.
05-22-2007, 08:48 PM#10
Joker
I thought structs were the alternative to handles...
05-22-2007, 09:02 PM#11
MaD[Lion]
I seriously dont know what he is trying to do lol.
why do u use handle when u use struct O.o
05-22-2007, 09:32 PM#12
Joker
How would you used structs so that it replaces handles?
05-22-2007, 09:38 PM#13
Anitarf
Joker, structs are nothing more than a set of global arrays where you can reserve indexes to store information to, that way other functions can't overwrite your data. Your test variable dat is basicaly just an integer that tells you what index in the arrays got reserved for you when you called test.create. You still have to pass this index if you want to use it in other functions, and the only way to do this with timer callback functions is to attach the index to a timer.

Therefore, structs can't fully replace handle vars, but that doesn't mean they are useless. In your particular case, you no longer need to use the return bug to get a unit from an integer, which is considered unsafe. Also, if you want to pass more information to the timer callback function, you only need to attach a single struct to the timer rather than a bunch of individual values, which is faster since you only have one gamecache call.
05-22-2007, 09:50 PM#14
Joker
thx Anitarf, that cleared most, not if all, of my questions :)

Edit: Sigh...these questions don't end....What's the most effective way of attaching the struct to a timer? Also, are you able to pass structs as an arguement?
05-22-2007, 10:01 PM#15
wyrmlord
You could use a timer stack where you create a certain amount of timers at the beginning of the game and use only those timers for spells and such. You could then fairly easily create a GetTimerIndex function or w/e to get a unique integer for each timer, and then you could use a global integer array for storage.