[vjass] something.something.something

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,288
I have no idea what it's called I just know that it's possible, thus the odd title.


How can you do something like:
set next.next.next.next = 5
or
call something.something.something.method()


I recall this being done in some linked list and also in the text adventure which was posted in the lab a while back.
I suppose it has to do with struct extends array, but I might as well be completely wrong.
 
That sounds like my kind of code :D

The basic idea is structs.
Structs are basically a blueprint of an object.
Objects can have functions or variables and you can access them by using the dot operator:
JASS:
struct Object
    unit u
endstruct

...

local Object obj
set obj.u = GetTriggerUnit()

You can also have structs as variables:
JASS:
struct Object
    unit u
    Object obj
endstruct

...

local Object obj
set obj.obj.u = GetTriggerUnit()

Things can get really unreadable when you do it a bit too far:
call GroupAddUnit(g, b.ref.ref.ref.ref)
But most of the time, it is just for readability and a nice way of coding.

Structs extending arrays is just so the basic struct functionalities are not automatically created.

For example, to create a new instance of your struct (in fact to lock an index so it wont be used multiple times), you can do local Object obj = Object.allocate()
When you extend a struct, the allocate() and deallocate() methods are not created.
(There is a lot more that is not created so if you use instances of the struct and are not really sure what extends array does, you shouldnt use it... yet.)
 
Okay I made some text code.
The x.x.x.x.x part seems to work my only issue with this test is that I had to use a bool to prevent the code from crashing.
JASS:
//! zinc
	library x
	{
		struct test
		{
			thistype next;
			integer value;
			
			static thistype last;
			
			static boolean first = true;
			
			static method create(integer i) -> thistype
			{
				thistype this = thistype.allocate();
				value = i;
				if(first == false)
				{
					last.next = this;
				}
				else
				{
					first = false;
				}
				last = this;
				return this;
			}
		}
		
		function onInit()
		{
			test h = test.create(0);
			integer i = 1;
			while(i < 5)
			{
				test.create(i);
				i += 1;
			}
			BJDebugMsg(I2S(h.value));
			BJDebugMsg(I2S(h.next.value));
			BJDebugMsg(I2S(h.next.next.value));
			BJDebugMsg(I2S(h.next.next.next.value));
			BJDebugMsg(I2S(h.next.next.next.next.value));
		}
	}
//! endzinc
 
Status
Not open for further replies.
Back
Top