• 🏆 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!

[JASS] struct variable inside struct

Status
Not open for further replies.
Level 4
Joined
Oct 2, 2011
Messages
89
Is having a variable inside a struct of type another struct possible? I am trying to do this but get a compiler error.

If you don't understand what I mean, something like this:
JASS:
    struct test
        UnitArray store
        static method create takes nothing returns instance
            local instance temp = instance.allocate()
            set store = UnitArray.create(30)
            return temp1
        endmethod
        
        method onDestroy takes nothing returns nothing
            call store.destroy()
        endmethod
specifically I get an error when with "set store = UnitArray.create(30)"
UnitArray is from SortUtils in case the problem is with the struct, but I tried this with other structs and I get the same error. Am I doing something wrong or is this just not possible?
(http://www.wc3c.net/showthread.php?t=107903)
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
u should never use onDestroy just make the destroy method urself. ur create method takes nothing also it should be
JASS:
static method create takes nothing returns thistype 
    local thistype this = thistype.allocate()
return this

i also dont know were ur getting the UnitArray from ?
it should be unit array store

here is full thing it takes one unit per instance
JASS:
struct test
        unit array store
        
        static method create takes unit u returns thistype 
            local thistype this = thistype.allocate()
            set this.store = u
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.deallocate()
        endmethod
        
    endstruct

also structs are vJass so instead of using JASS tag use the vJASS tag
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
learn from this http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/structs-dummies-197435/

here is the one to use and get nestharus's Alloc Alternative http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-alternative-221493/ put it in library and ur good to go

JASS:
struct test extends array
        implement Alloc
        
        unit store
        
        static method create takes unit u returns thistype 
            local thistype this = thistype.allocate()
            set this.store = u
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.deallocate()
        endmethod
        
    endstruct

edit: the error this time was my fault. im not used to using structs without using the extends array

edit:
JASS:
library Alloc

    module Alloc
        private static integer array recycler
        private static integer instanceCount = 0
        debug private static boolean enabled = true
        
        static method allocate takes nothing returns thistype
            local thistype this = recycler[0]
            
            debug if (not enabled) then
                debug return 1/0
            debug endif
            
            if (this == 0) then
                debug if (instanceCount == 8191) then
                    debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"OVERFLOW") 
                    debug set enabled = false
                    debug set this = 1/0
                debug endif
            
                set this = instanceCount + 1
                set instanceCount = this
            else
                set recycler[0] = recycler[this]
            endif
            
            debug set recycler[this] = -1
            
            return this
        endmethod
        
        method deallocate takes nothing returns nothing
            debug if (not enabled) then
                debug set this = 1/0
            debug endif
            
            debug if (recycler[this] != -1) then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"ATTEMPT TO DEALLOCATE NULL STRUCT INSTANCE")
                debug set enabled = false
                debug set this = 1/0
            debug endif
            
            set recycler[this] = recycler[0]
            set recycler[0] = this
        endmethod
    endmodule

endlibrary
 
Level 4
Joined
Oct 2, 2011
Messages
89
Sorry for delay in post... I had to do something quickly

Here is error. UnitArray is from SortUtils library, it is a different struct. Link in first post
attachment.php
 

Attachments

  • error.png
    error.png
    46.9 KB · Views: 178
Level 4
Joined
Oct 2, 2011
Messages
89
Here is the struct. It is not nearly complete yet, as getting UnitArray is a key part to it. I had originally set it up to use a normal unit array and write my own sort, but this library has dynamic arrays and can easily sort so I want to use it. Because of this, the some of the methods may not have the right syntax for UnitArray yet...
JASS:
struct test2
        integer index
        UnitArray store
        //limit is 273 instances at once
        static method create takes nothing returns test2
            local test2 temp1 = test2.allocate()
            set store = UnitArray.create(30)
            return temp1
        endmethod
        method destroy takes nothing returns nothing
            call store.destroy()
            call this.deallocate()
        endmethod
        method addunit takes unit bunit returns nothing
            set store[index] = bunit
            set index = index + index
        endmethod

        method setturn takes integer active returns nothing
        
        endmethod
	
        method start takes nothing returns nothing
            local trigger t
            local integer a
            set a = 0
            set udg_Mode = 1
            //<cam + unit lock>
            //set t = CreateTrigger()
            //call TriggerRegisterTimerEventPeriodic(t,1)
            //call TriggerAddAction(t,function lockplayer)
            set t = null
            //</cam + unit lock>
            loop
                exitwhen a == index
                call PauseUnit(store[a],true)
                set a = a + 1
            endloop
            //<sort here>
            
            //</sort here>
            set a = 0
            loop
                exitwhen a == index
                call PauseUnit(store[a],false)
                //<actions>
                if(GetPlayerController(GetOwningPlayer(store[a])) != MAP_CONTROL_USER) then
                    //<pass unit to ai>
                    
                    //</pass unit to ai>
                endif
                //</actions>
                set a = a + 1
            endloop
            set udg_Mode = 0
        endmethod
    endstruct

EDIT: [vjass][/vjass] does not seem to work... what is the correct vJASS tag?
 
Level 4
Joined
Jan 27, 2010
Messages
133
static method = normal function (ish). Need to use <instance>.store instead of <this.>store

JASS:
local test2 temp1 = test2.allocate()
set temp1.store = UnitArray.create(30)
 
Status
Not open for further replies.
Top