[Solved] How to add comma in money texts like 1,000

Anyone one know how to make money texts like 5,000 and 20,000 basically add komma like in real life you know if its over 1,000 and so. If the player makes over 150 mill i wanna make like 150,000,000

These events:

got this output:

1 Like

This is confusing. might you make easier to understand like what is declare text as this… what is commapos?

Commapos is declared at the top with the starting value 3.

The actions are just popping in commas every 3 spaces. SubStr() takes a piece of a string. StrLength(scoreText) is the length of the string, in this case 18. So this:

SubStr(scoreText, 0, StrLength(scoreText) - commaPos)
is the same as this:
SubStr(scoreText, 0, 18 - 3) or SubStr(scoreText, 0, 15)

In other words, grab the characters 0-15 of scoreText. The other part grabs the end:

SubStr(scoreText, StrLength(scoreText) - commaPos, StrLength(scoreText))
or
SubStr(scoreText, 15, 18)

Finally, the strings are concatenated with a β€œ,” in between.

SubStr(scoreText, 0, 15) + β€œ,” + SubStr(scoreText, 15, 18)
or
β€œ150222333444555” + β€œ,” + β€œ666”

Then add 4 to the commaPos and repeat again (you have to add 4 because you just added a comma to the string). So the next step will be:

SubStr(scoreText, 0, 12) + β€œ,” + SubStr(scoreText, 12, 19)

Here’s what all the steps look like:

β€œ150222333444555” + β€œ,” + β€œ666”
β€œ150222333444” + β€œ,” + β€œ555,666”
β€œ150222333” + β€œ,” + β€œ444,555,666”
β€œ150222” + β€œ,” + β€œ333,444,555,666”
β€œ150” + β€œ,” + β€œ222,333,444,555,666”

1 Like

I’m not finna pretend i understand or even know how to do this. This is one feature i’ll leave alone its pain and headache. I managed to implement vertical scroll in gdev with chatgpt help and this is still mad confusing thing i ever seen

I really like MrMen’s solution. I think the part confusing you is that he used local variables, so you are not sure about what β€œDeclare” this or that means. I reworked the example he was kind enough to share using only Scene and Object variables so you can see how to apply his solution without worrying yourself over local variables for the moment.

What is happening is that you need to keep track of a number, which is the players money, and you also need to display that number as a text, so that your player can see how much money they have, and with commas so that it’s easily readable to the player.

So here are 2 scene variables, one is called Money and it keeps track of all the player money, as a number and not a text. The other one is called commaPos and it will keep track of where to place commas in the text representation of the amount of money the player has, which you will display to the player in a text object.

Here is a text object called MoneyDisplay that will be used to show the player how much money they have. This text object has 2 variables. One is called nbrMoney and it is used to compare what the text box is displaying to the actual amount of money held in the scene variable Money. If this number is different from the scene variable, then the text box needs to be updated to the new amount of money the player has, and then this nbrMoney variable needs to be updated to match the scene variable Money so the text box will know it matches again. The other object variable is called txtMoney and this is going to be the text representation of the value of money the player has, complete with commas added to it for easy reading for the player. This variable is what the MoneyDisplay text object will be displaying.

For the events, the player increases the scene variable Money by releasing the β€˜j’ key. The other separate event checks to see if the scene variable Money is equal to the object MoneyDisplay variable nbrMoney - if it’s not equal, it sets the commaPos variable back to the starting spot (3) and it changes the object MoneyDisplay variable txtMoney which is a text variable, and it puts the text representation of the current number of scene variable Money in it, then it loops through and adds commas into it where they should be. Finally when it is done placing commas correctly, it displays itself as the text representation of the money the player has in the object MoneyDisplay, then updates the object variable nbrMoney to match the scene variable Money so that it knows its job is done and it will not have to do it again until the player value of money does not match what is displayed in the object MoneyDisplay.

Anyway I don’t think you have to use all those scene and object variables, I was just trying to break it down to show what was going on since it seemed like it was the local variables that were throwing you off. Also the math might be throwing you off, but you don’t really need to understand that part unless you are wanting to rework it to display commas in different positions. MrMen has done the math for you and made the nice formula, so you can just copy it and replace the variable with your variable that is holding the money/score/whatever.

Here is the text I entered into my action to change the text variable txtMoney of MoneyDisplay to add commas to it and get it ready for the action to display it in the text object. It might make it easier if you copy this and place it in your action, and substitute the variable name MoneyDisplay.txtMoney with your own variable.

SubStr(MoneyDisplay.txtMoney, 0, StrLength(MoneyDisplay.txtMoney) - commaPos) + "," + SubStr(MoneyDisplay.txtMoney, StrLength(MoneyDisplay.txtMoney) - commaPos, StrLength(MoneyDisplay.txtMoney))
2 Likes

I just wanna say thank you :face_holding_back_tears: :face_holding_back_tears:


I used global variable cause i found it easier cuz i got lots of stuff there and heres the code

Thank you soo much!!!

2 Likes

Apologies for omitting an explanation part to the solution. I was in a rush and only had about 15 mins when I responded to the initial question.

Thank you @magicsofa & @Lucky-j for explaining it in a way that OP understood.

1 Like

There’s no problem thanks to @Lucky-j helped me and you

I was so impressed with your solution I felt it important to try breaking down. I feel your solution can be valuable to other people that come here with the same question.

2 Likes

Indeed, it’s always things you think are simple are the hardest to implement

1 Like