Adding commas to values related to money stats

Hello, I’m currently trying to write code so that it will auto-create commas for different stats regarding money. I am not a coder and I am currently learning while doing so however I keep coming across a problem with the current code I have written which I found help on in a different topic tread. I put the code for commas into a different file currently as money_calculations.txt:

*temp current_char 0
*temp char_count 1
*temp length 0
*temp comma_check 0

*set display_wealth ""
*set length length(wealth)
*set current_char length



*label start_wealth_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_wealth "," & display_wealth          
    *set display_wealth (wealth#{current_char}) & display_wealth
    *set current_char - 1     
    *set char_count + 1
    *goto start_wealth_loop
*return

However while this does work, specify for wealth, when I try to do it with a different variable for income which here is the code for it:

*temp current_char 0
*temp char_count 1
*temp length 0
*temp comma_check 0

*set display_wealth ""
*set length length(wealth)
*set current_char length



*label start_wealth_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_wealth "," & display_wealth          
    *set display_wealth (wealth#{current_char}) & display_wealth
    *set current_char - 1     
    *set char_count + 1
    *goto start_wealth_loop
*return


*set display_income ""
*set length length(income)
*set current_char length

*label start_income_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_income "," & display_income          
    *set display_income (income#{current_char}) & display_income
    *set current_char - 1     
    *set char_count + 1
    *goto start_income_loop
*return

It doesn’t display the income value correctly when going to the stats page, it just shows as:

James Lee Smith
Age: 40

Wealth: $2,000

Income: $

Here is the current code in my choicescript_stats.txt file if it provides any help:

[b]$!{firstname} $!{middlename} $!{lastname}[/b]
*line_break
[b]Age:[/b] ${age}
*line_break
*gosub_scene money_calculations
[b]Wealth:[/b] $${display_wealth}
*line_break
*gosub_scene money_calculations
[b]Income:[/b] $${display_income}

Hi!

So your issue here is the *return statement in your money_calculations scene. Basically whenever it hits that, it’s always going straight back to the stats screen. You’re calling it twice, but it only runs the wealth loop and returns, it can’t ever reach the income loop.

I’ve thrown together a quick alternative that fixes it and pasted it below with a couple of comments in the code to show what it’s doing. There’s potentially neater ways of coding it that other people might come up with later on, but this should get you going. :slight_smile:

money_calculations:

*temp current_char 0
*temp char_count 1
*temp length 0
*temp comma_check 0

*set display_wealth ""
*set length length(wealth)
*set current_char length



*label start_wealth_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_wealth "," & display_wealth          
    *set display_wealth (wealth#{current_char}) & display_wealth
    *set current_char - 1     
    *set char_count + 1
    *goto start_wealth_loop
*else
  *comment Escape the wealth loop
  *goto end_wealth_loop
*label end_wealth_loop

*comment reset everything to be safe
*set current_char 0
*set char_count 1
*set length 0
*set comma_check 0

*comment carry on to calculate income

*set display_income ""
*set length length(income)
*set current_char length

*label start_income_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set display_income "," & display_income          
    *set display_income (income#{current_char}) & display_income
    *set current_char - 1     
    *set char_count + 1
    *goto start_income_loop
*else
  *comment escape the income loop
  *goto end_income_loop

*label end_income_loop
*comment return to the stats screen
*return

choicescript_stats

*gosub_scene money_calculations
[b]Wealth:[/b] $${display_wealth}
*line_break
[b]Income:[/b] $${display_income}