• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Advanced VJass Help

Status
Not open for further replies.
Level 15
Joined
Oct 16, 2010
Messages
941
1. Can i manually declare objects and add values to them (like in Java) using VJass?

2. What are methods and structs (what makes them different from normal functions).

3. What is the difference between one library "requiring" another library and "extending" it.

4. I've seen 'modules' declared inside libraries and then methods declared inside them. Why would I use a module, what does it do, and how would I use it?

Any help would be appreciated ^-^
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Can i manually declare objects and add values to them (like in Java) using VJass?

Yes

What are methods and structs (what makes them different from normal functions).

Structs are a sort of pseudo class and methods are just functions that take an extra parameter called this. Static methods don't take the extra parameter. Methods can only be in structs.

What is the difference between one library "requiring" another library and "extending" it.

You can't extend libraries, but you can certainly extend structs.

I've seen 'modules' declared inside libraries and then methods declared inside them. Why would I use a module, what does it do, and how would I use it?

A module is a high level text macro created just for structs. To use it, just do implement Module inside of the struct.



I wouldn't say this material is at all advanced for future ref ;D.



This tutorial shows what structs output and how to code efficient structs http://www.hiveworkshop.com/forums/...ls-280/coding-efficient-vjass-structs-187477/

This is for getting new gen set up http://www.hiveworkshop.com/forums/...ing-up-newgen-we-intro-trigger-editor-188349/

Also see

Full commented look into the background workings of structs

JASS:
//a struct that extends an array is code with the syntactic sugar of a struct
//this means that no extra code is generated

//structs normally generated create, destroy, and onDestroy
struct Hello extends array
    //instantiation can be a tricky thing. When a struct is created, you have to ensure
    //that the instance returned is unique. Furthermore, when a struct is destroyed,
    //you need to reuse that struct's instance to save on memory

    //instanceCount is a counter that continues to increase as new instances are created
    private static integer instanceCount = 0 //how many Hello's that are created

    //recycleCount is a counter that continues to increase as structs are destroyed
    //this is to ensure that destroyed instances are used the next time the struct
    //is created
    private static integer recycleCount = 0 //how many Hello ids that were recycled

    //recycle is an array that stored the destroyed instances
    private static integer array recycle //an array storing recycled Hello's

    //this is the create method, a method that creates unique instances of the struct
    //thistype is a keyword that translates into the struct's name
    //in this case, thistype would be Hello
    //if the struct was named BooYa, thistype would be BooYa
    public static method create takes nothing returns thistype
        //this is a keyword that refers to the current instance
        //static methods are like regular functions, so they don't get a this, which is
        //why I was able to declare one
        //static methods are called via struct.method()
        local thistype this

        //first check for any destroyed structs
        if (recycleCount > 0) then //if there are Hello's waiting to be reused
            //decrease the counter so that you are on the right slot in the array
            set recycleCount = recycleCount - 1 //dec counter

            //set the current instance to the destroyed struct
            set this = recycle[recycleCount] //use it
        else
            //if there are no destroyed structs, make a new one by increasing
            //the instance counter
            set instanceCount = instanceCount + 1 //0 is null, so first inc by 1

            //set the current instance to the instance counter
            set this = instanceCount
        endif

        //perform creation code here :O

        //return the created struct
        return this
    endmethod

    //destroy is called when the struct is to be destroyed
    //this is not a static method
    //a non static method translates to this (using destroy as an example)
    //public static method destroy takes thistype this returns nothing
    //the instance of the struct is automatically passed in
    //they are called via structInstance.method()

    public method destroy takes nothing returns nothing
        //remember this is passed in, so it exists!
        //store this into the recycle variable
        //the first struct destroyed would be recycle[0] = this
        set recycle[recycleCount] = this
        //increase recycle counter
        set recycleCount = recycleCount + 1

        //if create was called, recycleCount would be decreased back down to 0
        //and recycle[0] would be used

        //put destruction code here
    endmethod
endstruct

Above allows this
JASS:
local Hello h = Hello.create()
call h.destroy()
 
A struct is a kind of class as nestharius said, or container, that can include several different variables under a single struct name.
A method is a function that manipulates values inside the struct it belongs to.

I reccomend you read the "jasshelper manual" which you can find inside the JNGP directory (in the jasshelper folder). It has everything you need to know.
 
Status
Not open for further replies.
Top