Updating a Journal GUI dynamically

Started by newwaveburritos, Wed 30/06/2021 05:42:54

Previous topic - Next topic

newwaveburritos

I have a journal in my game that updates as the player progresses and one of the things it does is keep track of the player's relationships which can be leveled up from 1-10.  The way I have it now is that each character has a page in the journal and when you level up you get a little heart graphic for which I use a button on the GUI.  So, when that game starts you have zero hearts up to a maximum of 10 hearts.  Well, I want to track this as I test the game so I made a little GUI that I can use to update the variable I use to track the player's relationship.  While in the game I don't think you'll be able to lose these points I want to be able to add or subtract them while I test just to make sure they work so just enabling them isn't enough since if I move backward the button will have already been enabled.  What I have here works but I can't imagine this is the easiest way to code this but I've been scratching my head and I really can't think of a better way.  I'm sure I am missing something and would appreciate any help.  Thanks so much!

In repeatedly_execute:

Code: ags
if (GinaFriendshipPoints == 1){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = false;
   btnGinaFP02.Visible = false;
   btnGinaFP03.Visible = false;
   btnGinaFP04.Visible = false;
   btnGinaFP05.Visible = false;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
 }
  
 if (GinaFriendshipPoints ==2){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = false;
   btnGinaFP03.Visible = false;
   btnGinaFP04.Visible = false;
   btnGinaFP05.Visible = false;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
 }
   if (GinaFriendshipPoints ==3){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = false;
   btnGinaFP04.Visible = false;
   btnGinaFP05.Visible = false;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==4){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = false;
   btnGinaFP05.Visible = false;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
  
  if (GinaFriendshipPoints ==5){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = false;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==6){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = true;
   btnGinaFP06.Visible = false;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==7){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = true;
   btnGinaFP06.Visible = true;
   btnGinaFP07.Visible = false;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==8){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = true;
   btnGinaFP06.Visible = true;
   btnGinaFP07.Visible = true;
   btnGinaFP08.Visible = false;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==9){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = true;
   btnGinaFP06.Visible = true;
   btnGinaFP07.Visible = true;
   btnGinaFP08.Visible = true;
   btnGinaFP09.Visible = false;
   }
   
   if (GinaFriendshipPoints ==10){
   btnGinaFP00.Visible = true;
   btnGinaFP01.Visible = true;
   btnGinaFP02.Visible = true;
   btnGinaFP03.Visible = true;
   btnGinaFP04.Visible = true;
   btnGinaFP05.Visible = true;
   btnGinaFP06.Visible = true;
   btnGinaFP07.Visible = true;
   btnGinaFP08.Visible = true;
   btnGinaFP09.Visible = true;
   }

Khris

Using your button approach, and provided the buttons are positioned in the order of their ID, you can use the gGina.Controls[] array (or whatever the GUI is called).

Code: ags
  for (int i = 1; i <= 10; i++) gGinaControls[i-1].Visible = i >= GinaFriendshipPoints;

(assuming the first buttons is GUIControl #0)

However you can also use a single button and a for loop to redraw its image when the points change.

newwaveburritos

Thank you, this works.  I changed the values since it actually doesn't start at 00 but rather 02 and it was also making the value inverted so I changed the >= to a <= and it seems to be working fine.  Here's the whole function in case somebody wants to do a similar thing later.  If I were to do this to the other characters later it would just be a matter of creating the buttons and then adjusting that equation to make it the correct value?  If for your next friend it were the GUI buttons 22-32 you would just change the [i+1] and variable accordingly?  I am going to have at least 100 of these buttons, though.  It sounds like it would be easier to make a single button and redraw it with the for loop as you say but I haven't done this before and I'm not sure exactly what you mean.

Code: ags
function iNotebook_Look()
{
  Wait(10);
  for (int i = 1; i <= 10; i++) gFriendshipNotebook.Controls[i+1].Visible = i <= GinaFriendshipPoints;
  gFriendshipNotebook.Visible = true;
  FriendshipGUIScreenshotSprite = DynamicSprite.CreateFromScreenShot(320, 200);
    if (FriendshipGUIScreenshotSprite != null) {
      gFriendshipNotebook.BackgroundGraphic = FriendshipGUIScreenshotSprite.Graphic;
      //for (int i = 1; i <= 10; i++) gFriendshipNotebook.Controls[i-1].Visible = i >= GinaFriendshipPoints;
    }

}

Khris

You would use a DynamicSprite, draw the heart sprite to it x number of times using a loop, then assign the DynamicSprite as the button's NormalGraphic.
Or you don't use any buttons and draw all hearts to the GUI's background sprite.

newwaveburritos

Thanks so much.  I understand now.

SMF spam blocked by CleanTalk