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
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top