Author Topic: Show a increase numbers int till the new value  (Read 388 times)

Show a increase numbers int till the new value
« on: 04 May 2021, 04:31 »
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

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
Re: Show a increase numbers int till the new value
« Reply #1 on: 04 May 2021, 06:13 »
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: Adventure Game Studio
  1. if (dispmonie < realmonie) dispmonie++;
  2. else if (dispmonie > realmonie) dispmonie--;

Re: Show a increase numbers int till the new value
« Reply #2 on: 04 May 2021, 07:13 »
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?
« Last Edit: 04 May 2021, 07:29 by WiseFrog »

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Show a increase numbers int till the new value
« Reply #3 on: 04 May 2021, 07:30 »
You also need to actually update the label. Something like this (assuming the label is called lblMoney):

Code: Adventure Game Studio
  1. int dispMoney, realMoney;
  2.  
  3. function repeatedly_execute_always()
  4. {
  5.   // ... (anything else you have in this function)
  6.  
  7.   if (dispMoney < realMoney)
  8.     dispMoney++;
  9.   else if (dispMoney > realMoney)
  10.     dispMoney--;
  11.   lblMoney.Text = String.Format("%d", dispMoney);
  12. }

Re: Show a increase numbers int till the new value
« Reply #4 on: 04 May 2021, 07:40 »
You also need to actually update the label. Something like this (assuming the label is called lblMoney):

Code: Adventure Game Studio
  1. int dispMoney, realMoney;
  2.  
  3. function repeatedly_execute_always()
  4. {
  5.   // ... (anything else you have in this function)
  6.  
  7.   if (dispMoney < realMoney)
  8.     dispMoney++;
  9.   else if (dispMoney > realMoney)
  10.     dispMoney--;
  11.   lblMoney.Text = String.Format("%d", dispMoney);
  12. }

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
« Last Edit: 04 May 2021, 07:42 by WiseFrog »

Re: Show a increase numbers int till the new value
« Reply #5 on: 04 May 2021, 08:22 »
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.
Fail at Floaty Rog' now!  still having to deal with what games are going through

Gilbert

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
Re: Show a increase numbers int till the new value
« Reply #6 on: 04 May 2021, 10:41 »
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: Adventure Game Studio
  1. int dispMoney, realMoney;
  2.  
  3. function repeatedly_execute_always()
  4. {
  5.   // ... (anything else you have in this function)
  6.  
  7.   if (dispMoney < realMoney)
  8.   {
  9.     dispMoney+=5;
  10.     if (dispMoney > realMoney) dispMoney = realMoney;
  11.   }
  12.   else if (dispMoney > realMoney)
  13.   {
  14.     dispMoney-=5;
  15.     if (dispMoney < realMoney) dispMoney = realMoney;
  16.   }  
  17.   lblMoney.Text = String.Format("%d", dispMoney);
  18. }

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Show a increase numbers int till the new value
« Reply #7 on: 04 May 2021, 11:56 »
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

Re: Show a increase numbers int till the new value
« Reply #8 on: 04 May 2021, 14:44 »
Isn't that what WiseFrog meant? That it worked in repeatedly_execute_always?

Re: Show a increase numbers int till the new value
« Reply #9 on: 04 May 2021, 15:10 »
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.

Re: Show a increase numbers int till the new value
« Reply #10 on: 04 May 2021, 15:53 »
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:
I 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
« Last Edit: 04 May 2021, 15:55 by Khris »
Fail at Floaty Rog' now!  still having to deal with what games are going through

Matti

Re: Show a increase numbers int till the new value
« Reply #11 on: 05 May 2021, 10:08 »
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

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Show a increase numbers int till the new value
« Reply #12 on: 05 May 2021, 10:58 »
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:

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.)
« Last Edit: 05 May 2021, 11:01 by Snarky »

Re: Show a increase numbers int till the new value
« Reply #13 on: 06 May 2021, 06:41 »
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.


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

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
Re: Show a increase numbers int till the new value
« Reply #14 on: 06 May 2021, 10:05 »
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: Adventure Game Studio
  1. int dispMoney, realMoney;
  2.  
  3. function repeatedly_execute_always()
  4. {
  5.   // ... (anything else you have in this function)
  6.  
  7.   if (dispMoney < realMoney)
  8.   {
  9.     dispMoney+= (realMoney - dispMoney)/10 + 1;
  10.     if (dispMoney > realMoney) dispMoney = realMoney;
  11.   }
  12.   else if (dispMoney > realMoney)
  13.   {
  14.     dispMoney-= (dispMoney - realMoney)/10 + 1;
  15.     if (dispMoney < realMoney) dispMoney = realMoney;
  16.   }  
  17.   lblMoney.Text = String.Format("%d", dispMoney);
  18. }

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.
« Last Edit: 06 May 2021, 10:10 by Gilbert »

Re: Show a increase numbers int till the new value
« Reply #15 on: 06 May 2021, 13:16 »
You can also use easing like this:

Code: Adventure Game Studio
  1. int dispMoney, realMoney;
  2.  
  3. function repeatedly_execute_always() {
  4.  
  5.   int diff = dispMoney - realMoney;
  6.   if (diff > -5 && diff < 5) dispMoney = realMoney;
  7.   else dispMoney += diff / 10;
  8.  
  9.   lblMoney.Text = String.Format("%d", dispMoney);
  10. }

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 :)
« Last Edit: Yesterday at 20:58 by Khris »
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: Show a increase numbers int till the new value
« Reply #16 on: 09 May 2021, 05:02 »
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

Re: Show a increase numbers int till the new value
« Reply #17 on: 09 May 2021, 09:12 »
In Khris code I think it should be && instead of || in line 6:

Code: Adventure Game Studio
  1.   if (diff > -5 && diff < 5) dispMoney = realMoney;