Making money stats more readable

Hello all!

In my current WIP, there are a few different stats regarding money. Right now, there’s a stat that tracks your personal wealth as well as the amount of money located specifically in your campaign account. Both of these numbers can reach into the millions (or billions), and I was wondering if there was any way to auto-create commas in the numbers.

I came across this thread from 2018 that appears to do what I want but I’m not entirely sure how to implement it (where to put the code or alter it to work for all my money variables), or if that code from 2018 still works four years later.

I’d appreciate any help or suggestions, thanks!

1 Like

If you want to show the numbers on the stat screen you can make a subroutine with that code you found on a separate file, call it at the beginning of the choicescript_stats.txt, so you have the output set and updated to a variable every time you enter the stat screen, and then you can show the variable with this ${}. Assuming that the code works, which I think it should.

1 Like

I’m a bit unsure of how to actually put that code into my stats page so that it will work on that page and when those numbers are used in the story. Will any of it need to be edited to work for the game to function properly? I have two variables, “Wealth” and “PACMoney”, would any of the code need to be changed to update those numbers?

Appreciate the help!

1 Like

Create Wealth_comma "" and PAC_comma "" on your startup.txt file

choicescript_stats.txt

*gosub_scene money_calc

${Wealth_comma} Wealth
*line_break
${PAC_comma} PAC Money

money_calc.txt

*temp one "12345678901234"
*temp two ","
*temp counter 1
*temp counter2 0
*temp counter3 1
*temp counter4 0
*temp results 0
*temp results2 0
*temp results3 0
*temp num ""
*temp Wealth_comma ""
*set one Wealth

*if length(one) < 4
 *set Wealth_comma one
 *goto final

*set results3 length(one)/3
*if length(results3) = 1
 *set results3 -1
*if results3 < 1
 *set results3 0
