What is so special about structs?

T.s.e

Wish I was old and a little sentimental
Reaction score
133
Structs can also extend each other.

JASS:
struct firststruct
unit u
real x
real y
endstruct

struct secondstruct extends firststruct
unit target
real tx
real ty
endstruct
 

Viikuna

No Marlo no game.
Reaction score
265
Ye, and there is lot of more cool stuff in JassHelper manual. Read it.
 

Forty

New Member
Reaction score
6
you can also use struct interfaces. for more information just look in the jasshelper manual where you see how and why to use structs/interfaces/vJass

/edit: too late
 

Flare

Stops copies me!
Reaction score
662
what are the periods used for in that statement?

That's just the syntax used i.e.
JASS:
struct StructName
integer member
endstruct

//And another one, for fun ^_^
struct SomethingElse
integer anotherMember
endstruct

function Test takes nothing returns nothing
  local StructName structVar = StructName.create ()
  local SomethingElse data = SomethingElse.create ()
//Struct member must be prefixed by variablename. like so
  set structVar.member = 15
  set data.anotherMember = 72
endfunction
 

SanKakU

Member
Reaction score
21
EDIT. oh ye, wait sec i post more code.


EDIT2. Here is some fast made example how to attach some Data to some Hero, it might be usefull in some map where you have some Heroes...

JASS:
struct Velocity
    real x
    real y
    real z
endstruct

struct Color
    integer red = 255
    integer green = 255
    integer blue = 255
    integer alpha = 255
endstruct

struct Heroes
    string n = "nuthing"
    Color C
    Velocity V
endstruct

function F takes nothing returns nothing
     local Heroes H = Heroes.create()
     local unit somehero // some brave hero unit
     set H.C = Color.create()
     set H.V = Velocity.create()
     // 
     set H.V.x = GetRandomReal(-100,100)
     //
     call SetUnitUserData(somehero,H)
endfunction

can you explain what in JASS you just did there? i don't get it.
 

SanKakU

Member
Reaction score
21
First: For anyone else reading, everything except the last post is over a year old. No jumping on TriggerHappy for not knowing what structs are. ;)

That code is bad practise, it uses UnitUserData directly.
For attaching to heroes (or any units) use AIDS or PUI.

yeah i was like why can't i find that tutorial that i thought he wrote where he talked about structs instead all i was finding was posts about him asking about structs....lol

edit: hmm...yeah i'll check out AIDS...i wish someone had a really great struct somewhere i could learn from...i never learned library until i found texttag system...now i need an equally good struct to learn struct from...
 

SanKakU

Member
Reaction score
21
oh, thanks for the redirection...i think i'll be occupied with editing your creeprevival struct for a while...
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
hehe that was in my thread... /random
Yeah, structs are confusing. I understand just about everything in vJASS, except for structs.
 

ZugZugZealot

New Member
Reaction score
33
Game wise, structs add no new functionality, and aren't any more MUI than global arrays are (in fact structs are just a compilation of globals).

Structs just offer a higher level of code language, which basically means it cuts a lot of monotonous work for you. There is an already existing concept of Structs without vJass at all, which is the location handle, which has the variables x and y. You could consider Location(x,y) as a struct constructor. GetLocationX(loc) could be considered an encapsulation method to acquire x where as x is a private variable in the struct. MoveLocation(x, y) could be considered as an encapsulation method to modify the variables. RemoveLocation(loc) could be considered the destructor for a struct instance.

With that said and done, you could say the location handle is a struct, that has methods to create, modify, and destroy data.
Here's the concept of the location handle as a struct...
JASS:
struct location
    private real x
    private real y
    
    static method create takes real x, real y returns thistype
        thistype loc = .allocate()
        set loc.x = x
        set loc.y = y
        return loc
    endmethod
    
    method move takes real x, real y returns nothing
        set this.x = x
        set this.y = y
    endmethod
    
    method getX takes nothing returns real
        return this.x
    endmethod
    
    method getY takes nothing returns real
        return this.y
    endmethod
endstruct

function Location takes real x, real y returns location
    return location.create(x, y)
endfunction

function MoveLocation takes location loc, real x, real y returns nothing
    call loc.move(x, y)
endfunction

function GetLocationX takes location loc returns real
    return loc.getX()
endfunction

function GetLocationY takes location loc returns real
    return loc.getY()
endfunction

function RemoveLocation takes location loc returns nothing
    call loc.destroy()
endfunction


JASS:
globals
    location previousLoc
endglobals

//We'll assume it has been given an initial value of some sort...

function SomeTriggerActions takes nothing returns nothing
    call CreateUnit( Player(GetTriggerPlayer()), GetLocationX(previousLoc), GetLocationY(previousLoc), face)
    call MoveLocation(previousLoc, x, y)
endfunction



So... Location already exists... Why bother? Well! That's pretty narrow minded, this just shows the idea that structs can treat things as objects... One thing I could easily say locations lack is their own Z value... Sure locations have "GetLocationZ()" function but that just acquires the elevation of the terrain the location is at. This is where an example can come into play... You could make a struct that does a location for 3D space for 3D systems, which in XNA is called a Vector3 (not sure what it's called elsewhere other than Autodesk programs, which it's a Point 3).

JASS:
struct Vector3
    real x
    real y
    real z
    
    static method create takes real x, real y, real z returns thistype
        local thistype v3 = .allocate()
        set v3.x = x
        set v3.y = y
        set v3.z = z
        return v3
    endmethod
    
    method move takes real x, real y, real z returns nothing
        set this.x = x
        set this.y = y
        set this.z = z
    endmethod
endstruct

You'll notice it has no methods for GetVector3X, GetVector3Y, and GetVector3Z... The beauty with structs, is the fact I've made it non-private, unlike locations, you can just cut the middle man and do something like so...

JASS:
function SomeTriggerActions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local Vector3 v3 = Vector3.create(GetUnitX(u), GetUnitY(u), GetUnitZ(u)
    call BJDebugMsg( "X = " + R2S(v3.x) )
    call BJDebugMsg( "Y = " + R2S(v3.y) )
    call BJDebugMsg( "Z = " + R2S(v3.z) )
    call v3.destroy()
endfunction


Oh... There is no GetUnitZ() function by default... GetUnitZ() is a custom function...
JASS:
function GetUnitZ takes unit u returns real
    call MoveLocation( dummyLoc, GetUnitX(u), GetUnitY(u) )
    return GetLocationZ(dummyLoc) + GetUnitFlyHeight(u)
endfunction
 
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