Structs - don't link a tut to me.

Kenoriga

Ultra Cool Member
Reaction score
34
  1. How do structs work as globals arrays?
  2. Which would be faster, globals or structs?
  3. Is there a global "struct" instead of local struct, or must you use globals/attachments?
  4. All those attaching systems - would they be slower than globals?
  5. You use 2 instances of the same struct - how many structs are you using, 1 or 2? (referencing to the limit of 819x)
  6. Overall, for timers mostly, which would be more efficient: globals or structs with those attachment thingies?
 

Steel

Software Engineer
Reaction score
109
  1. How do structs work as globals arrays?
  2. Which would be faster, globals or structs?
  3. Is there a global "struct" instead of local struct, or must you use globals/attachments?
  4. All those attaching systems - would they be slower than globals?
  5. You use 2 instances of the same struct - how many structs are you using, 1 or 2? (referencing to the limit of 819x)
  6. Overall, for timers mostly, which would be more efficient: globals or structs with those attachment thingies?

1) Structs are globals
2) Irrelevant (Actually it depends on your code)
3) You can create a global structs and use them whenever you choose
4) In most cases the user will never notice the difference, the struct attachment systems are fast and make things easy to manage
5) 2 structs pointing to the same global instance? I'm not sure I quite understand.
6) In my own experience it depends on your own code. In my expertise I had a system that was using globals, but with the use of structs, it allowed me to optimize my code a great deal.

Simple reason you can have something like:

JASS:

struct ABC
integer a
integer b 
integer c
endstruct

function foo takes nothing returns ABC
local ABC bar = ABC.create()
set bar.a=1
set bar.b=3
set bar.c=8
return bar
endfunction


This allows you to use structs in returns, and in takes. You can take a struct in as data and it allows you to use and recall all of the data.
 
Reaction score
333
  1. How do structs work as globals arrays?
  2. Which would be faster, globals or structs?
  3. Is there a global "struct" instead of local struct, or must you use globals/attachments?
  4. All those attaching systems - would they be slower than globals?
  5. You use 2 instances of the same struct - how many structs are you using, 1 or 2? (referencing to the limit of 819x)
  6. Overall, for timers mostly, which would be more efficient: globals or structs with those attachment thingies?

Answers are back to front so I can put the largest at the bottom of the page.

6. Obviously globals. Sadly globals are not MUI.
5. 1.
4. Yes.
3. Question makes no sense.
2. Globals. Structs are mainly used to make spells MUI.
1. This isn't the exact functionality, but gives you a rough idea (obviously this doesn't demonstrate features such as pseudo-inheritance, interfaces, methods, various other things):

Here we have a struct "Data" that holds 2 integers (int1 and int2).

First our globals:
JASS:
globals
    integer array Data_int1 // Struct members
    integer array Data_int2 //
    integer array Data_S // Integer stack for allocation
    integer Data_N = 0// Stack pointer for allocation
    integer Data_M = 0// Integer count for allocation
endglobals

Next our allocate (create) function:
JASS:
function Data_Alloc takes nothing returns integer
    if (Data_N == 0) then
        set M = M+1
        return M
    endif
    set N = N-1
    return Data_S[N]
endfunction

Now our destroy function:
JASS:
function Data_Free takes integer i returns nothing
    set Data_S[N] = i
    set N = N+1
endfunction

Demonstration (creates a Data struct, sets its members to 42 and 404 and then destroys it):
JASS:
set integer dat = Data_Alloc
set Data_int1[dat] = 42
set Data_int2[dat] = 404
call Data_Free(dat)
 

Vexorian

Why no custom sig?
Reaction score
187
1. How do structs work as globals arrays?
See last post


2. Which would be faster, globals or structs?
What tastes better, color "A" or the number Orange?

3. Is there a global "struct" instead of local struct, or must you use globals/attachments?

You can have global variables of struct types, if that's what you are asking about.

4. All those attaching systems - would they be slower than globals?
See my reply to question #2
5. You use 2 instances of the same struct - how many structs are you using, 1 or 2? (referencing to the limit of 819x)

One struct, two instances. you got 8188 instances left.

6. Overall, for timers mostly, which would be more efficient: globals or structs with those attachment thingies?

See answer for question #2.
 
Reaction score
456
>One struct, two instances. you got 8188 instances left.
Wasn't the limit pumped up to over 65000?
 

Gwypaas

hook DoNothing MakeGUIUsersCrash
Reaction score
50
No, you gotta declare it like.
JASS:
struct MyStruct[65526]
     real x = 2
     .....
endstruct


Btw the limit was pumped to above 400k :p
 
Reaction score
456
>Btw the limit was pumped to above 400k
400k is over 65k, which makes my question absolutely correct. "If I feel like talking, I never be wrong"..

