Show a increase numbers int till the new value

Started by WiseFrog, Tue 04/05/2021 04:31:00

Previous topic - Next topic

WiseFrog

Hello everybody. Today I was implementing a money system in my game. The int can already be seen in its respective Label GUI and I can add and subtract money from it, but I would like to know if there is a way when I add or remove money the numbers visually decrease or increase until the new value as many games do and how could it be done, since it would give more dynamism than just the new value suddenly appear.

I've been searching the forum but didn't find anything about it.

Thanks in advance

Gilbert

Use another variable to be displayed in the label.
This is just an rough idea and could be silly looking but could be a start so that more elegant solution can be implemented in the future:

Say, you use the variable realmonie for the actual amount of money the player is holding, and dispmonie another variable for use in the label. Initially set dispmonie to the same value as realmonie.
When you need to give money to the player/use money just update realmonie directly.
In repeatedly_execute_always() of the global script add the following lines:
Code: ags

if (dispmonie < realmonie) dispmonie++;
else if (dispmonie > realmonie) dispmonie--;

WiseFrog

#2
Hey Gilbert thanks for the reply. I tried to do what you said but nothing happens, when I add money to the realmonie variable, dispmonie not only doesn't have the effect of increasing but it doesn't change the number either, I don't know why  :(

EDIT: I tried to put the script that you gave me in the repeatedly_execute only and it worked, I don't know why it didn't work in the always rep.
Anyway the effect works good but I notice that it takes too long to reach the new number. Is there a way to do it that only took a couple of seconds?

Snarky

You also need to actually update the label. Something like this (assuming the label is called lblMoney):

Code: ags

int dispMoney, realMoney;

function repeatedly_execute_always()
{
  // ... (anything else you have in this function)

  if (dispMoney < realMoney)
    dispMoney++;
  else if (dispMoney > realMoney)
    dispMoney--;
  lblMoney.Text = String.Format("%d", dispMoney);
}

WiseFrog

#4
Quote from: Snarky on Tue 04/05/2021 07:30:17
You also need to actually update the label. Something like this (assuming the label is called lblMoney):

Code: ags

int dispMoney, realMoney;

function repeatedly_execute_always()
{
  // ... (anything else you have in this function)

  if (dispMoney < realMoney)
    dispMoney++;
  else if (dispMoney > realMoney)
    dispMoney--;
  lblMoney.Text = String.Format("%d", dispMoney);
}


Hey snarky, I had already update the label. I found what the error was. I put the function to show the money on the label on repeatedly_execute and not on repeatedly_execute_always. Thank you very much

Now I just have to find how to do it not take so long to get to the new number and only take about 3 or 4 seconds for example

Khris

Please don't quote the entire previous post. There's a Reply button at the bottom.

Your current code increases the variable by 40 per second. If you want a faster increase, just do  dispMoney += 5;  instead, however in that case you need an additional check so you don't end up with a higher value than realMoney.

Gilbert

Quote from: WiseFrog on Tue 04/05/2021 07:40:34
Now I just have to find how to do it not take so long to get to the new number and only take about 3 or 4 seconds for example

As KhrisMUC pointed out you can actually add or subtract more than 1 in each game loop, but you'll need to do additional checks (for example, if you add/subtract 5 each time and don't check the result, then say if at some instance realMoney = 8 and dispMoney = 7 then dispMoney will annonyingly fructuate between 7 and 12 indefinitely). The check would be easy. I just put out the simplest possible codes as a start:
Code: ags

int dispMoney, realMoney;

function repeatedly_execute_always()
{
  // ... (anything else you have in this function)

  if (dispMoney < realMoney)
  {
    dispMoney+=5;
    if (dispMoney > realMoney) dispMoney = realMoney;
  }
  else if (dispMoney > realMoney)
  {
    dispMoney-=5;
    if (dispMoney < realMoney) dispMoney = realMoney;
  }  
  lblMoney.Text = String.Format("%d", dispMoney);
}


Snarky

Quote from: WiseFrog on Tue 04/05/2021 07:40:34
Hey snarky, I had already update the label. I found what the error was. I put the function to show the money on the label on repeatedly_execute and not on repeatedly_execute_always. Thank you very much

This is not the cause of the error, as it should work equally well (if not better) in repeatedly_execute_always(). You must have made some mistake in the implementation that you later fixed.

Matti

Isn't that what WiseFrog meant? That it worked in repeatedly_execute_always?

Crimson Wizard

Quote from: Matti on Tue 04/05/2021 14:44:57
Isn't that what WiseFrog meant? That it worked in repeatedly_execute_always?

I suppose what could have happened is that the label did not update during blocking actions, which looked like money counter got stuck.

