|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: Sun Feb 17, 2008 6:05 pm
[2.18] Possible type issue with %if |
I'm back to trying to fix up my combat system, and I'm running into a few little things here and there as I go. This time it's got to do with %if, I think. What I expect to be set in my variable as a number comes out as a string, including the leading space before the number.
Code: |
$rightleg = %if(@ruptures.rightleg and !@ninshi.rightleg, @ruptures.rightleg, 0) |
Using this variable to concatenate strings with the stored value, I get the extra space.
Code: |
#SHOW "rupture_legs_"$rightleg |
gives me "rupture_legs_ 1" instead of "rupture_legs_1"... |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Mon Feb 18, 2008 9:37 am |
You are right. Looking at the documentation for %if it hints that it defaults to strings, not the lack of spaces between the true, false options.
One workaround (which I'm sure you've found already) is to specify it is an int like so.
Code: |
$rightleg = %if(@ruptures.rightleg and !@ninshi.rightleg, @ruptures.rightleg, %int(0)) |
|
|
_________________ Asati di tempari! |
|
|
|
Malach Apprentice
Joined: 03 Nov 2007 Posts: 132
|
Posted: Tue Feb 19, 2008 3:49 pm |
It has always been my understanding that %if defaults to strings. I'm pretty sure that's not a bug. This may be an obvious question but is there a reason you can't just take out the spaces before the @ruptures.rightleg or the 0? The code works fine without the extra spaces. If you really need it to be a number you can always do:
Code: |
$rightleg = %int(%if(@ruptures.rightleg and !@ninshi.rightleg, @ruptures.rightleg, 0)) |
That gets it to concat it properly in the show you used. If the number part doesn't matter as much as the spaces part what about trim?
Code: |
$rightleg = %trim(%if(@ruptures.rightleg and !@ninshi.rightleg, @ruptures.rightleg, 0)) |
This also gets the result you're looking for. |
|
_________________ Intel Core2 Quad CPU @ 2.4 GHZ with Windows Vista Home Premium and 2 GB Ram |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: Wed Feb 20, 2008 2:18 pm |
I already found a suitable workaround for my problem, and I didn't even use %if to do it. Given that many other commands and functions seem to do autotyping just fine, however, I figure that %if should be able to distinguish a number from a string. At the very least, it shouldn't include the space between the comma and the value as part of the string.
Thanks for the suggestions, but I already knew about %int and %trim. Just reporting a possible issue that needed some improvement. |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: Wed Mar 19, 2008 2:32 pm |
Just a quick bump of this post to ask about the possibility of %if ever returning a typed result. I just found another spot in my code where numbers came back as strings, and subsequently my math evaluation on them failed. I noticed this time that when I removed all spaces that the syntax coloring changed to indicate the type change, so at least that's helpful to me now.
Here's the snippet that was giving me troubles:
Code: |
$wounds_healed = %if(@flags.medbag_rub, 900, 800)
$wounds_present = @wound_count($body_part)
$wounds_left = $wounds_present - $wounds_healed
|
Removing the spaces before 900 and 800 fixed it for me. I know I also could've fixed it by changing the last line to evaluate each variable in the expression with %int and then doing the subtraction.
Is the behavior of subtracting a string from an integer even defined? |
|
|
|
|
|