• 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.
  • Create a faction for Warcraft 3 and enter Hive's 19th Techtree Contest: Co-Op Commanders! Click here to enter!
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 21st Texturing Contest: Upgrade is now concluded, time to vote for your favourite set of icons! Click here to vote!

Detecting if string is not real?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
A function on my map is supposed to convert strings to real. But I don't know how to detect if a string is not a set of correct characters.

"1.11" - correct
"3.00 - correct
"3" - correct
"0.01" correct
"aaa" incorrect
etc.


- I want a player to type -tribute red (real) but if there is no real I need to detect it.
- Negative values don't work (return negative)
- Values > 3 (return 3.00)
- Text don't work (returns negative)
 
If you typecast no valid input to type <real>, then you never get null, always 0.00. That's what a real is.

But you can check if substring is bigger than 0 and lower than the max value:

  • Set String = SubstringEnteredChatString(x, y)
  • (If String == "0")
    • Then - Actions
      • ...
    • Else - Actions
      • Set real = S2R(String)
      • If (real < 256) and (real > 0) then DoActions
Substring should include every character from where you expect a number until StringLenght.

If Then - Actions is fired, the player typed "-command 0".
If Else - Actions is fired, the player typed something different, so then we check if it's a valid value. (bigger 0 and smaller than 256)
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
If you typecast no valid input to type <real>, then you never get null, always 0.00. That's what a real is.

But you can check if substring is bigger than 0 and lower than the max value:

  • Set String = SubstringEnteredChatString(x, y)
  • (If String == "0")
    • Then - Actions
      • ...
    • Else - Actions
      • Set real = S2R(String)
      • If (real < 256) and (real > 0) then DoActions
Substring should include every character from where you expect a number until StringLenght.

If Then - Actions is fired, the player typed "-command 0".
If Else - Actions is fired, the player typed something different, so then we check if it's a valid value. (bigger 0 and smaller than 256)

Okey, like a printer that if it detects an error trashes the whole paper. The problem with your method is that 0 is a valid number to type. But come to think of it. If it returns 0 and entered string is not "0" or "0.0" or "0.00" it would return -1.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Does all work properly? :)

Like so?

JASS:
function TributeValue takes string s returns real
    local real r = S2R(s) 
    // Test for bad input
    if (r == 0 and s != "0" and s != "0.0" and s != "0.00" and s != "0.00" and s != "0.000") then
        set r = -1
    endif
    return r
endfunction

Is there a action for rounding reals want to keep them in decimal form 3.00 to 0.00

... I managed to git rid of the last decimal.

  • Actions
    • Set t_String = 3.3333
    • Set t_r = (Real(t_String))
    • Set t_String = (Substring((String(t_r)), 1, 4))
    • Game - Display to (All players) the text: (String((Real(t_String))))
 
You also want to check if value is between 0 and 255 or? And 0 should be valid, right? If Command was "-red value 0".

Also I think just checking for if string equals "0" could be enough, no need to check for each decimal. ^^
JASS:
function TributeValue takes string s returns boolean
    local real r = S2R(s)
    // Test for bad input
    if s != "0" then
        if (r > 255 or r < 1)
            false
        endif
    endif
    return true
endfunction
JASS:
if TributeValue(SubString(GetEventPlayerChatString(x, y))) then
    // your actiones
else
    // false returned, invalid substring
endif
If you typecast a real to an integer it gets rounded. But everything after the comma gets lost.
 
JASS:
function TributeValue takes string s returns boolean
    local real r = S2R(s)
    // Test for bad input
    if s != "0" then
        if (r > 255 or r < 1)
            false
        endif
    endif
    return true
endfunction
Can even be a bit better:
JASS:
function TributeValue takes string s returns boolean
    local real r = S2R(s)
    return (s == "0" or (r < 256 or r > 0))
endfunction
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
JASS:
function TributeValue takes string s returns boolean
    local real r = S2R(s)
    // Test for bad input
    if s != "0" then
        if (r > 255 or r < 1)
            false
        endif
    endif
    return true
endfunction
Can even be a bit better:
JASS:
function TributeValue takes string s returns boolean
    local real r = S2R(s)
    return (s == "0" or (r < 256 or r > 0))
endfunction

oh, why u keep using < 256? or > 0?

I made it like this now:

JASS:
function TributeValue takes string s returns real
    local real r = S2R(s)     
    local string s1 = SubStringBJ(R2S(r),1,4)
    set r = S2R(s1) 

    if (r == 0 and  SubStringBJ(s1, 1, 1) != "0" ) then
        set r = -1
    elseif (r > udg_DIPS_MaxTribute) then // r > 3.00
        set r = udg_DIPS_MaxTribute
    endif
    return r
endfunction

if you wonder why i convert it to a real, to a string back to a real again it was to chop of

any numbers above 3 decimals. But perhaps it would be better to convert -tribute red 0,006 to r = 0,01 instead of r = 0,00 (Just to know how to do
it.)
 
Last edited:
Status
Not open for further replies.
Top