Khris

#10
WiseFrog said he moved it from _always into the regular one and it started working. Like Snarky correctly mentions, that sounds weird and was probably not what fixed it.

Edit:
Quote from: WiseFrog on Tue 04/05/2021 07:40:34I put the function to show the money on the label on repeatedly_execute and not on repeatedly_execute_always. Thank you very much

It can be read both ways, so I guess he did move it to _always...  :P

Matti

I'm pretty sure that was the meaning. I guess you have to imagine a colon after the first sentence.

Quote
I found what the error was: I put the function to show the money on the label on repeatedly_execute and not on repeatedly_execute_always.

I guess the problem was exactly like CW described.

Snarky

#12
But there's no ambiguity in the earlier post about the problemâ€"it's clear that WiseFrog "solved" the problem by moving the code from repeatedly_execute_always() to repeatedly_execute(), not the other way around:

Quote from: WiseFrog on Tue 04/05/2021 07:13:37
Hey Gilbert thanks for the reply. I tried to do what you said but nothing happens, when I add money to the realmonie variable, dispmonie not only doesn't have the effect of increasing but it doesn't change the number either, I don't know why  :(

EDIT: I tried to put the script that you gave me in the repeatedly_execute only and it worked, I don't know why it didn't work in the always rep.

So it didn't work at first, when WiseFrog tried to do it the way Gilbert instructed using repeatedly_execute_always(). Then they moved it to repeatedly_execute() ("only," as in without the "always") and it worked.

(But as I said earlier, this change in itself can hardly be the reason why it started working; there must have been some other error.)

WiseFrog

Quote from: Khris on Tue 04/05/2021 08:22:21
Please don't quote the entire previous post. There's a Reply button at the bottom.

Sorry I didn't mean to it, I'm new in the forums and didn't know very well how it was handled.

Quote from: Gilbert on Tue 04/05/2021 10:41:26

As KhrisMUC pointed out you can actually add or subtract more than 1 in each game loop, but you'll need to do additional checks (for example, if you add/subtract 5 each time and don't check the result, then say if at some instance realMoney = 8 and dispMoney = 7 then dispMoney will annonyingly fructuate between 7 and 12 indefinitely). The check would be easy. I just put out the simplest possible codes as a start:


Thanks Gilbert, this worked like a charm, but now I have a new problem. If I want to add a number of money such as 30000, if it only adds dispmoney+=5 in the script it's still very slow, if I change the number for example to dispmoney+=95 or 105 now it reaches the new value as quickly as I need, but now if I add money less than 105 only the value appears without the animation. Is there a way to regulate the animation depending on the amounts of money? I mean that the animation effect available to be seen with small numbers, but in very high numbers the animation beign much faster, for example only 2 or 3 seconds.

Gilbert

#14
I think in some games, the increment would scale with the difference between the actual value and displayed value.
I haven't tested, but try this if it improves anything:
Code: ags

int dispMoney, realMoney;

function repeatedly_execute_always()
{
  // ... (anything else you have in this function)

  if (dispMoney < realMoney)
  {
    dispMoney+= (realMoney - dispMoney)/10 + 1;
    if (dispMoney > realMoney) dispMoney = realMoney;
  }
  else if (dispMoney > realMoney)
  {
    dispMoney-= (dispMoney - realMoney)/10 + 1;
    if (dispMoney < realMoney) dispMoney = realMoney;
  }  
  lblMoney.Text = String.Format("%d", dispMoney);
}


You may tweak the "/10" part to see what speed is optimal for you. Note that these are all integer arithmetic, so say, 4/10 = 0 (not 0.4) and thus the "+1" is added to compensate with it never adding anything to reach the real value.

Khris

#15
You can also use easing like this:

Code: ags
int dispMoney, realMoney;
 
function repeatedly_execute_always() {

  int diff = dispMoney - realMoney;
  if (diff > -5 && diff < 5) dispMoney = realMoney;
  else dispMoney += diff / 10;

  lblMoney.Text = String.Format("%d", dispMoney);
}


This will approach the target value fast if the difference is big, and get slower the closer it gets. Adjust the 10 to change the speed.

Edit: code fixed, thanks Matti :)

WiseFrog

First of all I want to thank everyone for the help. Khris I tried your code and for some reason it didn't work, it didn't even get animated, the new number just appears.
Gilbert your code worked perfectly, now with small numbers it goes slow and with bigger  number it goes fast, thank you very very much.

Matti

In Khris code I think it should be && instead of || in line 6:

Code: ags
  if (diff > -5 && diff < 5) dispMoney = realMoney;

SMF spam blocked by CleanTalk