Structs

TheOverWhelm

Member
Reaction score
16
Nubby JASSER 'ere
I read some tutorials about Structs.
So that means I've read this
http://world-editor-tutorials.thehelper.net/cat_usersubmit.php?view=63595 [Structs tutorial]
And this
http://world-editor-tutorials.thehelper.net/cat_usersubmit.php?view=127853 [Newgen Addon]
I decided to start making a struct for a hero system (not even a system) stuff so I could just mess around with it, y'know, do whatever. Learning experiences ^_^
This 'system' pretty much does everything a hero is and modifies it accordingly.
the initial STR, INT, AGI, and the Raise Per Level and such.


At one point, the first link says
" method RaiseAgi takes nothing returns nothing //encapsulation specifies that we let functions do as little as possible on their own"
Why is this?
Reason I'm asking is that it really is doing as little as possible. I was just going to have my system automatically increase the whole "per level" thing but an outside trigger would need to call heroData.LevelUp() [I think that's how you'd call it!]
 

luorax

Invasion in Duskwood
Reaction score
67
I'm not sure if I got you. You don't need "outside" triggers, you can use them as members of the struct.

I really love examples, so I give you one:
JASS:
struct HeroData

    private static trigger onLevelUp
    
    readonly integer id=0
    readonly static integer Max=0
    integer startStr=0
    integer startAgi=0
    integer startInt=0
    integer perLevelStr=0
    integer perLevelAgi=0
    integer perLevelInt=0
    
    static method create takes integer id returns thistype
        local thistype this=thistype.allocate()
        set this.id=id
        set thistype.Max=thistype.Max+1
        return this
    endmethod
    static method getInstance takes integer id returns thistype
        local thistype this=0
        local integer i=1
        loop
            set this=thistype(i)
            exitwhen i>thistype.Max
            if this.id==id then
                return this
            endif
            set i=i+1
        endloop
        return 0
    endmethod
    private static method onLevel takes nothing returns boolean
        local unit u=null
        local thistype this=thistype.getInstance(GetUnitTypeId(GetTriggerUnit()))
        if this!=0 then
            set u=GetTriggerUnit()
            call SetHeroAgi(u,GetHeroAgi(u,false)+this.perLevelAgi,true)
            call SetHeroInt(u,GetHeroInt(u,false)+this.startInt,true)
            call SetHeroStr(u,GetHeroStr(u,false)+this.perLevelStr,true)
            set u=null
        endif
        return false
    endmethod
    private static method onInit takes nothing returns nothing
        set thistype.onLevelUp=CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(thistype.onLevelUp,EVENT_PLAYER_HERO_LEVEL)
        call TriggerAddCondition(thistype.onLevelUp,Condition(function thistype.onLevel))
    endmethod
endstruct


This will automatically refresh the hero's stats on leveling up. And you can set the basic stats easily:
JASS:
scope InitChampions initializer init
    private function Setup takes nothing returns nothing
        local HeroData h
        
        /*
        integer startStr
        integer startAgi
        integer startInt
        integer perLevelStr
        integer perLevelAgi
        integer perLevelInt
        */
        
        set h=HeroData.create('Hpal')
        set h.startStr=22
        set h.startAgi=13
        set h.startInt=17
        set h.perLevelStr=3
        set h.perLevelAgi=1
        set h.perLevelInt=2
    endfunction
    private function init takes nothing returns nothing
        call Setup()
    endfunction
endscope


Feel free to ask anything BTW.
 

TheOverWhelm

Member
Reaction score
16
Mmmm, good to know I've been doing it right so far!
The issue I had was what the comment was talking about encapsulation and having the function "do as little as posible on their own"
Was wondering if there was a real for that.
Also, does my create one make sense?

JASS:
    static method create takes unit newunit returns heroData
        local heroData newData = heroData.allocate() 
        set newData.u = newunit
        call newData.FixHero()
        call SetUnitUserData(newData.u,heroData.allocate())
        return newData
    endmethod


What does the local heroData newData = heroData.allocate() basically mean?
It seems weird to me. When I look at it as I see it as something similar to
local integer i = 0
Except fancier. -Shrug-
I think I did the SetUnitUserData? It's compiling just feels funny.


Also,
the allocate() returns the units custom integer
I thought "Ohhey what if I do.."
JASS:
    method FindUnit takes nothing returns integer
        return this.allocate()
    endmethod

So this function returns the custom integer of a unit? Cause it doesn't seem like it would..
Is it as fast or faster then checking the unit's user data? I feel like it's faster because it's done with variable. If people don't know, that's fine.

One last thing, when I don't do this.STUFF, if I don't put the this it works most the time. Does the method actually put it all in right without the this? Or do I need to only do the this one time per method?

Futurethanks!
 

luorax

Invasion in Duskwood
Reaction score
67
Hmm, so let's see:
>What does the local heroData newData = heroData.allocate() basically mean?

Structs're basically arrays and struct instances are basically integers. Let's say you have this:
JASS:

struct foo
    unit owner
    static method create takes unit ureturns thistype
        local thistype this=thistype.allocate()
        set this.owner=u
        returns this
    emdstruct
endstruct


"this" is always the actual instance you're working with, it's an integer as I've said before. "Owner" will be compiled as an array, and if you call "set this.owner=u" it'll compiled like this: "s_foo_owner[this]=u"
If you call "thistype.allocate()" it returns last available slot. So if you have 32 instantces created it'll return 33.

>The issue I had was what the comment was talking about encapsulation and having the function "do as little as posible on their own"
Encapsulation means data hiding. It depends on the member type what you can do with it. Example:
JASS:

struct A
    private static unit a
    readonly static unit b
    public static unit c
    method A takes nothing returns nothing
        set thistype.a=null
        set thistype.b=null
        set thistype.c=null
        //You can do each of these as you can read and write all your members in the struct
    endmethod
endstruct
struct B
    private static unit a
    readonly static unit b
    public static unit c
    method A takes nothing returns nothing
        set A.a=null       //'a' is private so you can not read or overwrite it
        set B.a=A.A       //'You can not do this either as 'A.a' is a private member
        set A.b=null       //'b' is readonly so you can not overwritewrite it - but you can read it
        set B.b=A.b       //'You can do this, as 'A.b' is readonly. The name is kinda self-speaking
        set A.c=null       //'c' is public, you can use it as you want - you can either read or overwrite it.
        set B.c=A.c       //'You can do this too
    endmethod
endstruct


>One last thing, when I don't do this.STUFF, if I don't put the this it works most the time. Does the method actually put it all in right without the this? Or do I need to only do the this one time per method?
You can leave this/thistype if you want. This/thistype will be automatically assigned to your method/member (thistype for static this for dynamic ones). But it's much more readable with this/thistype.

Hopefully I was clear enough (I was in a hurry - my friend've got some free time so we can work on our map now :))
 

TheOverWhelm

Member
Reaction score
16
I think I understand structs a whole lot more through you and testing ^_^
And, oh boy, I was allocating the unit twice in my Create method! Woops xD
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • 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 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