JASS: An Introduction to Structs

Tom_Kazansky

--- wraith it ! ---
Reaction score
157
i have a question.In my map, i make a spell that uses a 0.04 timer to move a projectle, the problem is when i cast this spell many times at a same time, it causes lag. if i use structs to make this kind of spell, will i get that problem ?
 

The Undaddy

Creating with the power of rage
Reaction score
55
I basically got everything except the unitData.create().What does the method create do?Return a random integer or what?Also whats a unitData variable?
 

SFilip

Gone but not forgotten
Reaction score
634
Not random, it returns the first unused number (from 1 to 8190).
For example if you have three structs active (with indexes 1, 2 and 3) it will return 4.
 

NetherHawk

New Member
Reaction score
26
can there be an example of retrieving data o.o? do i have to create a struct for every trigger and every thing i want to attach some data to? do structs require anything in the header?
 

Technomancer

New Member
Reaction score
14
Ok - so for all of you that are asking what exactly a struct is, I'll lay down the law:

First of all you have to understand what happens when you declare a variable. Take this code:

JASS:
function omgDeclareVariable takes nothing returns nothing
local integer i = 0
endfunction


The middle line can be broken down into four parts:
local
Tells the compiler to rename this variable to a random code and delete it at the end of the function.
integer
Variable type is set to integer. The game cache is accessed, and however many bytes an integer takes up are reserved.
i
This is just a name you give your variable to identify it and make it unique.
= 0
An initializer. Without this, in higher level code, the space would be reserved for the integer, and whatever accessed the int would get whatever was in that space. = 0 clears the space.


Now, think of a struct as making your own kind of integer.
JASS:
struct potato
boolean redskinned
real weightingrams
real heightininches
real widthininches
endstruct


Now you can create a variable of type "potato", like "i" is an "integer". Really, it's all ones and zeros. Since the compiler doesn't know wtf a potato is already, you have to tell the compiler in advance how much memory to reserve for a potato, otherwise you could overwrite other potatoes, other integers, and half your operating system by running over the amout of memory allocated to you. You do this by declaring it's members (or the parts of the potato that you need to save data for). That way, the compiler knows to save room for one boolean variable (1 byte) and 3 real type variables (like 50 bytes or something, I dunno).

Now later, you have a new type of variable, not to repeat myself, but this is like an integer. So we can declare it like a variable.

JASS:
local potato Tater = potato.Create()


You don't have to type struct anymore, because the structure for the variable type is already defined for the compiler, so now it can just create a potato (again, just like creating an integer), reserve memory for it, and allow you to mess with it.

How to mess you ask? That's what methods are for. Methods are functions inside of a struct that allow you to access their data. Essentially, treat them just like that: functions inside of a struct. I'm not the WC3 struct master yet, so I don't know of you can directly access the variables, but the method functions you define should do all the manipulating for you (cleaner code), and be very clearly named, because you'll be using them alot.

Oh! I forgot to add. You call method functions by using the "." operator. The "." tell the compiler that THIS is a part of THAT. (THAT.THIS)

Example
JASS:
struct demo
integer demovalue
method DemoSetup takes nothing returns nothing
set this.demovalue = 3
endmethod
endstruct

function sillyfunction takes nothing returns nothing
local demo D = demo.Create() //create the struct
call D.DemoSetup()
endfunction


So that will create a "demo" variable, and then call it's setup function (aka method) which we created in the struct, which sets demo.demovalue = 3. We can't access demovalue without another method, so we'll have to create that later, but the syntax is all the same all over, to the letter, as making functions.

One more thing anything a method calls inside it's own struct needs to be called with this.whatever, because otherwise you get an error, and that's the plain and simple truth.
 

Light Alkmst

New Member
Reaction score
20
I've heard and used GameCaches to transfer and keep track of unit data (just like ppl used to), but how do structs do it? Would it become accessible like global data? How would I access the structs in timers where I wouldn't know the structs name, as in, how would I know which struct the unit had attached? This may have been in the tut, but my mind's too warped and tired right now, and I may have missed something...

and another question

what does setUnitUserData() do? Is it only used in vJass?

EDIT: I re-read it really quickly, so my guess is SetUnitUserData() binds the struct to the unit and GetUnitUserData() gets the struct bound to the unit?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
but how do structs do it? Would it become accessible like global data? How would I access the structs in timers where I wouldn't know the structs name, as in, how would I know which struct the unit had attached? This may have been in the tut, but my mind's too warped and tired right now, and I may have missed something...

This is where attachment systems come in... :p

Look a few up:

TimerUtils, CSData, HSAS, ABC, HAIL, ABCT, PUI, DUI, etc. There's tons of them, and many allow you to attach/ get the attached "integer" from a unit. Structs are actually integers, so you just attach the struct as an integer, and there you go.

This technique (referring to all the data inside a struct as a single integer) is called Encapsulation.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
struct ?= attachment system? i thought so...?

and can you check what's attached to a timer?

A struct is a 'wrapper' for all the contents. That way, instead of having to attach every single thing inside to a timer, you can just attach the struct itself.

