Vjass

SFilip

Gone but not forgotten
Reaction score
634
If you have experience with Jass then you probably know that every handle type has to be created. For example:
local group g = CreateGroup()
Without creategroup you couldn't access this variable because it's empty - pointing nowhere actually.
Structs work in a similar way - just defining a struct does nothing, once you do a create you have access to it's members. Don't forget to destroy though, as you have a limit on the number of created structs.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
This is just an example but.....how come it can declare a [POINT] as a type?

It doesn't really declare a new type, instead JassHelper does some transformation on the code and turn it to "normal" jass. The jass generated is very hard to work with, you can say that structs of vJass is just synthetic sugar but it's a very sweet sugar :)

Your code will transformed to something like this:

JASS:
globals
    integer v__struct_point_number = 0
    integer v__struct_point_garbage = 0
    integer array v__struct_point_recycle
   
    real array v__struct_member_x
    real array v__struct_member_y
endglobals

function v__struct_point_method_create takes nothing returns integer
    if v__struct_point_garbage > 0 then
         set v__struct_point_garbage = v__struct_point_garbage - 1
         return v__struct_point_recycle[v__struct_point_garbage]
    endif
    set v__struct_point_number = v__struct_point_number + 1
    // Do initial initializing of struct members, your code had none
    // set v__struct_member_x[v__struct_point_number] = -1.2
    return v__struct_point_number
endfunction

function v__struct_point_method_destroy integer struct nothing returns nothing
    set v__struct_point_recycle[v__struct_point_garbage] = struct
    set v__struct_point_garbage = v__struct_point_garbage + 1
endfunction

function somefunc takes nothing returns nothing
local integer c = v__struct_point_method_create()
set v__struct_member_x[c] = GetUnitX(GetTriggerUnit())
set v__struct_member_y[c] = GetUnitY(GetTriggerUnit())
// hey, you forgot to destroy 
endfunction


Then we have private/public/static/error checking/ect that complicates stuff but the final result is always normal, but hard to work with, jass.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
cant understand...phrex's code...cant even understand 1 line
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
?

It's just a bunch of global arrays. Plus, vJass makes an Allocate and Destroy function for each 'struct'. It's really simple stuff... I could write a struct myself, but vJass does it for us, and we get to use struct and endstruct. YAY!
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
JASS:
    set v__struct_point_recycle[v__struct_point_garbage] = struct

Omg? integer = struct?
 

Tom Jones

N/A
Reaction score
437
Structs are nothing more than global variable arrays in disguise. When the preprocessor finds a struct declaration, it'll create a array for each declared variable inside the struct. Variables you declare inside a struct is called members. It'll also create (at least) two functions, one for creating a unique number for the index of the arrays associated with the struct (Remember that members of a struct is in fact arrays) and one for reallocating the arrays associated with the struct. A practical example of a struct declaration:
JASS:
struct data //When the preprocessor meets the declaration, it'll create a function called s__data__allocate and s__data__destroy
    unit u //When the preprocessor meets this member, it'll create a array called s__data__u
    unit v //When the preprocessor meets this member, it'll create a array called s__data__v
endstruct
Notice how the preprocessor uses the struct and members name as suffix for the arrays. Now when we use a struct we simply declare a local variable of the type of struct we want:
JASS:
local data s
If you think of a struct as a handle, just as unit, force, group, and timer are handles, it's easy to see why we declare it the way we do. Firstly we declare that it's a local variable. Next we declare that the type of the variable is data. Next we declare a name for the variable. Now we should get a unique number for this instance of the data struct, and we do this by using the function mentioned earlier, that creates a unique number. The function is actually also a member of the struct, and all members of a struct is accessed by the name of the struct variable, followed by a dot:
JASS:
local data s = data.create() //Since the variable s isn't declared yet, we're forced to use the type of the struct instead of the actual struct variable
We now have a unique number for this instance of the struct, and can now use the data variable s. Let's assign some units to the struct:
JASS:
local data s = data.create()
set s.u = CreateUnit(Player(0),'hfoo',0,0,0)
set s.v = CreateUnit(Player(0),'hkni',0,0,0)
set s.d = CreateUnit(Player(0),'hpea',0,0,0) //This will cause a syntax error, because d isn't a member of the data struct.
Now what the preprocessor is actually doing, is replacing the instances of s.u and s.v with the name of respective arrays, and using s as index. Neat isn't it?
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
Code:
struct [B]data[/B] //When the preprocessor meets the declaration, it'll create a function called s__data__allocate and s__data__destroy  
unit u //When the preprocessor meets this member, it'll create a array called s__data__u  
unit v //When the preprocessor meets this member, it'll create a array called s__data__v 
endstruct

