• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Different between "method" and "function" ?

Status
Not open for further replies.
Level 9
Joined
Jan 3, 2010
Messages
359
Methods are functions associated with an instance of a struct.
Static methods are functions associated with a struct.
These are strictly vJass features, meaning if you don't use vJass then you cannot use them.

They are compiled to functions anyways, so there's technically no difference.

and what is struct ?? :D
i've read some vJASS tutorials about this struct, but i'm still confused ...

i just know that struct is used for creating a new variable type, is that true ?
 
It's important to note that Vexorian uses parallel arrays for structs. Though they are so well-written in syntax that they appear to actually be objects sometimes, they are not something mysterious.

You know how locations have to be created, can be moved, can get their x/y/z values and such? There is a struct called vector on wc3c.net that emulates a similar functionality. It's:

Base-3-real-array. real array x, real array y, real array z.

Creating a location requires the command line Location(x, y)
Emulating that behavior with vectors: vector.createTerrainPoint(x, y).

GetLocationX(loc) becomes vec.x.
GetLocationY(loc) becomes vec.y.
GetLocationZ(loc) becomes vec.z.

MoveLocation(loc, x, y) becomes: vec.getTerrainPoint(x, y)

RemoveLocation(loc) becomes: vec.destroy()

Point is, you can create something very useful with structs, which could never be this simple without vJass.
 
A struct is kind of like an object type, or a container, or class, or whatever.

Let's just say that you want to store the properties of a rocket ship. Instead of creating a bunch of spread variables and functions, you can store it all in one great package, say a struct called "RocketShip".

What you do is simply to state that you are makign a struct, like this:

struct RocketShip

endstruct

And then store the different properties you want into it:

struct RocketShip
real MaxSpeed
real CurrentSpeed
real Acceleration
integer Model
etc..

endstruct

And voila! You have all the variables stored in a struct, and through every instance od a struct, you can access it's different properties by using the dot operator.
for example, if you create a new RocketShip struct called Ship, you can acces it's MaxSpeed by roughly typing:

local RocketShip Ship = RocketShip.Allocate()
set Ship.Maxspeed = 10

And that's not the end of it!
You can then create methods for how the different variable values changes, or how the data is cleaned when the struct is destroyed (when the ship dies) and any other actions that might be related to your struct!

Finally, to put it to practical use, you can use the new hashtable functions to tie a struct to a unit, by using the new replacement native for what the return bug used to do (can't remember it's name) to get a unique integer that can be the struct's key in the hashtable.

I don't know if all of this is exactly correct, it was a while ago since i used vJass, but it's roughly what it is about.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
There is no difference between methods and functions. Both are saved as functions while saving a JNGP map.

The only difference is how JNGP's jasshelper handles them. It treats functions and methods differently when it saves them. Thus in the end both end up as functions but the process of how the functions are made varies from method to normal functions.
 
Level 9
Joined
Jan 3, 2010
Messages
359
JASS:
struct ship
    real x
    real y
endstruct

function foo takes ship p returns nothing
    set p.x = 10.00
    set p.y = 10.00
endfunction
function bar takes nothing returns nothing
    local ship p = ship.create()
    call foo(p)
    call p.destroy()
endfunction

so, there's no need to store the real "ship" in a variable then ?
just use a function that detect if it's a ship ...
variables in struct just for setting the inner data of that struct

i think i understand a bit ....
 
Yes, you know how a unit has attributes like HP, MP, level, etc.

Well, you pass a unit to a function (takes unit u) the same way you pass a struct to an argument (takes ship p). You can think of "x" and "y" permanently to "ship" the same way HP, MP, etc. are permanently attached to a "unit". Structs grant you the ability to make a brand new type.

Forget about the fact that they are "arrays" for now. In object-oriented programming, x and y are not arrays but are inherently bonded to the struct.
 
Level 11
Joined
Apr 29, 2007
Messages
826
You can use operators just like you would with a normal variable.

JASS:
    struct foo
        private real x = 0

        method operator X takes nothing returns real
            return x
        endmethod
        method operator X= takes real newX returns nothing
            set x = newX
            call BJDebugMsg(R2S(newX))
        endmethod
    endstruct

    function bar takes nothing returns nothing
        local foo test = foo.create()
        set test.X = test.X + 10 //Would pop out 10 on the screen
        call test.destroy()
    endfunction
 
What if you have a lineup like this:

JASS:
set x= GetWidgetX(this.unit)
set y= GetWidgetY(this.unit)
set z= GetUnitFlyHeight(this.unit)

//and in another method:

set x= GetWidgetX(this.unit)
set y= GetWidgetY(this.unit)
set z= GetUnitFlyHeight(this.unit)

//and in another method:

local vector v = vector.create(GetWidgetX(this.unit), GetWidgetY(this.unit), GetUnitFlyHeight(this.unit))

And this appears many times in your script.

You can use method operators to make your script look smaller than it is (enhancing readability), while still doing the same thing:

JASS:
method operator getx takes nothing returns real
    return GetWidgetX(this.unit)
endmethod
method operator gety takes nothing returns real
    return GetWidgetY(this.unit)
endmethod
method operator getz takes nothing returns real
    return GetUnitFlyHeight(this.unit)
endmethod

Now, your script can be shortened to:

JASS:
set x = getx
set y = gety
set z = getz
//and in another method:
set x = getx
set y = gety
set z = getz
//and in another method
local vector v = vector.create(getx, gety, getz)
 
Status
Not open for further replies.
Top