Number of a dynamicsprite ?

Started by Baguettator, Sat 23/10/2021 12:53:53

Previous topic - Next topic

Baguettator

Hi there,

Little question : if I create a dynamicsprite and assign it as a graphic of a button for example. If then I write :

Code: ags
int g=mybutton.Graphic;


Which value should be g ?

Because I have in my games a lot of buttons that use the same "template" for their graphics, but as these buttons have very differents heights/widths, I prefer to create dynamicsprite for each of these buttons when the game starts instead of import a lot of images that will make the game heavier.

That's my code :

Code: ags
DynamicSprite* fondshachures[50];

function gamestart()
{
  int h[50], l[50];
  int c;
  for (int i=0 ; i<Game.GUICount ; i++)
  {
    for (int j=0 ; j<gui[i].ControlCount ; j++)
    {
      if (gui[i].Controls[j].AsButton!=null && gui[i].Controls[j].AsButton.Graphic==0)
      {
        bool v=false;
        // Check if the dynamic sprite exists with the same width/height
        for (int k=0 ; k<c ; k++)
        {
          if (gui[i].Controls[j].AsButton.Width==l[k] && gui[i].Controls[j].AsButton.Height==h[k])
          {
            v=true;
            gui[i].Controls[j].AsButton.NormalGraphic=fondshachures[k];
            k=c;
          }
        }
        
        if (v==false) // The dynamic sprite doesn't exist yet
        {
          gui[i].Controls[j].AsButton.CreerFond(c);
          h[c]=gui[i].Controls[j].AsButton.Height;
          l[c]=gui[i].Controls[j].AsButton.Width;
          c++;
        }
      }
    }
  }
}


I know that button.Graphic==0 means no sprite is assigned to that button (so it's the grey button by default). So my goal is to scan all the buttons that are grey when the game starts, and create/assign to them a dynamicsprite.

Crimson Wizard

#1
DynamicSprite's number is contained in its DynamicSprite.Graphic property.

So in your case it should probably be:
Code: ags

gui[i].Controls[j].AsButton.NormalGraphic=fondshachures[k].Graphic;

Baguettator

OK I see. But what's the number of button.Graphic if this button has no sprite ? (I mean : the button is the grey default button in the editor).


Because my script seems to not do anything. And I wrote if (button.Graphic==0) to detect buttons without any sprite.

Crimson Wizard

Quote from: Baguettator on Sat 23/10/2021 18:54:10
But what's the number of button.Graphic if this button has no sprite ? (I mean : the button is the grey default button in the editor).


Because my script seems to not do anything. And I wrote if (button.Graphic==0) to detect buttons without any sprite.

Oh, I misread the question. I think Button.Graphic can be <= 0.

How it's noted in the manual:
Quote
readonly int Button.Graphic;

Gets the current image on a GUI button. If a value less than 1 is returned, then no image is currently displayed on the button.

Baguettator

Hmm ok, well I found out that if the function was called in a game_start function, buttons won't have their graphic changed (why ? I don't know !). So I ran my script in the room 1, before fade in, and in a DoOnceOnly condition. Thanks Crimson, as always perfect !

Crimson Wizard

Quote from: Baguettator on Sat 23/10/2021 19:32:43
Hmm ok, well I found out that if the function was called in a game_start function, buttons won't have their graphic changed (why ? I don't know !). So I ran my script in the room 1, before fade in, and in a DoOnceOnly condition.

I never heard about such problem. There are few issues with game_start, for example the room is not loaded yet so you cannot access anything from the room. But I did not know that buttons work differently...

SMF spam blocked by CleanTalk