• 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.

[Solved] static variable issue

Status
Not open for further replies.
Level 3
Joined
Nov 12, 2010
Messages
43
Hi , I was making a system for my map and got some troubles with static variables inside structs (this is because i have no idea how does it works)

JASS:
//sorry, I couldn't paste all my code because it was too long, but this is 
//what is happening  

library test initializer init
 struct myStruct
   private static player p
   private static real mainX
   private static real mainY
   private timer t
   
   static method something takes nothing returns nothing
        if .p == GetLocalPlayer() then
          call PanCameraToTimed(.mainX ,.mainY,0)
       endif     
   endmethod

  method forCamera takes nothing returns nothing
    set .t = CreateTimer()
     call TimerStart(.t,0.05,true, function thistype.something)
  endmethod
  
  static method onCreate takes player p , real x , real y returns thistype
    local myStruct this = myStruct.allocate()
    set .p = p
    set .mainX = x
    set .mainY = y
     return this
  endmethod

 endstruct
   // and my call 
 private function init takes nothing returns nothing
  local myStruct StructA = myStruct.onCreate(Player(0),100,300)//for this player works fine forcing the camera ,while there aren't more structs(instances) ,just StructA is created
  local myStruct StructB = myStruct.onCreate(Player(1),100,300)//when i try to get another instance for blue player ,camera doesn't works for red player  
  local myStruct StructC = myStruct.onCreate(Player(2),100,300)//when i try to get another instance for teal player ,camera doesn't works for red player and blue player 
 endfunction
 
endlibrary

seems like player static variable ".p" just can save the last value assigned ,plz can someone tell me how the static variables works?
:goblin_jawdrop:
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Static variables are associated with the class, not the instances of it. They're basically just globals that you reference by strucname.foo rather than foo. They're useful mostly for constants or classes that can't be instantiated.

In your case just remove the statics. However, you'll need a way of passing the struct instance to function "something" (use a hashtable).
 
Level 3
Joined
Nov 12, 2010
Messages
43
Just Globals OMG , thanks for the info man, I just used global arrays ,couldn't figure out how to use the hashtables in this case.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Here's the solution using hashtables. Untested so there might be a syntax error somewhere.

JASS:
library test initializer init
 struct myStruct
   private player p
   private real mainX
   private real mainY
   private static hashtable h = InitHashtable()
   private timer t
   
   static method something takes nothing returns nothing
        local thistype this = LoadInteger(.h, GetHandleId(GetExpiredTimer()), 0)
        if .p == GetLocalPlayer() then
          call PanCameraToTimed(.mainX ,.mainY,0)
       endif     
   endmethod

  method forCamera takes nothing returns nothing
    set .t = CreateTimer()
     call SaveInteger(.h, GetHandleId(.t), 0, this)
     call TimerStart(.t,0.05,true, function thistype.something)
  endmethod
  
  static method onCreate takes player p , real x , real y returns thistype
    local myStruct this = myStruct.allocate()
    set .p = p
    set .mainX = x
    set .mainY = y
     return this
  endmethod

 endstruct
   // and my call
 private function init takes nothing returns nothing
  local myStruct StructA = myStruct.onCreate(Player(0),100,300)
  local myStruct StructB = myStruct.onCreate(Player(1),100,300)
  local myStruct StructC = myStruct.onCreate(Player(2),100,300)
 endfunction
 
endlibrary
 
Level 1
Joined
May 28, 2011
Messages
4
Here's the solution using hashtables. Untested so there might be a syntax error somewhere.

JASS:
library test initializer init
 struct myStruct
   private player p
   private real mainX
   private real mainY
   private static hashtable h = InitHashtable()
   private timer t
   
   static method something takes nothing returns nothing
        local thistype this = LoadInteger(.h, GetHandleId(GetExpiredTimer()), 0)
        if .p == GetLocalPlayer() then
          call PanCameraToTimed(.mainX ,.mainY,0)
       endif     
   endmethod

  method forCamera takes nothing returns nothing
    set .t = CreateTimer()
     call SaveInteger(.h, GetHandleId(.t), 0, this)
     call TimerStart(.t,0.05,true, function thistype.something)
  endmethod
  
  static method onCreate takes player p , real x , real y returns thistype
    local myStruct this = myStruct.allocate()
    set .p = p
    set .mainX = x
    set .mainY = y
     return this
  endmethod

 endstruct
   // and my call
 private function init takes nothing returns nothing
  local myStruct StructA = myStruct.onCreate(Player(0),100,300)
  local myStruct StructB = myStruct.onCreate(Player(1),100,300)
  local myStruct StructC = myStruct.onCreate(Player(2),100,300)
 endfunction
 
endlibrary


Or you know.. use TimerUtils which has the functionallity built in.
 
Status
Not open for further replies.
Top