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

Do Structs generate their own unique ID

Status
Not open for further replies.
Level 9
Joined
Apr 23, 2011
Messages
460
I have been writing pseudo code now and I am almost 90% sure that when you instantiate a struct via structname.create() or thistype.create() it will assign that struct a unique reference, but I haven't coded in a long time, and have some uncertainty either way. If I store a variable as
JASS:
local thistype this = thistype.create()
this will reference the integer or class object of the struct instantiated correct? To clarify: If I were to create a struct reference, and store the data in a later function outside the struct, if the values are public, can I use something like this:
JASS:
globals
     timer t = CreateTimer()
endglobals
struct Test
     local thistype this = thistype.create()
     local integer i = 0
     local string hello = ""
     call SetTimerData(t, this)
     call TimerStart(t, 1., false, function Testing)
endstruct

function Testing takes nothing returns nothing
     local Test t = GetTimerData(GetExpiredTimer())
     set t.hello = "Hello!"
endfunction

This is riddled with syntax errors and problems, but hopefully you're looking at the underlying concepts. This is just to demonstrate.
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
that wont compile at all. you reference a timer as a "Test" struct. if u did

Test t = Test.allocate()

than it would compile...

e/ well not even close. a struct is not a function? wtf?
JASS:
globals
   Test testV = Test.create()
endglobals
struct Test
     integer i = 0
     string hello = ""
     timer t
     private static method Testing takes nothing returns nothing
          set this.hello = "Hello!"
      endmethod
     public static method create takes nothing returns thistype
          local thistype this = thistype.allocate()
          set t = CreateTimer()
          call SetTimerData(t, this)
          call TimerStart(t, 1., false, function thistype.Testing)
          return this
     endmethod
endstruct

http://www.wc3c.net/vexorian/jasshelpermanual.html#stru
 
Last edited:
Level 19
Joined
Aug 8, 2007
Messages
2,765
Yours won't run either >.>

Timer capitalized?
You can't instantiate a struct in a global variable... it can't see the function from up there
You use Testing, but create can't see testing
You use the this keyword inside of a stack method

Typed that in the reply editor -_-

also, assuming you mean "static" not "stack", check the locals block >.>

im not sure your right about the globals thing, i thought all global initializers are moved to a function at the very bottom during compilation
 
To answer your question: yes, sort of. Just not in that exact way.

When you pass the struct instance through to another function, and use GetTimerData(), then yes it will refer to that same struct instance. Yes, you will be able to receive the data of that instance.

Example:
JASS:
struct Test
    string s

    static method expire takes nothing returns nothing
        local thistype this = GetTimerData(GetExpiredTimer()) // retrieve the data 
        call BJDebugMsg(this.s) // should display "Hello World!"
    endmethod

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        local timer t = NewTimer()
        set this.s = "Hello World!" // set this.s
        call SetTimerData(t, this) // attach the data to the timer
        call TimerStart(t, 0.03125, true, function thistype.expire)
        return this
    endmethod
endstruct
 
Level 9
Joined
Apr 23, 2011
Messages
460
The more I look at what I'm actually trying to accomplish, it makes more sense to keep the function inside the struct anyways. So purge's example is pretty much the route I would take, which I have done before so it's not going to be anything new. The example I provided in the original post wasn't meant to compile, as explicitly stated. I used it as an example to convey a concept.
 
yes.

I think it goes likes this:
JASS:
 // We will have 3 example of structs here

struct One
endstruct
struct Two
endstruct
struct Three
endstruct

While trying this with JNGP with adding such errors,you will see this(idk if this is what it looks like,but i think i can remember):
JASS:
si__One = 1
si__Two = 2
si__Three = 3

Just got the idea from here:
http://www.hiveworkshop.com/forums/pastebin_data/qdpilj/_files/File IO.txt
http://www.hiveworkshop.com/forums/pastebin_data/qdpilj/_files/Network.txt
 
Status
Not open for further replies.
Top