-
Notifications
You must be signed in to change notification settings - Fork 3
Structs
Structs were one feature that I really wanted to improve. They work pretty similar as they do in vJASS, but do keep in mind that they use Hashtable instead of arrays (you can avoid this by using static).
You only have to generate an unique integer id (you can use GetHandleId if you want):
globals
integer instances = 0
end
function allocate returns integer
set instances += 1
return instances
end
struct foo
public static method allocate returns foo
return cast allocate() to foo
end
end
Do keep in mind that an instance equal to cero means null (not allocated).
struct foo
method deallocate
call FlushChildHashtable(vrjass_structs, cast this to integer)
end
end
vrjass_structs
is a global hashtable created by the compiler where all the data of the structs is stored.
abstract struct foo
public method bar takes integer i returns nothing
public method someFeature takes nothing returns nothing
call this.bar()
endmethod
endstruct
struct baz extends foo
public method bar takes integer i returns nothing
endmethod
endstruct
In this example, the abstract foo
struct enforces child structs to implement a public method called bar
. We can also create methods that child structs are going to inherit (in this example, someFeature).
Modules were not implemented in vrJASS since abstract structs can accomplish the same.
In vJASS, to override a method it must be declared as stub. This is not the case in vrJASS
struct foo
public method baz takes nothing returns nothing
call BJDebugMsg("foo")
endmethod
endstruct
struct bar extends foo
public method baz takes nothing returns nothing
call BJDebugMsg("bar")
endmethod
endstruct
...
local foo instance = foo.create()
call instance.baz() // prints "foo"
set instance = bar.create()
call instance.baz() // prints "bar"
struct foo ...
struct bar ...
local foo f = foo.create()
set f = bar.create() // ERROR f must have a foo value
You can however use cast if needed
set f = cast bar.create() to foo
null
can be used in place of an instance so for example
function foo takes someStruct instance
endfunction
call foo(null)
Is valid.
If there is an onInit
static method vrJASS will call it automatically at map initialization.
Structs can contain arrays. Do keep in mind that they use Hashtable (they're not real array
s) but have the same length limit (8191)
struct foo
integer array bar
method someMethod
set this.bar[1] = 42
endmethod
endstruct
Struct members can be public
, protected
and private
. By default, if no visibility is specified, the member will be private
.
public: Can be used from anywhere.
protected: Can be used from child structs (inheritance).
private: Can only be used where it is declared.