*if length(results3) > 1
 *set results3 (results3#1)

*label next
*set counter3 length(one)
*set counter2 length(one)
*set counter4 length(one)

*label resultsloop
*set results (counter4/3)
*if length(results) > 1
 *set counter4 +1
 *set results2 +1
 *goto resultsloop

*label main

*if length(num) < counter3
 *set num &(one#counter2)
 *set counter2 -1
 *set counter +1
 *goto main
*if length(num) = counter3
 *set counter 1
 *set counter +results2
 *set counter2 length(one)
 *goto main2

*label main2

*if length(Wealth_comma) < (counter3 +results3)
 *if counter < 4
  *set Wealth_comma &(num#counter2)
  *set counter +1
  *set counter2 -1
  *goto main2
 *if counter = 4
  *set Wealth_comma &two
  *set counter 1
  *goto main2


*label PAC
*temp one1 "12345678901234"
*temp two1 ","
*temp counter1 1
*temp counter21 0
*temp counter31 1
*temp counter41 0
*temp results1 0
*temp results21 0
*temp results31 0
*temp num1 ""
*temp PAC_comma ""

*set one1 PAC_comma

*if length(one1) < 4
 *set PAC_comma one1
 *goto final

*set results3 length(one1)/3
*if length(results31) = 1
 *set results31 -1
*if results31 < 1
 *set results31 0
*if length(results31) > 1
 *set results31 (results31#1)

*label next1
*set counter31 length(one1)
*set counter21 length(one1)
*set counter41 length(one1)

*label resultsloop1
*set results1 (counter41/3)
*if length(results1) > 1
 *set counter41 +1
 *set results21 +1
 *goto resultsloop1

*label main1

*if length(num1) < counter31
 *set num1 &(one1#counter21)
 *set counter21 -1
 *set counter1 +1
 *goto main1
*if length(num1) = counter31
 *set counter1 1
 *set counter1 +results21
 *set counter21 length(one1)
 *goto main21

*label main21

*if length(PAC_comma) < (counter31 +results31)
 *if counter1 < 4
  *set PAC_comma &(num1#counter21)
  *set counter1 +1
  *set counter21 -1
  *goto main21
 *if counter1 = 4
  *set PAC_comma &two1
  *set counter1 1
  *goto main21

*label final1
*return


Let me know if it works, I didn’t tested it.

Also whenever you want to show the Wealth and PAC outside the stat screen you should also call the money_calc first so the values get updated.

*gosub_scene money_calc
You have ${Wealth_comma} Wealth
*line_break
You have ${PAC_comma} Money
1 Like

When playtesting I get the bug “There is no character at position 0; the position must be greater than or equal to 1.”

*label main2

*if length(Wealth_comma) < (counter3 +results3)
*if counter < 4
     *set Wealth_comma &(num#counter2)
     *set counter2 -1
     *goto main2
*if counter = 4
     *set Wealth_comma &two
     *set counter 1
     *goto main2

The message flags line 55 which is: “*set Wealth_comma &(num#counter2)”

Look the original code, you missed the indentation and one entire line of the code.

2 Likes

Just implemented this – it looks like while that issue has been fixed, it does not display the value.

When going to the stats page, it just shows

Wealth: $

Without the actual value.

This is the coding of my stats page if that helps:

[b]The President[/b]

[b]Name:[/b] ${FullName}
*line_break
[b]Gender:[/b] ${GenderName}
*line_break
[b]Home Region:[/b] ${HomeRegion}
*line_break
[b]Home State:[/b] ${State}
*line_break
[b]Party:[/b] ${Party} Party
*line_break
*gosub_scene money_calc
[b]Wealth:[/b] $${Wealth_comma}
*line_break
[b]Spouse:[/b] ${Spouse}

Try changing the *goto final for a *goto PAC I’ve missed that one. Though I’m not sure if that’ll solve the issue.

1 Like

Just tried that, doesn’t seem to fix the bug. I’m going to keep fiddling with it to see if I can figure it out. If you have the time I could also DM you the text files so you could try reproducing it.

If not, I completely understand and appreciate all your help up to here. Thanks!

I just saw it. Your line that’s supposed to show Wealth has 2 $$, it needs to be ${Wealth_comma} not $${Wealth_comma}

1 Like

Also just tried that and didn’t seem to fix it. Wealth there has two $ signs because I wanted it to display the symbol infront of the value so the stat would display as “Wealth: $4,000,000” as opposed to “Wealth: 4,000,000”. Changing it doesn’t appear to impact whether or not the stat displays.

Oh, okay, I thought that it could mess up the command. I’m gonna keep looking then.

1 Like

I think I figured it out!

The issue appeared to be in the line:

*temp Wealth_comma ""

Since I already created the permanent Wealth_comma variable in startup, I think creating a new temporary variable with the same name is what was breaking the code.

With that being said, while commas do appear, the values also appear to be acting strange. Sometimes it will display a “0” ahead of the value, and changing the Wealth stat value doesn’t appear to work. (A choice that should result in +$100,000 doesn’t appear to change the stat value at all).

I’m not sure if this is an issue just with the code itself but hopefully there’s a fix for it!

Ups, sorry, my bad. I’m not in my best coding day.

As I said before, if you change the value you have to run the *gosub_scene money_calc to update the value of the variable Wealth_comma.

Wrong

*choice
  #Give me more money
    *set Wealth +20000
    You have ${Wealth_comma}

Right

*choice
  #Give me more money
    *set Wealth +20000
    *gosub_scene money_calc
    You have ${Wealth_comma}
1 Like

I don’t think I’ve had a good coding day just yet so you’re doing far better than I am haha.

Is there any reason you can think of as to why this wouldn’t be uploading on the stats screen? Wouldn’t the code run every time you click on the stats page to update the value?

In the stats screen yes, it should run the code every time you enter. But if you want to show the value outside of it you need to run it before you actually show it with ${} so it updates the value to the newest one.

Other than that I don’t really know, I’m just glancing at the code and not testing it on my own.

1 Like

Hmm, not sure why I’m getting weird values then. It almost seems like something in the code is changing the original ‘Wealth’ stat as that would explain it.

If the ‘Wealth’ stat was changed then future changes to the value would no longer work, I wonder if that might be the issue?

The code only copies the value of Wealth to the variable one. there’s no change made the the variable Wealth.
It’s strange but I haven’t made the code nor tested it, so… I don’t see anything wrong at plain sight, yet.

1 Like

Jumping in late, but I saw you were trying to solve a problem I had seen before. Not that it hugely matters, but the solutions in that thread are a little over engineered. I have written a simpler process for insering commas into a money stat.

Your startup file would have these (or your named versions of them)

*create money 0
*create display_money ""

Then your scene file, which you call with a gosub_scene has this

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

*set display_money ""
*set length length(money)
*set current_char length

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

When I wrote it, it was a self contained piece of code and wasn’t designed to be called repeatedly by a gosub_scene. I have changed it a bit for that, but haven’t tested it.

3 Likes

Just tried this and it doesn’t seem to work with being called through gosub.

This is the code I’m currently working with in money_calc:

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

*set Wealth_comma ""
*set length length(Wealth)
*set current_char length

*label start_loop
*if current_char > 0
    *set comma_check (char_count - 1) modulo 3
    *if ((comma_check = 0) and (current_char != length))
        *set Wealth_comma "," & Wealth_comma     
    *set Wealth_comma (Wealth#{current_char}) & Wealth_comma
    *set current_char - 1     
    *set char_count + 1
    *goto start_loop