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:
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:
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