local [B]data[/B] [U]s[/U] = [B]data[/B].create() 
set [U]s[/U].u = CreateUnit(Player(0),'hfoo',0,0,0) 
set [U]s[/U].v = CreateUnit(Player(0),'hkni',0,0,0) 
set [U]s[/U].d = CreateUnit(Player(0),'hpea',0,0,0) //This will cause a syntax error, because d isn't a member of the data struct.

and is it we only can struct once per trigger? and also scope and library
is all the underlined and the bolded word needed to be same?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
1. No, if I understood you correctly. Did you mean that you can use a struct only once per trigger? If so, then no, because I think that you can use it multiple times.

2. Yeaaaa! Totally dude! :D

Sorry, I was listening to Party like a rockstar atm. :p
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
>>Did you mean that you can use a struct only once per trigger?
no...=.= i am asking...aw i will take an example and show u :
JASS:
struct data
real x
real y
endstruct

function ... takes....
actions....
endfunction....

struct anotherData
real a
real b 
endstruct

function otherfunction takes....
endfunction


can i do this??

>>Yeaaaa! Totally dude!
dude...whic question are u answerinG?
 

Tom Jones

N/A
Reaction score
437
You can make as many struct declarations as you want, with any name you like. You can also declare struct variables of the same struct type multiple times, as long as the variable name isn't the same:
JASS:
struct ABCD
    unit u
endstruct

struct someRandomNameForAStruct
    unit u
    unit v
endstruct

struct data
    string txt
endstruct

function ...
    local ABCD s1 = ABCD.create()
    local ABCD s2 = ABCD.create()
    local someRandomNameForAStruct s3 = someRandomNameForAStruct.create()
    local data whatever = data.create()
    local unit u = GetTriggerUnit()
    local unit v = GetSpellTargetUnit()
    
    set s1.u = u
    set s2.u = v
    set s3.u = u
    set s3.v = v
    set whatever.txt = "hey"
    set u = null
    set v = null
endfunction
Makes sense? data and s is simply the names I use for the name of the structs, and the name of a struct variables.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
So....As a conclusion...
JASS:
struct LoL
unit x
endstruct

In this line...LoL is declared as a [TYPE]

JASS:
local LoL afg = LoL.create()

we create LoL and name it as afg

JASS:
set afg.x = GetTriggerUnit()

i declare the member inside the struct .....Are all these correct?

//---------------------------------------------------------------------
Next ~
JASS:
struct ABCD
    unit u
endstruct

struct someRandomNameForAStruct
    unit u
    unit v
endstruct


unit "u" in struct ABCD and also someRandomNameForAStruct ??



How do we destroy struct to prevent leak?


Struct last along whole map or only 1 trigger or even only 1 function?
**This important...i need to know this**
 

SFilip

Gone but not forgotten
Reaction score
634
> unit "u" in struct ABCD and also someRandomNameForAStruct ??
Completely independent members.

> How do we destroy struct to prevent leak?
http://www.wc3campaigns.net/vexorian/jasshelpermanual.html#strucrest
Either call <variable name>.destroy() or call <struct name>.destroy(<variable name>).

> Struct last along whole map or only 1 trigger or even only 1 function?
From the point where you create it to the point where you destroy it.
The definition itself can be anywhere, doesn't really matter.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
>>Completely independent members
independent?? whats that?

>>Either call <variable name>.destroy() or call <struct name>.destroy(<variable name>).
Got that !!

>>From the point where you create it to the point where you destroy it.
The definition itself can be anywhere, doesn't really matter.

So that once i typed:
JASS:
struct ABCD
trigger u
endstruct

local ABCD AName = ABCD.create()

Then i will be able to use "AName" in other trigger after this line?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
I believe SFilip means that they are there own variables because they have different struct names, which means that when accessing them, you can use both ABCDVARNAME.u and someRandomNameForAStructVARNAME.u...

>Then i will be able to use "AName" in other trigger after this line?

Yes, I'm pretty sure.

>Are all these correct?

Yes.

:D
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
it seems that....i cant call a struct from another trigger....how do i do that?
Example:
JASS:
TRIGGER 1
struct A
string X
endstruct

function SomeFunc takes nothing returns nothing
local A AS = A.create()
set AS.X = &quot;SomeString&quot;
call EnableTrigger(TRIGGER 2)
endfunction
//--------------------------------------------------------
TRIGGER 2 (With a periodic event)

function Trig2Action takes nothing returns nothing
call BJDebugMsg(AS.X)
endfunction

How come this does syntex?
 
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