Thanks for telling that, though :).
 

GoGo-Boy

You can change this now in User CP
Reaction score
40
Ohh I read about that array increase... but that it includes struct as well is very nice! Now people can use more array-members without being afraid to reach the limit oO
 

Gwypaas

hook DoNothing MakeGUIUsersCrash
Reaction score
50
You will never really reach the limit anyways, the only case would be huge array members but then you probably made something wrong in your code.
 
Reaction score
333
I remember DSG mentioning some array system where you can use globals and keep MUIness. It isn't too hard either.
http://www.hiveworkshop.com/forums/showpost.php?p=590178&postcount=13

But isn't too efficient if there are a lot of objects stored. ;)

I probably should have clarified what I meant before making a blanket statement like that. What that poster has made is essentially an attachment system, I had assumed the OP meant passing data directly with globals.
 

Vexorian

Why no custom sig?
Reaction score
187
I remember DSG mentioning some array system where you can use globals and keep MUIness. It isn't too hard either.
http://www.hiveworkshop.com/forums/showpost.php?p=590178&postcount=13

But isn't too efficient if there are a lot of objects stored. ;)

These terribly inefficient systems with linear complexity are on the rise, which is quite frightening, what's worse is that his timer's time is constant so a FIFO would work just as well.

Ohh I read about that array increase... but that it includes struct as well is very nice! Now people can use more array-members without being afraid to reach the limit oO
Be warned that extending the storage space to more than 8191 will force vJass to use function calls instead of array set and get for the struct, and it makes a big impact on performance. The good news is that you can use a constant, for example this is the way somebody should code systems now:


JASS:

//config:
globals
constant integer MAX_INSTANCES = 8191 //maximum instances supported by the system



//...

integer array V[MAX_INSTANCES] //just an example

endglobals

struct data [MAX_INSTANCES] //another example
//...
endstruct


So, for example if the limit stays as 8191, the system will use the fast array get and set . But if for some reason the user really needs more instances, he can just increase the value of the constant so he can choose between no limits and speed.
 

Kenoriga

Ultra Cool Member
Reaction score
34
Uh thanks all.
Looks like I shouldn't have ventured into how structs were made. Plus its enlightening to hear that the array limit is no longer 8190...

Few questions left:
  1. How do you declare global structs?
  2. Aren't gamecaches slow? Don't attachments use gamecaches?
  3. Damien says that globals are better, Steel gives a neutral point, I suppose... And Vexorian gives me a question that uhm...
 
Reaction score
333
[*]How do you declare global structs?
JASS:
globals
    SomeStruct dat
endglobals

Is that what you meant? Essentially, structs are just integers.
[*]Aren't gamecaches slow? Don't attachments use gamecaches?
Gamecache is fast enough to be usable, especially if you use the natives. Modern attachment systems are usually based purely on handle IDs and lookup tables, so they do not use gamecache (except occasionally as a failsafe).

[*]Damien says that globals are better, Steel gives a neutral point, I suppose... And Vexorian gives me a question that uhm...
[/list]

Plain global variables are the fastest possible way to pass data between functions. The reason attachment systems and the like exist is so that systems and spells can be made to be multi-instanceable.

Since almost every spell and system is based on global variables and arrays, it is hard to tell what someone means when they say "globals". I am assuming you refer to this sort of usage:

JASS:

globals
    unit spell_unit
endglobals

function TimerCallback takes nothing returns nothing
    // Do something to spell_unit
endfunction

function SpellCast takes nothing returns nothing
    set spell_unit = GetTriggerUnit()
    // Set up timer to call TimerCallback, etc.
endfunction
 

Vexorian

Why no custom sig?
Reaction score
187
Just see structs as your own created 'variable' types
JASS:
struct myStruct
    integer x=0
    integer y=0
endstruct

globals
    myStruct A
    myStruct myglobalname
    myStruct array V
endglobals

function test takes myStruct B returns myStruct
 local myStruct C=myStruct.create()
    set C.x=B.y
    set C.y=B.x
 return C
endfunction

function dostuff takes nothing returns nothing
 local integer i=0
    loop
           exitwhen i==100
           set V<i>=myStruct.create()
           set V<i>.x=i+20
           set V<i>.y=i-20
           set i=i+1
    endloop
    set A=myStruct.create()
    set A.x = V[30].x
    set A.y = V[15].y
    set myglobalname=test(A)
    call BJDebugMsg(I2S(myglobalname.x)+&quot; , &quot;+I2S(myglobalname.y))
    
endfunction

</i></i></i>



3. Damien says that globals are better, Steel gives a neutral point, I suppose... And Vexorian gives me a question that uhm...

Reply With Quote

The thing is, you are comparing oranges with apples.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top