private integer instanceCount = 0
private integer array stackRecycler
function CreateInstance takes nothing returns integer
local integer this
if (stackRecycler[0] == 0) then
//increase the counter (ensure unique instances)
set this = instanceCount + 1
set instanceCount = this
else
//pop last recycled instance off the stack
set this = stackRecycler[0]
set stackRecycler[0] = stackRecycler[this]
endif
return this
endfunction
function DestroyInstance takes integer this returns nothing
//push on the stack
set stackRecycler[this] = stackRecycler[0]
set stackRecycler[0] = this
endfunction
//a struct that generates 0 code in the background
struct A extends array
endstruct
//a struct that generates lots of code in the background (create, destroy, allocate, etc)
struct A
endstruct
//a function that goes into a struct and automatically takes the this argument of
//the struct type (integer)
method hello takes nothing returns nothing
//is
function hello takes integer this returns nothing
//a method that doesn't take the this argument
static method hello takes nothing returns nothing
//is
function hello takes nothing returns nothing
public integer h
and there was a variable MyStruct i
, then you could do set i.h = 5
.struct MyStruct extends array
public integer h
//is
//integer array h (within the scope of MyStruct)
endstruct
function Hello takes nothing returns nothing
local MyStruct i = 0 //a plain integer, but can act as a pointer to MyStruct arrays
set i.h = 5 //(set h[i] = 5, where the h is of MyStruct)
endfunction
struct MyStruct extends array
public static integer h
//is
//integer array h (within the scope of MyStruct)
endstruct
function Hello takes nothing returns nothing
local MyStruct i = 0 //a plain integer, but can act as a pointer to MyStruct arrays
set i.h = 5 //(set h = 5, where the h is of MyStruct)
set MyStruct.h = 5 (//also works since it doesn't require an index)
endfunction
public static integer array
within a struct.public
and private
access modifiers.struct A extends array
public method bleh takes nothing returns nothing
endmethod
endstruct
struct B extends array
private delegate A a //a delegate of type A, my syntax may be wrong here since I don't use them often)
endstruct
function DoSomething takes nothing returns nothing
local B b = 3
call b.bleh() //calls A(3).bleh(), or bleh(3) due to the delegate. Will only call it if the method doesn't already exist
//in the original struct, in this case B.
endfunction
struct unitdata
integer handleid
integer uid
real x
real y
real life
real mana
real maxlife
real maxmana
real facing
endstruct
thistype
) are exactly like integers ^^function action takes nothing returns nothing
local unitdata ud = unitdata.create() // We call a create function when we want to create an "object". There's also an .allocate() function that we use inside the struct if we want to add a "custom" create static method
set ud.uid = 'u001'
// ... bla bla bla
// If there's a "method" (not static) you call something like this:
call ud.somemethod
// If it's static:
call unitdata.somemethod
endfunction
If you use plain vJASS structs, you could be flooded with triggers and trigger evaluations.
scope Spell initializer Init
globals
private constant integer ABIL_CODE = 'A000'
endglobals
struct Data
// some variables
// ..
// ..
// ..
static method dmg takes nothing returns nothing
endmethod
static method filter takes nothing returns boolean
endmethod
static method expire takes nothing returns nothing
// for timer loops
endmethod
static method create takes nothing returns thistype
// bla bla bla bla
endmethod
method onDestroy takes nothing returns nothing
endmethod
endstruct
private function Action takes nothing returns nothing
// create struct
// do some actions
// start timer
endfunction
private function Cond takes nothing returns boolean
return GetSpellAbilityId()==ABIL_CODE
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
// register event
// register condition
// register action
set t = null
endfunction
endscope
struct Spell extends array
private static integer ic = 0
private static integer array ir
static method create takes nothing returns thistype
local thistype this
if (ir[0] == 0) then
set this = ic + 1
set ic = this
else
set this = ir[0]
set ir[0] = ir[this]
debug set ir[this] = -1
endif
return this
endmethod
method destroy takes nothing returns nothing
debug if (ir[this] == -1) then
set ir[this] = ir[0]
set ir[0] = this
debug endif
endmethod
private static method run takes nothing returns nothing
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerAddAction(t, function thistype.run)
set t = null
endmethod
endstruct
struct data
integer id
unit u //cast
real x
real y
//and so on,.....
structname.variable
?function actions takes nothing returns nothing
local data d = unitdata.create()
set d.id = 'H001'
//actions for spell.
endfunction
private integer instantCount = 0
and private integer array stackRecycler
?I'm starting out small I don't wanna move to advance right away. So please bare with me,
So after I make struct "data" I make another function to call those variable-like thingy inside the struct? How do I make them? is it structname.variable ?
function blekh takes nothing returns nothing
local somestruct data = somestruct.create()
// It has a unit variable called "u" in it
set data.u = GetTriggerUnit()
// if it was "static" (like in the case Nestharus gave):
set somestruct.u = GetTriggerUnit()
// WARNING: Logically, stuff like the "caster" in a struct should NEVER be static
// Static means constant/nonchanging
endfunction
function actions takes nothing returns nothing
local data d = unitdata.create()
set d.id = 'H001'
//actions for spell.
endfunction
function actions takes nothing returns nothing
local unitdata d = unitdata.create()
set d.id = 'H001'
//actions for spell.
endfunction
why use that (structs) instead of globals
struct SpellData
unit caster
real x
real y
static method create takes nothing returns thistype
local thistype this = thistype.allocate // A "thistype" is an instance
set this.caster = GetTriggerUnit()
set this.x = GetUnitX(this.caster)
set this.y = GetUnitY(this.caster)
return this
endmethod
endstruct
As for you Nestharus, I know your a good coder
When you give a struct a variable, if it's not static, you call it instancename.variablename
-> Should be:
JASS:function actions takes nothing returns nothing local unitdata d = unitdata.create() set d.id = 'H001' //actions for spell. endfunction
Call PauseTimer(???)
what are those for? Loops/Instances? I wanna learn about 'em.struct Data
// some stuff
endstruct
function blekh takes nothing returns nothing
local Data dat = Data.create()
// if a variable is static:
set Data.integer = 3
endfunction
struct A
static integer b = 0
integer c = 0
method change takes nothing returns integer
set A.b = A.b + 1
return A.b
//not sure if the variable "this" works with static members...
endmethod
method changec takes nothing returns integer
set this.c = this.c + 1
return this.c
endmethod
method loop takes nothing returns nothing
local A data = 1
call BJDebugMsg(I2S(data.Change))
//will show 1
call BJDebugMsg(I2S(data.Changec))
//will show 1
set data = 2 //changes the instance
call BJDebugMsg(I2S(data.Change))
//will show 2 even if we changed instances
call BJDebugMsg(I2S(data.Changec))
//will show 1 since we changed instance
endmethod
endstruct