• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Littlebit stuck on strings/substrings usage

Status
Not open for further replies.
Level 19
Joined
Oct 12, 2007
Messages
1,821
Hello there.

I'm having an issue with strings/substrings and I was wondering if anyone here knows if my approach is right, or if I should do something completely different.

I want to create a function that checks if a unit has a specific 'classification'. Those custom classifications are added in the unit names in my map. So a unit with the name "Human, Cavalry, Wizard" would be a Human wizard on a horse.
Now that's just an example, but I thought because of this I should be able to use the GetUnitName function and work with substrings so I could run a function like this:
JASS:
call IsUnitClassified(u, "Cavalry")

This should end up returning true because substring (GetUnitName(u), 8, 14) should return "Cavalry".

I think my approach is right, but for some reason it doesn't seem to work. Maybe someone here knows what I'm doing wrong.

Here's my function's code:

JASS:
function IsUnitClassified takes unit u, string class returns boolean
    local integer ut = GetUnitTypeId(u)
    local string n = GetUnitName(u)
    local integer l = StringLength(class)
    local integer ln = StringLength(n)
    local integer i = 1
    local boolean check = false
    
    if class == n then
        set check = true
    endif
    
    loop
    exitwhen i+l-1 > ln or check == true
        if SubString(n, i, i+l-1) == class then
            set check = true
        endif
        set i = i + 1
    endloop

    return check
endfunction
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
In jass strings start at 0 not 1. So if you enter "Cavalry" C = 0, a = 1, v = 2, a = 3, l = 4, r = 5, y = 6.

Also never use if check == true. simply do if check. It is both faster and more efficient.

When posting a question you should also tell us what the current output is. You tell us what is expected but having a current output lets us solve it much much faster.
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
Thanks guys.

@edo494: I don't know what's better in any way. That's why I'm asking. It's just that I don't want to index all classifications for units in an index trigger. I already have to index a lot and this is much more convenient so I can just add it to the Object Editor Data, and the function solves itself by checking for the string.

@deathismyfriend: I suppose that's the problem then. I'll try that out. And you're true about the output part. I should've maybe given myself an errormessage to see what the strings look like while testing this.

Anyways. When I get up tomorrow I'll try things out!
 
Status
Not open for further replies.
Top