Question about structs

Tyman2007

Ya Rly >.
Reaction score
74
I've been getting into structs and I haven't had too much experience with them..

I've been wondering, that when I set the variables in the struct, say something like this

JASS:
private struct DATA
    unit u
endstruct

private function Actions takes nothing returns nothing
    local DATA d = DATA.create()
    set d.u = GetSpellAbilityUnit()
endfunction


How can I use the variables I set in other functions? I don't quite know how to work that. Like, is it

JASS:
private function Other takes nothing returns nothing
    local DATA d = DATA.allocate()
endfunction


I've been confused on this, and this would truly make my life a whole lot easier..
 

Nexor

...
Reaction score
74
If you are using global structs, then you can simply use them like this:

JASS:
struct thing
    unit hero
endstruct

globals
    thing THING
endglobals

function SetHero takes unit u returns nothing
    set THING.hero = u
endfunction


Else you have to attach it to something, to a timer/trigger and such.
 

Tyman2007

Ya Rly >.
Reaction score
74
oh, never thought about that. Thanks!

EDIT: Tried the globals thing out..
Didn't quite work.

No errors, except for the actual setting the variables.

Not allowed to use .syntax or something.

How would I attach it to a timer/trigger though.. I'm new to structs here and don't know how..

EDIT: Unless... one minute

EDIT: Nevermind... Doesn't work..

Yeah.. how would I attach it to a timer/trigger
 

Joker(Div)

Always Here..
Reaction score
86
In order to pass structs to other functions you need to either:

use a global struct array
pass it through an argument
use an attaching system
use hashtables/table/data storing systems

For timers, I like to use TimerUtils.
 

Tyman2007

Ya Rly >.
Reaction score
74
Tried a fairly old hashtables system and I don't quite know where to find a new one...

What I'm asking is how do I pass the values set from a function to a struct to another function?

I don't know how to use things like what you're saying. I know how to use timers and hashtables, but really that voids the purpose of me having a struct.

I need an explaination.. Not a single line saying basically what I have to do.. I'm a noob at this and don't know where to even start..
 

saw792

Is known to say things. That is all.
Reaction score
280
Well we can't explain how to transfer a struct if you don't provide a context. Something like "I want to get my struct in a timer callback", "I want to get my struct in a different trigger based on the same unit in each", "I want to get my struct in a ForGroup/GroupEnum filter function". The method is different for each of those which is why you have only got general answers so far.
 

Tyman2007

Ya Rly >.
Reaction score
74
Yeah sorry... Just a little stressed.. Have to go into surgery soon ya know.

I was working on a spell and was using timers for the first time in my life.. Then tried using hashtables and found out that the system I was using was waay outdated.

I'm using timers for slide basically. I got everything down, just need to make the variables go throughout the functions. I thought this was easily possible through structs, and I'm confused on that. I set a variable in a struct in one function and I want to be able to use that same variable that I set in the struct into another function in the same trigger.

I need to use the structs for a slide function basically.

I would use hashtables... but I don't know of any good hashtable systems out there, and this is my first time using timers and around my third for structs. I'm clueless on programming and programming terms pretty much...

I'm an uber noob really..
 

saw792

Is known to say things. That is all.
Reaction score
280
No problem. Simplest method using TimerUtils (link in Joker's post):
JASS:
scope Spell

private struct data
  unit u
  real r
  integer ticks
  //etc
endstruct

private function Callback takes nothing returns nothing
  local data d = GetTimerData(GetExpiredTimer())  //TimerUtils function, gets struct back from the timer
  //do stuff
  set d.ticks = d.ticks + 1
  if d.ticks >= (<duration> / <period>) then //if the timer has fired the number of times it should have
    call PauseTimer(t) //Mightn't be necessary... only really is if TimerUtils breaks
    call ReleaseTimer(t) //TimerUtils function, replaces DestroyTimer()
    call d.destroy() //Destroy your struct
  endif
endfunction

private function Actions takes nothing returns nothing
  local data d = data.create()
  local timer t = NewTimer() //TimerUtils function, replaces CreateTimer()
  set d.u = GetSpellAbilityUnit()
  //etc
  call SetTimerData(t, d) //TimerUtils function, attaches the struct instance to the local timer
  call TimerStart(t, <period>, true, function Callback)
endfunction

endscope
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
[ljass] GetSpellAbilityUnit() [/ljass] is slower than [ljass] GetTriggerUnit() [/ljass].
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top