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.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top