Discussion Vanilla Structs?

tooltiperror

Super Moderator
Reaction score
231
Today I got thinking. It would be logical to make structs in vanilla JASS.

Think of a struct, what is it but a number? Then you use the number, or the name of the object, to reference arrays. So let's say we have two real arrays.
JASS:
 real array udg_X[8191]
 real array udg_Y[8191]


Then we have a global integer array: the struct. Let's call it [ljass]udg_structCount[/ljass], for example. Then we could just create make a simple function, or we could even just instruct people to set variables at the end of their functions.
JASS:
.
 function endStruct takes nothing returns nothing
     set udg_structCount = udg_structCount + 1
 endfunction


Or, we could even set the global count to an integer, to give the struct a name. We would need to revise the above function to return an integer.

JASS:
.
 function InitStruct takes nothing returns nothing
     set udg_structCount = udg_structCount + 1
     return udg_structCount - 1
 endfunction



JASS:
 function example takes nothing returns nothing
     local integer somelocation = InitStruct()
     // Now we get the information using the somelocation.
     local real x = udg_X[somelocation]
     local real y = udg_Y[somelocation]
 endfunction


Now, to make it even more complicated and interesting, we could use hashtables instead to save a value as a key or something like that, and with a more simple syntax and a cleaner interface, this could be a complete system for vanilla JASSers to use structs, and we could make it easily customizable.

Thoughts?

Edit: I call it SugarStructs, or SuStruct for short, or even SS.

EditEdit: Another thought, what about using a key from a hashtable instead of recycling a global variable?
 

SerraAvenger

Cuz I can
Reaction score
234
No.
I already had posted how it works.

I'll try to remember, since I don't know where.
Yeah, this would work with hashtables, but since array syntax is much nicer I'd use arrays instead.

btw, SS and SA are not your friends in germany.

I think it should work like this:

JASS:
globals 
  integer array VS_FreeIndicies
  integer VS_NextFree = -1 
  integer VS_NextNew = 1
endglobals

function IAlloc takes nothing returns integer
  local integer index
  if VS_NextFree != -1 then
    set index = VS_FreeIndicies[ VS_NextFree ]
    set VS_NextFree = VS_NextFree - 1 
  else
    set index = VS_NextNew
    set VS_NextNew = VS_NextNew + 1
  endif
  return index
endfunction

function IFree takes integer index returns nothing
  set VS_NextFree = VS_NextFree + 1
  set VS_FreeIndicies[ VS_NextFree ] = index
endfunction


EDIT: Okay this would work, would recycle, but ignores any 8k limit. One thing: The number of freed indicies should never excess 8k. This would only happen if 8k indicies were freed in a row without reallocation.



This is a "safer", slower, array centered version:

JASS:
globals 
  integer array udg_VS_FreeIndicies
  boolean array udg_VS_Allocated
  integer udg_VS_NextFree = -1 
  integer udg_VS_NextNew = 1
  integer udg_VS_MaxNew = 8191
endglobals

function IAlloc takes nothing returns integer
  local integer index
  if udg_VS_NextFree != -1 then
    set index = udg_VS_FreeIndicies[ udg_VS_NextFree ]
    set udg_VS_NextFree = udg_VS_NextFree - 1 
  elseif udg_VS_NextNew < udg_VS_MaxNew then
    set index = udg_VS_NextNew
    set udg_VS_NextNew = udg_VS_NextNew + 1
  else  
    set index = 0
  endif
  set udg_VS_Allocated[ index ] = true
  return index
endfunction

function IFree takes integer index returns nothing
  if udg_VS_Allocated[ index ] then
    set udg_VS_Allocated[ index ] = false
    set udg_VS_NextFree = udg_VS_NextFree + 1
    set udg_VS_FreeIndicies[ udg_VS_NextFree ] = index
  endif
endfunction



JASS:
function example takes nothing returns nothing
     local integer test_index = IAlloc()
     // Now we get the information using the somelocation.
     local real x = udg_X[test_index]
     local real y = udg_Y[test_index]
     if udg_VS_Allocated[test_index] then
        call IFree( test_index )
     endif
endfunction


EDIT2:
Now in fact this can be advanced and advanced, but believe me, you can be glad if a preprocessor does the advancement for you.

A couple of fun thoughts though:

