• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

vJass, Decleareing structs, array of structs, and there useage, Questions

Status
Not open for further replies.
Level 12
Joined
Feb 23, 2008
Messages
587
Questions
1. Is this how I declare a local and global struct, and a local array of structs and a global array of structs?

2.Array of structs cant have methods. Correct?

3.If I wanted something like in array of structs (Values, And methods) vJass doesn't currently support anything like it, or any other warcraft scripting languages.

Here is my sample Code I Made, To help me understand how to work with structs

JASS:
function CreateLocalStruct takes nothing returns nothing
    
    //Create Local Object and run Constructor
    local Object myLocalObject = Object.create()
    
    //Call Meathods
    call myLocalObject.setX(100)
    call myLocalObject.addFiveToX()
    call DisplayTimedTextToForce( GetPlayersAll(), 30,  I2S(myLocalObject.getX()) )
        
endfunction

function CreateGlobalStruct takes nothing returns nothing

   //Create Global Object
    globals
        Object myGlobalObject 
    endglobals 
        
    //Call Constructor
    call myGlobalObject.create()
    
    //Call Meathods
    call myGlobalObject.setX(100)
    call myGlobalObject.addFiveToX()
    call DisplayTimedTextToForce( GetPlayersAll(), 30,  I2S(myGlobalObject.getX()) )
        
endfunction

function CreateLocalArrayOfStructs takes nothing returns nothing
   
   //Create Array Of "ObjectPlusArray"
   local ObjectPlusArray myOPA
   
   //You cant have a constructor
   
   //Access Variables Directly  
   
       //Meathod 1
       set ObjectPlusArray[0].x = 5 
       
       //Meathod 2
       set myOPA = ObjectPlusArray[0]
       set myOPA.x = 5
   
   //You Cant Call Meathods On Arrays Of Structs
   
endfunction

function CreateGlobalArrayOfStructs takes nothing returns nothing

   //Create Array Of "ObjectPlusArray"
    globals
        ObjectPlusArray myGOPA 
    endglobals 
   
   //You cant have a constructor
   
   //Access Variables Directly  
   
       //Meathod 1
       set ObjectPlusArray[0].x = 5 
       
       //Meathod 2
       set myGOPA = ObjectPlusArray[0]
       set myGOPA.x = 5
   
   //You Cant Call Meathods On Arrays Of Structs

        
endfunction


scope SetupGame initializer InitA 
    
    function  InitA takes nothing returns nothing
       
        call CreateLocalStruct()
        call CreateGlobalStruct()
        
        //It would be very un-wise To Have a local
        //and global Array of the same type of struct
        call CreateLocalArrayOfStructs()
        call CreateGlobalArrayOfStructs()
       
    endfunction
    
endscope

JASS:
library MyLibary

    struct Object
        //Varibles
        integer x
        
        //Constructor
        static method create takes nothing returns thistype 
        local Object r = Object.allocate() 
                                 
            set r.x = 0

            return r
        endmethod
        
        //Get
        method getX takes nothing returns integer 
            return this.x
        endmethod
        
        //Set
        method setX takes integer x returns nothing 
            set this.x = x
        endmethod
        
        //Action
        method addFiveToX takes nothing returns nothing 
            set .x = .x + 5
        endmethod
    endstruct
    
    struct ObjectPlusArray extends array
        //Varibles
        integer x
        
        //You cant have a constructor
        //You cant have methods
        
    endstruct
endlibrary
 
Level 9
Joined
Aug 2, 2008
Messages
219
Did you ever try to compile your sample code ?
However lets see if i can help you:
JASS:
struct aSample // <- This line acutally declares the struct

    integer value
    unit    dummy
    
    static method create takes nothing returns thistype
    //remember to write create fully in lowercase if you want override the default creator
        
        local thistype this = thistype.allocate() // <- Creates a new instance of the this struct
        
        set this.value = 555
        set this.dummy = null
        
        return this
        
    endmethod

endstruct

globals
    aSample blob //declares a globals variable of the type aSample
    
    //!Important! you cannot do something like: aSample blob = aSample.create()
endglobals


function MakeLocalInstance takes nothing returns nothing

    local aSample as //declares a local variable of the type aSample
    
    set as = aSample.create() //creates a new instance of the aSampel struct and assings it
                              //to the local variable as                           
endfunction


function MakeGlobalInstance takes nothing returns nothing

    set blob = aSample.create() //creates a new instance of the aSampel struct and assings
                             //it to the globals variable blob   
endfunction
I hope this helps you to understand how to declare structs, variables of struct types and the instantiation.

[!]Remember, All struct and interface types are integers !
So when all structs types are integers you can also do something like taht. But even when it´s possible to do something like this it´s not really recomendened:
JASS:
function IntegerCompatibilty takes nothing returns nothing

    local aSampel k = 3 // <- Jasshelper will tolerate that
    local integer d = aSample.create() //also accepted
    
endfunction
Array of structs cant have methods. Correct?
Wrong. Array structs are supposed to have user-made alloctation/creation and destrution method. They can use static methods and normal methods, they are just supposed to have user-made allocation/creation and destruction methods. That is also why you can´t have methods named allocate, create and destroy (and onDestroy) since they would refer to Vex algorithms. Another issue is that you cant have array members in structs which extends array. Furthermore you cannot initialize non-static members since you cannot call allocate. And another limitation is that another struct cannot extend a struct which extends an array (and therefore method overriding would be stupid).
JASS:
struct eXample extends array

    integer val
    real    wrong = 0.5 //0.5 won´t get initialized
    boolean res[3] //won´t work either
    
    static method allocate //will fail in any struct
    static method create ..... //will fail
    method destroy .....   //will fail aswell
    stub method Haha .... //stupid because this struct can´t be extended
    
    method MyMethod takes ...... // <- Works !
    static method MyStatic ..... // <- Also works
    static method onInit .....   // <- And this works too
    
endstruct

struct silly extends eXample //Can´t happen
If I wanted something like in array of structs (Values, And methods) vJass doesn't currently support anything like it, or any other warcraft scripting languages.
I don´t really understand what you mean. Could you clarify this ?

Oh and besides i suggest you should read the JassHelper manual might help you as well.
 
Status
Not open for further replies.
Top