• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows [SOLVED] I have a issue

W

Whiskey01

Guest
Hi guys ive come across a bit of a problem and i need a bit of help.....
im making a game where you can have a lot of money and i dont want to make the user have to read like 12 digits of numbers just to see how much they have. so i would like to create a piece of code such as this pseudo code bellow.....if you can help me please do.

if money >= 10000
money = money / 1000 + "k"
k = money * 1000


but i still want the money to be as much as the user has i just dont want to show the entire thing for example.
if ihs money is 12567 i would like it to print as 12.5k although he still has 12567. please help im really stuck and have no idea how to solve this.
 
How about converting the money to a string as soon as it's over 1000.
The you can read the first characters of the string and add "k". I'm a noob myself, but that's how I'd do it.

If global.money >= 1000
{
Money_String = string(global.money);
New_String = (first characters of the Money_String) + "k";
}

Then you could draw the money like: draw_text(x, y, New_String);

Actually I'm not sure how the string read character function is called right now, cause I'm on phone. But you'll have to check, if the money is:

If global.money >= 1000 & & <10000
{ //read the first character only}
else if global.money >= 10000 & & global.money < 100000
{
//read the first two characters only
}

And so on.
Hope I gave you an idea.


EDIT:
maybe you don't even need two strings. Maybe one is enough.
 
L

Lars Karlsson

Guest
Not super beautiful but should be enough. It also doesnt handle values at and over billion.
Code:
[Script]
///money_format(int money)
var money = argument0;
var divided_money;
var result;

if (money < 1000)
{
    result = string(money);
}
else if (money >= 1000 && money < 1000000)
{
    divided_money  = money / 1000;
    result = string_format(divided_money, string_length(string(divided_money)), 1) + " K";
}
else if(money >= 1000000)
{
    divided_money  = money / 1000000;
    result = string_format(divided_money, string_length(string(divided_money)), 1) + " M";
}

return result;
 
W

Whiskey01

Guest
i have now come up with this code using your guys help but its saying that Money_string is not being set before it is read.... even though i am defining it before hand.
this is a step event by the way


upload_2016-12-6_22-57-6.png
 
R

Rocket35

Guest
i have now come up with this code using your guys help but its saying that Money_string is not being set before it is read.... even though i am defining it before hand.
this is a step event by the way


Have you created the created the variable in the create or the begin step event?
 

Hyomoto

Member
If that code is all that exists, it shouldn't be an issue. What line is it throwing an error when it says it doesn't exist? I'm guessing your problem is probably when you draw it, not when you are setting it. Money string is only being created if your money is more than 10000. As @Rocket35 implied, you need to create that variable before you use it anywhere. Generally, it's best to define all your variables in the creation event of the objects that will use them, or during some initialization script that runs when the game starts. The way you are doing it now is a pretty bad habit and it would be wise to break it early.
 
W

Whiskey01

Guest
If that code is all that exists, it shouldn't be an issue. What line is it throwing an error when it says it doesn't exist? I'm guessing your problem is probably when you draw it, not when you are setting it. Money string is only being created if your money is more than 10000. As @Rocket35 implied, you need to create that variable before you use it anywhere. Generally, it's best to define all your variables in the creation event of the objects that will use them, or during some initialization script that runs when the game starts. The way you are doing it now is a pretty bad habit and it would be wise to break it early.

the error is on line six and its saying that Money_string has not been set before reading it.
 

Hyomoto

Member
If that doesn't fix it, you need to post the entire error from the console. I'm not saying you are describing it wrong, but it's impossible for us to make accurate suggestions if we're assuming the problem based on your interpretation of it.

Generally that's what you should do anyways, it just cuts down on the human error factor and ensures we're all most closely on the same page.
 

Hyomoto

Member
Now that I look again, I think it see it. you have no {}'s . that means the if statement is only run for the first line where you set the variable, the other lines are always run. so when your points are less than, they are trying to access a missing variable. tabs have no impact on code blocks, if you don't use brackets to define your blocks, an if statement only applies until the next operation. You should always use brackets and semicolons in GML, it's a poor habit to not use them and will increase easily preventable errors like this.

Doing what @LucasTheNewbie suggested should fix it, but you'll probably still have problems if you code like this.
 
Last edited:
W

Whiskey01

Guest
Now that I look again, I think it see it. you have no {}'s . that means the if statement is only run for the first line where you set the variable, the other lines are always run. so when your points are less than, they are trying to access a missing variable. tabs have no impact on code blocks, if you don't use brackets to define your blocks, an if statement only applies until the next operation. You should always use brackets and semicolons in GML, it's a poor habit to not use them and will increase easily preventable errors like this.

Doing what @LucasTheNewbie suggested should fix it, but you'll probably still have problems if you code like this.

thank you for the tip i will try and get into the habbit of doing it, i have also solved the problem i used a different solution that seems to work, thanks everyone.
 
M

Millionsknivez

Guest
how do you get it to read this script though, where do you place the script execute code?
I have global.cash
then in my 2nd room I draw it...I cant get it to refer to the script, or maybe I don't know how to change your code to fit my game....please help?
 
Top