Help with first Jass spell

Zeth

Member
Reaction score
3
All the structs have default methods (create, destroy, allocate...).
.allocate method gives the struct its unique instance number (when you reach the instance limit, it will return 0).

^ http://www.thehelper.net/forums/showthread.php?t=127853

then, If you have:

JASS:
local Data billy = Data.create(u1, u2)
local Data john = Data.create(u3, u4)


billy and john are the same struct, but each is unique and have an unique index number (wich you obtain with .allocate)
 

luorax

Invasion in Duskwood
Reaction score
67
3. The "method" basically means I can create global variables that can be used in all the functions of the spell, but are also local to this spell only?

No. The method is a simple function (like the other functions you used), but inside a struct. As i can see, you understood how do the struct members (variables) work. So, when you create a struct (XYZ.create, or with a custom allocator), every member (variable) and METHOD is created (except statics), and they will use the current variables. So, an example:

JASS:
scope ASD
    private struct Data

        unit caster
        unit target
        real time

        static method create takes unit caster, unit target returns thistype
            local thistype this = .allocate()
            set .caster = caster
            set .target= target
            set .time = 0.
            return this
        endmethod

        method getCaster takes nothing returns unit
            return .caster
        endmethod

        method onDestroy takes nothing returns nothing
            set .caster = null
            set .target= null
        endmethod

    endstruct

    function Test takes nothing returns nothing
        local Data d1 = Data.create(u1, u2)
        local Data d2 = Data.create(u3, u4)
        local unit nu1 = d1.getCaster()  //nu1 = u1
        local unit nu2 = d2.getCaster()  //nu2 = u3
    endfunction
endscope


See? Every object has its own (non-static) methods, and they will use their own members (variables)

1. The scope and private globals and struct basically makes all data inside this scope separate from all others, hence MUI?
Yes, but don't forget one thing: using "globals/endglobals" will create really globals, like you create in the trigger editor (but without the "udg_" perifx).

2. "thistype" is merely the name of the struct? so I could used "billy" if I wanted?

Yes-yes. But using "thistype" is much easier, especially, if you have a really big struct with a lot of <struct_name> variable/return, and you modify it's name.. it could make a lot of error. So, using "thistype" is much easier.
 

tommerbob

Minecraft. :D
Reaction score
110
Okay I think I understand all of that. One more quick question about methods:

Why is the first method called "static method" and the rest are just "method"?
 

luorax

Invasion in Duskwood
Reaction score
67
Each instance of a struct contains all of its own members + methods, but static members/methods're created once, at the map initalization. They're like the global variables. That's why they can use static members only (or using the "local thistype this =" we can set which instance do we want to use - but then we have to store the data for example using the "Table" system to get it later. If we use it, then we can use the dynamic [non-static] members too)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top