Type coercion/parsing/polymorphism in *params?

I’m having trouble with the *params function. Specifically, the interaction with the “set” function. Here’s the code I have:

*comment set stats and optionally show the adjustment
*label set_power
*params power_amount
*set stat_power power_amount
*if show_stats_increase
    [i]Your power is now ${stat_power}.[/i]
*return

This only works for the case where you are setting a number to a specific value. So, if I call

*gosub_scene utils set_power 10

then it will set the ‘power’ stat to 10.

However, none of these work:

*gosub_scene utils set_power +5
*gosub_scene utils set_power -5
*gosub_scene utils set_power +%5
*gosub_scene utils set_power -%5

In each case, it says “Invalid expression at char 1, expected NUMBER, STRING, VAR or PARENTHETICAL, was: OPERATOR [+]”

These will not give the desired result:

*gosub_scene utils set_power "+10"
*gosub_scene utils set_power "+%10"

In this case, the stat_power will become a string of “+10” or “+%10”, instead of being modified.

Is this a limitation of the choicescript engine that can be worked around?

1 Like

You can have a “polymorphism” with multiple parameters, or parse your one parameter instead of using its value directly in the set command.

1 Like

I tried a few wrong ways and started sour-grapes-ing to “Save your sanity, just always use fairmath”

1 Like

Here’s my quick attempt, there might be some bugs.

*comment with polymorphism
*comment *gosub set_power 10
*comment *gosub set_power "-" 10
*comment *gosub set_power "%" "+" 10
*comment *gosub set_power false "-" 10
*label set_power
*params
*temp sign "+"
*temp fair false
*temp value ""
*if (param_count = 0)
    *bug expected at least 1 parameter, got 0
*if (param_count = 1)
    *set value param[1]
*if (param_count = 2)
    *set sign  param[1]
    *set value param[2]
*if (param_count >= 3)
    *set fair  "@{((param[1] = \"%\") or (param[1] = true)) true|false}"
    *set sign  param[2]
    *set value param[3]
*if (sign = "+")
    *if (fair)
        *set stat_power %+value
    *else
        *set stat_power +value
*else
    *if (fair)
        *set stat_power %-value
    *else
        *set stat_power -value
*return


*comment with parsing
*comment *gosub set_power_2 10
*comment *gosub set_power_2 "-10"
*comment *gosub set_power_2 "%10"
*comment *gosub set_power_2 "%-10"
*comment *gosub set_power_2 "10%"
*comment *gosub set_power_2 "10%-"
*comment *gosub set_power_2 "1-%0" = "%-10"
*label set_power_2
*params arg
*temp sign "+"
*temp fair false
*temp value ""
*temp i_ 1
*label set_power_loop
*comment begin loop
*temp char (arg#i_)
*if (char = "+")
    *set sign "+"
*elseif (char = "-")
    *set sign "-"
*elseif (char = "%")
    *set fair true
*else
    *set value &char
*set i_ +1
*if (i_ <= length(arg))
    *goto set_power_loop
*comment end loop
*if (sign = "+")
    *if (fair)
        *set stat_power %+value
    *else
        *set stat_power +value
*else
    *if (fair)
        *set stat_power %-value
    *else
        *set stat_power -value
*return
2 Likes

Brilliant! Ok, I get it. basically accept a text string, and then parse the text string myself