double function button[SOLVED]

Started by Construed, Fri 17/02/2012 22:17:26

Previous topic - Next topic

Construed

Hey, I'm trying to use these two functions:
Code: ags

///////////////////////////////////////////////////////////////////////////////

function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  txtNewSaveName.Text = nick;
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  int i = 0;
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
    }
    i++;
  }
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  close_save_game_dialog();
}

function btnIconSave1_OnClick(GUIControl *control, MouseButton button)
{
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  

  gSaveGame.Visible = true;
  // Get the list of save games
  lstSaveGamesList.FillSaveGameList();
  if (lstSaveGamesList.ItemCount > 0)
  {
   //If there is at least one, set the default text
   //to be the first game's name
    
  txtNewSaveName.Text = lstSaveGamesList.Items[0];
  }
  else
  {
  txtNewSaveName.Text = "";
  }
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;

 }



/*//////////////////////////////////////////////////////////////////////////

With this function:
Code: ags

function btnQuit_OnClick(GUIControl *control, MouseButton button)
{
  
  btnIconSave1_OnClick(btnIconSave1, eMouseLeft);
  Wait(20);
  btnSaveGame_OnClick(btnSaveGame, eMouseLeft);
  QuitGame(1);
  
}


All seems to work, however I have to press the quit button twice for the save function to work.
Anyone have any idea why I'm getting this type of behavior from this?
I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

Khris

You can't code like this. You have to understand what a command does before you use it.
Calling those two functions one after another, the only lines from btnIconSave1_OnClick that have any effect are:
Code: ags
  lstSaveGamesList.FillSaveGameList();
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;

And the latter two don't even really do anything save game related.
Removing the rest wouldn't make any difference.

What the original code does is check whether the entered save game name is the same as that of an existing save game. If it is, that save game is overwritten; to avoid having two save games with the same name (because they can't be distinguished in a list that only shows the name).

Since you're setting the name to nick, you're overwriting the same save game every time.

I have no idea why you think you have to press the button twice, but that's clearly not what's wrong with your code.

What exactly do you intend to do?
Do you want save game names called "nick1", "nick2", etc. where nick is the player's nickname?

Construed

Yes, The code currently saves nick1 nick2 nick3 etc from a predefined string.
Before the game starts the player is asked their name which is inputted into the:
String nick;

Everything works great except if i click quit, when those 2 commands fire off it doesn't immediately save, I have to click play instead of quit, then press quit again and it saves correctly.
I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

Construed

Well since this is the case, Anyone know if theres a way i can just cause this function to run twice automatically?
I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

Construed

Whoops >< just saw no bumping in the forum rules  :o
I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

Construed

AHAHAH I finally figured it out!
its because you cant run both of the functions twice in the same GUI.
[SOLVED]
I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

SMF spam blocked by CleanTalk