Correct syntax method operators []

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
It's been so long since I've vJassed, I've forgotten what the correct syntax was of this. I know the basic idea, but how would I write the method operator for something like this?

score would be the method operators name.

[lJASS]call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, I2S( u.score[PLAYER_SCORE_FOOD_MAXUSED] ) )[/lJASS]
 

Jesus4Lyf

Good Idea™
Reaction score
397
I don't think it is that simple -

You may define [] for a struct type, and you may define it for instances of that type. You may not define name[] (as far as I know). So what you must have for u.score[] is a struct "u" with a member which is also a struct named "score", with a non-static operator.

So:
JASS:
method operator [] takes handle h returns IHaveNoIdea
    return Something[GetHandleId(h)]
endmethod

You must figure out what you wish to store and how.

I bet that wasn't what you were after. Please tell us exactly what you are trying to do, and reveal your "u" and "score" structs, or whatever you have.
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
You're right, a better example is required but I thought I could name method operators which take [], but anyways here is a better example.
JASS:
struct user extends array
    player p
    // Something like this I had in mind:
    method operator score[] takes playerscore whichPlayerScore returns integer
        return GetPlayerScore( .p, whichPlayerScore )
    endmethod
endstruct

// Somewhere later..

call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, I2S( user(0).score[PLAYER_SCORE_FOOD_MAXUSED] ) )


So, what you're saying is the only way this is possible is having a struct, withing user named score and add a method operator to that.

Edit:
I hope this isn't the only way, of which I am willing to conciser that is.
 

Jesus4Lyf

Good Idea™
Reaction score
397
For teh lulz, try this:
JASS:
struct ScoreDummy
    method operator score[] takes playerscore whichPlayerScore returns integer
        return GetPlayerScore( Player(this) /* faster than array read - see Azlier */, whichPlayerScore )
    endmethod
endstruct

struct user extends array
    // Assuming Player(user[x])==Player(x)
    method operator score takes nothing returns ScoreDummy
        return ScoreDummy(this)
    endmethod
endstruct

I think that will inline... depends on order or parameters in struct methods. <_<
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I've been looking for something just like this as well. I figured out how to do it the way Jesus4Lyf described, but I just decided to exchange [ljass] u.score[PLAYER_SCORE_FOOD_MAXUSED][/ljass] (the nonexistent syntax "method operator score[]")with [ljass] u.score(PLAYER_SCORE_FOOD_MAXUSED)[/ljass] (the syntax "method score").

It's a very subtle difference, and while it may look a bit strange, I think it's the easiest way to fix it.
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
JASS:
    method operator score[] takes playerscore whichPlayerScore returns integer

    /* faster than array read - see Azlier */

Syntax error, ofcourse :p. Either way, when I changed it to [], the u.score[MAX_FOOD...] gave a syntax error. In other words, no dice.
And for that inline comment, what the hell is going on here. I thank you for the update but this is just ridiculous, seriously.
Could you perhaps link me to the post?

I've been looking for something just like this as well. I figured out how to do it the way Jesus4Lyf described, but I just decided to exchange [ljass] u.score[PLAYER_SCORE_FOOD_MAXUSED][/ljass] (the nonexistent syntax "method operator score[]")with [ljass] u.score(PLAYER_SCORE_FOOD_MAXUSED)[/ljass] (the syntax "method score").

It's a very subtle difference, and while it may look a bit strange, I think it's the easiest way to fix it.
This really breaks my heart. I've quit mapping for a long time, came back for just messing around and I come to the conclusion that this is a non existent syntax?!
Your fix kinda defeats the purpose, of it being a function instead of acting as a variable array.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
This really breaks my heart. I've quit mapping for a long time, came back for just messing around and I come to the conclusion that this is a non existent syntax?!
Your fix kinda defeats the purpose, of it being a function instead of acting as a variable array.

I know what you mean, I became pretty disappointed with method operators when I learned how limited they are. I know the fix defeats the purpose of using array-like syntax, but after all it is a function whether you were to write it with [] syntax or () syntax.
 

Jesus4Lyf

Good Idea™
Reaction score
397
>And for that inline comment, what the hell is going on here. I thank you for the update but this is just ridiculous, seriously.
>Could you perhaps link me to the post?

I agree and don't care for speed advantage. In fact, it is apparently circumstancial. I just was hoping you weren't doing the .p thing for a speed advantage. :)

Link. Have a read of the thread... *Shrugs*
 

Azlier

Old World Ghost
Reaction score
461
>Have a read of the thread... *Shrugs*

The fact that my benchmark was a mess, that the array was faster 70% (thus proving that the benchmark is garbage) of the time, and that we need a better benchmark are the results from that thread.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top