Oct 11, 2013

vJASS for mules #6: Structs

You already know that there are several types in vJASS: Integers, reals, strings, booleans... It's also already been said we can create our own types. That's what structs are for.

In the previous lesson, we learnt about objects. It was given the dog example. In the real world, there are millions of different dogs, but all them own to the same specie. A struct(class) is like a specie. A model of how determined objects must behave.

Creating structs
We can create a struct throught the syntax:

struct StructName
 // code
endstruct

And now we can create a variable like that:

StructName var

Instances
Throught a single struct we can create undetermined objects that have different attributes (i.e, different skin's colors) but the same methods (i.e, all dogs barks). The process of create a new object (i.e, germinate a new dog) is called instantiation. We can do it throught the code:

StructName var = StructName.allocate()

Now we have a new object of kind StructName : )

Attributes
It's already been said that a object has attributes (i.e, skin's color).
Those attributes are define inside the struct. For every new instantiation, we define the values of those attributes (i.e, yellow, black, white...). See the example:

struct Dog
    string skinColor
endstruct

Dog rex = Dog.allocate()
Dog lassie = Dog.allocate()
set rex.skinColor = "orange"
set lassie.skinColor = "white"

Very simple, isn't it? In a next lesson, we'll learn why this way of to do it isn't a good idea.

Methods
Methods are actions and they are like functions. Also they're all the same of all instances of a struct, but since they work with attributes instead of arguments and those attributes varies of object to object, the output may be different. The syntax to create a method is:

struct Something
   ...
   method methodName takes argumentList returns returnType
      // Code
   endmethod
endstruct

The way we call it:

Something var = Something.allocate()
call var.methodName()

The constructor method
We saw that using the allocate method we can create a new object. But, sometimes, we don't want to simply create a new object, but pre-define its values. For example, in the dog example we have an attribute called "skinColor", and to define it we have to create a object and just after set its value. Now imagine if a determined class had ten or more attributes. It's very tiresome! So, that's why a constructor method is important. It create an object AND pre-define the attribute values. See the syntax:

struct Dog
   string skinColor
   static method create takes string skinColor returns thistype
       local thistype this = thistype.allocate()
       set this.skinColor = skinColor
       return this
   endemthod
endstruct

A lot of new keywords, yea? Don't despair! Now when we want to create a new dog, we use:

Dog rex = Dog.create("Orange")
Dog lassie = Dog.create("White")

A lot simpler.

This and thistype refference 
In the previous example, we saw the "this" keyword:

set this.skinColor = skinColor

What does it mean? Well, if you see, the create method takes an argument that's also called skinColor. So we have two variables called skinColor. How the program knows which one we want to use? That's what this and thistype are for.
We use "this" as an refference to an attribute. So, everytime we use "this" the program knows we are refferencing to a attribute.
We use "thistype" as an refference to a static attribute or method. We'll talk about it the next lesson.


Answer mentally:

  • What is a struct? How we create one?
  • What is instantiation? How we do it? 
  • What is a constructor method? 
  • For what this keyword is for?

<<Previous>>                                                                                  <<Next>>

No comments :

Post a Comment