• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] module/struct instance problem

Status
Not open for further replies.
Level 13
Joined
Mar 19, 2010
Messages
870
Hallo hivers,

first... Happy new Year :)

Now, my problem, in the test map i wrote a short code snippet which shows my problem. I have a module and a member of it is a struct.
I created 3 instances of the module. After its created, there are 3 instances of the struct (1,2,3 and the values 1,2,3). After a timer delay all 3 instances of the module (1,2,3) has all the value 3 instead of 1,2,3!!! Why? After the creation its all good but after the timer its wrong!

What did I do wrong?
 

Attachments

  • test.w3m
    23.1 KB · Views: 48
Level 13
Joined
Mar 19, 2010
Messages
870
JASS:
scope TestScope initializer Init
    
    struct TestStruct
		private static integer stack = 0
		
		method getValue takes nothing returns integer
			return stack
		endmethod
		
		static method create takes nothing returns thistype
			set stack = stack + 1

			return stack
		endmethod
	endstruct

    module TestModule
        private timer t
        TestStruct test
        
        private static integer stack = 1
        
        private static method onTest takes nothing returns nothing
            local thistype this = GetTimerData(GetExpiredTimer())
            
            call BJDebugMsg("ONTEST ---> test-id: " + I2S(.test) + " - Value (create): " + I2S(.test.getValue()))
        endmethod
    
        static method create takes nothing returns thistype
            local thistype this = stack
            
            set .t = NewTimerEx(this)
            call TimerStart(.t, 5.0, false, function thistype.onTest)
            
            set .test = TestStruct.create()
            call BJDebugMsg("CREATE ---> test-id: " + I2S(.test) + " - Value (create): " + I2S(.test.getValue()))
            
            set stack = stack + 1
            return this
        endmethod
    endmodule
    
    private struct DefaultTest extends array
    	implement TestModule
    endstruct
    
    private function Init takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i == 3
            call DefaultTest.create()
            set i = i + 1
        endloop
    endfunction
endscope

attachment.php
 

Attachments

  • image.jpg
    image.jpg
    105.4 KB · Views: 182
In your create method you increment a static integer variable. (stack = stack + 1)
In method getValue you simply return this static variable, so it will be same in each call.

At start, when you create the instances, it prints "1,2,3".
And later the value never changed and is still "3", so will print "3, 3, 3".

static variable is like a global for the struct and does not belong exclusivly to one instance.
 
Status
Not open for further replies.
Top