• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Operators

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
What happens is that the system splits up a chat string into an array of strings "words" but I want to replace this array of string with array of start and end positions as integers are much smaller to store and I am already saving the entire chat string.

So to remove this duplication of data I wanted to use opreators. But I can't quite figure out how to write them. I'm also trying to avoid regular methods here.

Here is psuedo code I wrote on notepad (since WEX is on vacation, and I got no other tool to write jass in...)
JASS:
struct PlayerChat
    string chatStr
    string word[100]            // I want to replace this
 
    integer wordStart[100]        // With this
    integer wordEnd[100]        // While still keeping the syntax that i had before: playerChat.word[n]
 
 
    method operator word[] takes integer index returns string
        return SubString(.chatStr, wordStart[index], wordEnd[index])
    endmethod
 
    methd operator wordLength[] takes integer index returns integer
        return .wordEnd[index] - .wordStart[index]
    endmethod

    method oprator wordLower[] takes integer index returns string
        return StringCase(SubString(.chatStr, .wordStart[index], .wordEnd[inde]), false)
    endmethod

So the syntax I'm trying to acomplish here is:

playerchat.word[3]
playerchat.wordLength[3]
playerchat.wordStart[3]    // Already exists
playerchat.wordEnd[3]    // Already exits
playerchat.wordLower[3]
playerchat.wordUpper[3]

endstruct

Hope it was clear what I want to achieve.
 
Last edited:
i believe vjass doesn't allow that--its operator syntax is pretty light. One thing you can do, however, is store "word" as a struct-type instead of a string, and give that struct-type an operator overload. Here is an example of what I mean:
JASS:
struct StringSlice 
    string original
    integer lowerBound

    method operator [] takes integer upperBound returns string
        local string result = SubString(original, lowerBound, upperBound)
        call this.destroy()
        return result
    endmethod

    static method create takes string original, integer lowerBound returns thistype
        local thistype this = thistype.allocate()
        set this.original = original
        set this.lowerBound = lowerBound
        return this
    endmethod

endstruct

struct String
    string value
     
    method operator [] takes integer lowerBound returns StringSlice
        return StringSlice.create(.value, lowerBound)
    endmethod

    static method create takes string value returns thistype
        local thistype this = thistype.allocate()
        set this.value = value
        return this
    endmethod

endstruct


// ... this allows you to do fancy stuff like
local String str = String.create("Hello World")
call BJDebugMsg(str[0][5]) // this would print "Hello"

that might be a better way to encapsulate a "start" and "end" index, instead of relying on them being provided separately

but the examples you gave are very confusing--it might be helpful to show a practical example of how your API would be used in a map.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Ah then, getters it is. Just wanted everything to look unison. Not sure I liked the idea of allocating lots of new structs for this Purge, though the idea was interesting. As for what I'm doing, it can be found in my signature :) --> [vJASS] - Command Utilities

A player enters a chat message, slice it up into words that are easily accessible. So the purpose here was literally just to remove the need of storing the sliced words in string-arrays. Come to think of it another optimization would be to only slice it up if the getWord method is used. A drawback of not storing the words is that you have to use SubString each time you need a word though... Not sure if this change is for the better really.

Any comment on this optimization, is it at all good or sub-optimization and not so needed given what it does?
 
Last edited:
Status
Not open for further replies.
Top