Code:
API += "
function ISet%s takes integer index, string name, %s value returns nothing
   call Save%s( udg_VS_VALUES, index, StringHash( name ), value )
   call SaveString( udg_VS_TYPES, index, StringHash( name ), "%s")
endfunction" % [ var_type.capitalize, var_type, var_type.capitalize, var_type ]
API += "
function IGet%s takes integer index, string name returns  %s 
   return Load%s( udg_VS_VALUES, index, StringHash( name ) )
endfunction" % [ var_type.capitalize, var_type, var_type.capitalize ]
API += "
function IGetType takes integer index, string name returns string 
   return LoadString( udg_VS_TYPES, index, StringHash( name ) )
endfunction" % [ var_type.capitalize, var_type, var_type.capitalize ]



JASS:
function example takes nothing returns nothing
     local integer test_index = IAlloc()
     // Now we get the information using the somelocation.
     call ISetReal( test_index, "X", 5.0 )
     call ISetReal( test_index, "Y", 5.0 )
     call ISetString( test_index, "Y", "hi" )
     call BJDebugMsg( IGetType( test_index, "X" ) ) // -> real
     call BJDebugMsg( IGetType( test_index, "Y" ) ) // -> string
     call IFree( test_index )
endfunction


yeah. And that was the fun.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Another thought, what about using a key from a hashtable instead of recycling a global variable?
It will become handle vars. No point to do so.
 

Nestharus

o-o
Reaction score
84
Another topic of more interesting is changing what type of variable the pointer is. For example, in a regular struct it'd be an int, but what if you wanted to change it to let's say a unit to do something crazy like this?

Oh, and treat my structs as [ljass]struct extends array[/ljass]. I always write them from scratch, I'm one of those insane people =P.


JASS:
struct Hi extends unit {
    public string operator name() {
        return GetUnitName(this)
    }
}



And it'd be just as interesting if you could just do this instead..

JASS:
public string operator name() : unit {
    return GetUnitName(this)
}

public string operator name() : player {
    return GetPlayerName(this)
}


That last example makes more sense than the first one for moving natives into an OO design without any overhead = ).

It's too bad nobody's done this yet = ).


Also if structs could always refer to struct extends array and classes could refer to standard structs, that too would be rather epic ; ).
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I think structs should be left to JassHelper, since it does all the heavy coding for you, and gives your syntax for it, so you don't have to do it manually.
 

SerraAvenger

Cuz I can
Reaction score
234
I think structs should be left to JassHelper, since it does all the heavy coding for you, and gives your syntax for it, so you don't have to do it manually.

Yeah quite much.
Don't forget though, If you want to write a parser yourself, this might be useful.
 

tooltiperror

Super Moderator
Reaction score
231
Serra, all your examples are terrible.

The point of Vanilla structs are for those who can not use vJASS to use vJASS. Why would I create a system for those who can not use vJASS that uses vJASS?
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Most people can use JassHelper. Those who can't are Mac uses (Linux can via Wine). The majority of map makers are Windows, with a small precentage on Linux, and an even smaller precentage on Mac.

The modding community just isn't made for Mac and Linux users. Sure, Linux can do some of the stuff Windows can, but not quite all of it.
 

SerraAvenger

Cuz I can
Reaction score
234
Serra, all your examples are terrible.

The point of Vanilla structs are for those who can not use vJASS to use vJASS. Why would I create a system for those who can not use vJASS that uses vJASS?

Wait? Where does my code require vJASS?
Ofc the first one did, I noticed lateron that you don't have globals declaration freedom in JASS, so I named them udg_[NAME] in the second example.
Ofc, there's a "globals block", but that's not really there. It's just easier to read that way.
You can always just introduce them via variables editor...
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
Most people can use JassHelper. Those who can't are Mac uses (Linux can via Wine).
You can on Mac with WINE also, but it's still a sucky workflow (can't enter vJASS code directly into the editor).

The majority of map makers are Windows, with a small precentage on Linux, and an even smaller precentage on Mac.

The modding community just isn't made for Mac and Linux users.
How is that in any way a reason for him to not investigate coding techniques for those users?

As for the topic at hand: I basically just make my own "structs" out of a bunch of global arrays and do the indexing myself when necessary (though with unit indexing, it's not often that I have to write any index recycling code per spell.)
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top