Consider this...
I'm asking my player to type in an angle for a cannon to shoot.
The angle can be a float value, i.e. 45.3 or 15.0, like that.
GetTextInput() only returns a string.
Val() only returns an integer.
How do I get the float value my player typed in?
Here's what I've come up with.
a$ = "25.349"
` search for the first dot in the string
for c = 1 to len(a$)
c$ = mid(a$, c, 1)
if c$ = "." and foundDot = 0
foundDot = 1
dotPosition = c
endif
next c
if foundDot = 0
value# = val(a$) * 1.0
fractionalPart$ = "0"
else
` find the two parts of the value
integerPart$ = left(a$, dotPosition)
fractionalPart$ = right(a$, len(a$) - 1 - dotPosition)
` calculate the divisor needed to move the decimal
divisor = (10 ^ (len(fractionalPart$)))
` calculate both values
integerValue# = (val(integerPart$) * 1.0)
decimalValue# = (val(fractionalPart$) / (divisor * 1.0))
` combine them
value# = integerValue# + decimalValue#
endif
do
print("testing string " + a$)
printc("float value is ")
print(str(value#, len(fractionalPart$)))
Sync()
loop
If anyone can suggest a quicker or easier way to input a float, I would sure appreciate the help!
EDIT: Oops! You may assume that I've modified the above code to account for no decimal point in the player input.
MORE EDIT: Okay, I stripped it down and added a check for no decimal point in the input text. But it still seems like an awful lot of work just to get a float...
and this fixes the change in right() made in the last update to AGK...
a$ = "25.349"
` search for the first dot in the string
for c = 1 to len(a$)
c$ = mid(a$, c, 1)
if c$ = "." and foundDot = 0
foundDot = 1
dotPosition = c
endif
next c
if foundDot = 0
value# = val(a$) * 1.0
fractionalPart$ = "0"
else
` find the two parts of the value
integerPart$ = left(a$, dotPosition)
fractionalPart$ = right(a$, len(a$) - dotPosition)
` calculate the divisor needed to move the decimal
divisor = (10 ^ (len(fractionalPart$)))
` calculate both values
integerValue# = (val(integerPart$) * 1.0)
decimalValue# = (val(fractionalPart$) / (divisor * 1.0))
` combine them
value# = integerValue# + decimalValue#
endif
do
print("testing string " + a$)
printc("float value is ")
print(str(value#, len(fractionalPart$)))
Sync()
loop