Here's one attachment system for ya:

JASS:
library CSData

    //****************************************************************************************************
    // CSData 15.2
    // ¯¯¯¯¯¯¯¯¯¯¯
    // CSDatas are like UserData in units and items, they are faster than gamecache unless you have more
    // than 8191 handles in your map. In that case it would be a little slower but only for those
    // handles. And if you have more than 8191 handles your map is too slow already anyways.
    //
    // Notice that for public spells or systems to be distributed you should only use these
    // for private objects (those who the mapper would never have access to) If you are making something
    // for your map you can use them wherever you want.
    //
    // Best to be used in conjunction to CSArrays so you just specify an array id for a handle.
    //
    // DO NOT USE THIS ON THESE HANDLE TYPES: -lightning, -ubersplat, -image, -texttag,
    //                                        -any 'argument' handle (like playerstate, damagetype, etc)
    //
    //****************************************************************************************************

    //====================================================================================================
    globals
        private constant integer MAX_HANDLE_ID_COUNT = 50000
        // values lower than 8191: very fast, but very unsafe.
        // values bigger than 8191: not that fast, the bigger the number is the slower the function gets
        // Most maps don't really need a value bigger than 50000 here, but if you are unsure, leave it
        // as the rather inflated value of 408000
    endglobals

    //=================================================================================================
    // a.k.a H2I, changed name to CS_H2I to prevent conflicts with other systems, it then stayed that
    // instead of changing to a private or public function since many outside spells use it.
    //
    function CS_H2I takes handle h returns integer
        return h
        return 0
    endfunction

    //==================================================================================================
    globals
        private integer array csdata[MAX_HANDLE_ID_COUNT]
        private constant integer MIN_HANDLE_ID=0x100000
    endglobals

    //It is dependent on jasshelper's recent inlining optimization in order to perform correctly.
    function SetCSData takes handle h, integer v returns nothing
        debug if(CS_H2I(h)-MIN_HANDLE_ID>=MAX_HANDLE_ID_COUNT) then
        debug     call BJDebugMsg("SetCSData: Handle id too big, increase the max handle id count or use gamecache instead")
        debug endif
        set csdata[CS_H2I(h)-MIN_HANDLE_ID]=v
    endfunction

    function GetCSData takes handle h returns integer
        debug if(CS_H2I(h)-MIN_HANDLE_ID>=MAX_HANDLE_ID_COUNT) then
        debug     call BJDebugMsg("SetCSData: Handle id too big, increase the max handle id count or use gamecache instead")
        debug endif
        return csdata[CS_H2I(h)-MIN_HANDLE_ID]
    endfunction

endlibrary


And here's an example code:

JASS:
scope Example

struct Data
    unit U
    timer whichTimer
endstruct

function Callback takes nothing returns nothing
    local Data d = Data(GetCSData(GetExpiredTimer())) //Here we get the data that was attached to the timer.
    call Data.destroy(d)
endfunction

function Start takes nothing returns nothing
    local Data d = Data.create()
    set d.U = GetLastCreatedUnit()
    set d.whichTimer = CreateTimer()
    call SetCSData(d.whichTimer,d) //Here we attach the struct d to the timer
    call TimerStart(d.whichTimer,0.05,true,function Callback)
endfunction

endscope


The attachment system 'links' the two items together.
 

SanKakU

Member
Reaction score
21
i wish you guys who use comments in your jass scroll boxes would make additional comment lines so that i don't have to play scrolly scroll box throughout the entire tutorial...it's like right, left, right, left, right, left while trying to read a sentence it's really hard.
 

MagnaGuard

Active Member
Reaction score
49
I'm confused, I know a bit of JASS so excuse the obvious questions :)

>>Whered the statBonus come from? It wasn't declared, and how is it the purpose? " set this.statCredits=this.statCredits+statBonus"

>> Where'd this.RaiseInt() come from? Is it a JASS native :S

>> Whats this.ModifyStatCredits and Pow(this.kills/20) //.25 stats at 10, 1 stat at 20, 2.25 stats at 30, 4 extra stats at 40 kills

>> Can you only attach this to a created unit though triggers?
 

wraithseeker

Tired.
Reaction score
122
>Where'd this.RaiseInt() come from? Is it a JASS native :S

Just calling a function.


> Whats this.ModifyStatCredits and Pow(this.kills/20) //.25 stats at 10, 1 stat at 20, 2.25 stats at 30, 4 extra stats at 40 kills

Some maths.

> Can you only attach this to a created unit though triggers?

Yes..

> Whered the statBonus come from? It wasn't declared, and how is it the purpose? " set this.statCredits=this.statCredits+statBonus"

Remember, statCredits is a member of the struct and statbonus is the real that the function takes.
 

SanKakU

Member
Reaction score
21
it's been awhile but if i remember right it looks like a system like CSData among other things, makes up entirely new functions to be called. that's why when you try to call the functions in a spell that requires the system you get errors. normal jass doesn't have those functions, but they appear in the systems. i don't really understand any systems to tell you the truth, i've just used them.
 
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

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top