[vJass].allocate()

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
What is .allocate()?
How do we use it?
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
Well, if I do understand that manual, I dont need to get help here.
If everyone understands JASS, there is no need for all those Tutorial around.
 
Reaction score
456
Then, maybe, you could tell us what part of the explanation you don't understand. It's pretty damn fine English, if we don't count few typos.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
Why we are using .allocate() instead of .create()?

I saw something in TH, Hive and Wc3c.
  • ".allocate() is private, it should always in a struct"
  • ".allocate() gets a uniqed id for the struct"
  • "If the JassHelper processor detects none of these method being created, it will automatically create 1, refering to which method you using"
  • ".allocate() should always used inside a struct, but you may use it outside a struct too"
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
From my understanding (I haveyet to use advanced structs) .allocate gives the struct his special ID, kind of like .create. However, you can replace .create with other method, so you'll need to use allocate. For example

JASS:

struct r
    static method create takes nothing returns r
        return r.allocate()
    endmethod
endstruct


I'm pretty sure I'm wrong, so don't count on me. Maybe Überplayer would be kind enough to share his knowledge. :)
 
Reaction score
456
This might be wrong or totally wrong, and of course, this has a chance to be correct. I quote jass helper in this few times.

Things that I have to say before anything else is:
-structure is an integer
-think method as a function, which can take and return values


Example code:
JASS:
struct ourStruct
    static method create takes nothing returns ourStruct
        local ourStruct data = ourStruct.allocate()
        return ourStruct
    endmethod
endstruct

function createNewStructure takes nothing returns nothing
    local ourStruct data = ourStruct.create()
endfunction

When a struct does not have an specific create method declared, jasshelper will use allocate directly when .create is called.
Now that doesn't happen as we have a new create method inside our structure, so we have to create the structure by ourselves (not fully of course). .allocate() method creates us a unique INTEGER, which nothing else uses.

So basically, when we create a structure which contains static method create, .create() calls the method like a function. And that method returns us our structure.

This is quite unfinished.. As I'm gonna eat :cool:.. you should ask.
 
Reaction score
333
.allocate does basically the same thing as .create, and is useful if you want to define a custom create method for a struct.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Oh so it seems like I wasn't wrong.

JASS:
static method create takes nothing returns r
    return r.allocate()
endmethod


Can we do that? Or we must do:

JASS:
static method create takes nothing returns r
    local r returned = r.allocate()
    return returned
endmethod
 

Vexorian

Why no custom sig?
Reaction score
187
When you are using a "normal" struct withot a custom create method you call baba.create() this generates a unique id (if available) that you can use.

But then people wanted to customize create, by adding arguments or maybe just initializing values that can't be initialized that easily by = in member declarations, you could even customize it to the extend of not using vJass' usual allocation ways...

When people want to customize .create we run into a problem, since you need to ask vJass to generate a unique id for you, but you can't just call create() as usual because you are redefining it.

So, the solution was to add allocate() to the language, allocate() is just what a normal create() does, only that you cannot override it, it is also private which means you cannot use it outside an struct's methods.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
We cant define allocate as a method in struct...too?
 

Doomhammer

Bob Kotick - Gamers' corporate spoilsport No. 1
Reaction score
67
See structs as what they are after the preprocessor pass: global arrays connected by an integer. To make those arrays work, some sort of controller has been implemented: this makes sure that the arrays are initialized and connected correctly, and that you get a message on overflow
Ok, so let's look at an example of a simple allocator: that's my wrapper struct ) for the DestroyEffectTimed function after preprocessing:
JASS:

globals //pasted to show struct-generated globals
    integer si__tfx_F=0
    integer si__tfx_I=0
    integer array si__tfx_V
    effect array s__tfx_fx
endglobals

//Generated allocator of tfx
function s__tfx__allocate takes nothing returns integer
 local integer this=si__tfx_F
    if (this!=0) then
        set si__tfx_F=si__tfx_V[this]
    else
        set si__tfx_I=si__tfx_I+1
        set this=si__tfx_I
    endif
    if (this>8190) then
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Unable to allocate id for an object of type: tfx")
        return 0
    endif

    set si__tfx_V[this]=-1
 return this
endfunction


After this little procedure, it returns your struct, i.e. your unique struct id. this is your struct, get it now? Same principle, complementary procedure: what happens to structname.destroy() ?
JASS:

//Generated destructor of tfx
function sc__tfx_destroy takes integer this returns nothing
    if this==null then
            call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Attempt to destroy a null struct of type: tfx")
        return
    elseif (si__tfx_V[this]!=-1) then
            call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Double free of type: tfx")
        return
    endif
    set f__arg_this=this
    call TriggerEvaluate(st__tfx_onDestroy)
    set si__tfx_V[this]=si__tfx_F
    set si__tfx_F=this
endfunction
 
Reaction score
456
You cannot override it
That's what he said. And what would you even do with it x)

What would happen if the structure had same id?

JASS:
globals
    ourStruct array ourStruct_ar
    integer totalStructs = 0
endglobals

struct ourStruct
    string value
    static method create takes nothing returns ourStruct
        return ourStruct.allocate()
    endmethod
endstruct

function createNewStructure takes nothing returns nothing
    local ourStruct data = ourStruct.create()
    local integer index = 0
    set data.value = "Value of this struct is: "+I2S(GetRandomInt(0, 999))
    set ourStruct_ar[totalStructs] = data
    set totalStructs = totalStructs + 1
    loop
        exitwhen (index == totalStructs)
        set ourStruct_ar[index] = 0
        set index = index + 1 
    endloop
    set index = 0
    loop
        exitwhen (index == totalStructs)
        call BJDebugMsg(data.value)
        set index = index + 1 
    endloop
endfunction


I wrote that as fast as I could, and what it does.. is what you see in the code. It makes every structure exactly same :p
 

SFilip

Gone but not forgotten
Reaction score
634
What's the point of overloading create if you're only going to return allocate()? :rolleyes:
Actually what's the point of ourStruct_ar? You first set it and then revert to 0. Structs are pointers so it doesn't matter what you do with variables that hold them.

> We cant define allocate as a method in struct...too?
No, why would you ever need that?
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
>>No, why would you ever need that?
Nope, just asking :p

Conclusion
.allocate() is as same as .create().
Might use any of these 2 when create method is not customly defined.
When create method is defined, it'll need .allocate() to replace it when doing local StructName Namez = StructName.create().

Thats it.

Correct me if I am wrong. I do doubt my explaination
 
Reaction score
456
>No, why would you ever need that?
I answered the exact same thing. Yeah, and that my example was just a test.. Nothing.. just forget it :p

>.allocate() is as same as .create().
Not exactly.. You are able to override ".create()", but not ".allocate()". They work differently. ".create()" just automatically does ".allocate()", if you don't have your own static method create inside the struct, if you do have, then you need to do the allocating yourself.
 

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
>>Not exactly.. You are able to override ".create()", but not ".allocate()". They work differently. ".create()" just automatically does ".allocate()", if you don't have your own static method create inside the struct, if you do have, then you need to do the allocating yourself.
What i mean same is its function, what it does. (Creating a struct)
 
Reaction score
456
.create() automatically calls .allocate(), if you don't use your own static method create. That's basically the answer to you question.

And also, when we say creates a structure.. well.. :rolleyes:
 
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