Calling functions from modules [solved]

Started by Revae, Sun 28/10/2012 07:16:58

Previous topic - Next topic

Revae

I have a save gui I'm working on.  All the code works fine from inside the global script, but I'd rather have my code somewhat cleaner than that (I say somewhat because I'm more an artist than a coder and it's pretty messy all over as is).

Anyway.  I understand that you have to import the scripts from the header files of each script (I've read other posts on this), but I'm having a hard time figuring out how to call the button functions from the script.  My save gui script looks like this:
Code: AGS

int SavSel=0;

//Funks//
function btnClsSavLd_OnClick(GUIControl *control, MouseButton button){
gSaveGameScreen.Visible = false;
gJournal.Visible=true;
}

function btnSelSlt1_OnClick(GUIControl *control, MouseButton button){
btnSelSltA.NormalGraphic=1552;

btnSelSlt2.NormalGraphic=1552;

btnSelSlt3.NormalGraphic=1552;

btnSelSlt1.NormalGraphic=1554;

SavSel=1;
}

function btnSelSlt2_OnClick(GUIControl *control, MouseButton button){
btnSelSltA.NormalGraphic=1552;

btnSelSlt1.NormalGraphic=1552;

btnSelSlt3.NormalGraphic=1552;

btnSelSlt2.NormalGraphic=1554;

SavSel=2;
}

function btnSelSlt3_OnClick(GUIControl *control, MouseButton button){
btnSelSltA.NormalGraphic=1552;

btnSelSlt2.NormalGraphic=1552;

btnSelSlt1.NormalGraphic=1552;

btnSelSlt3.NormalGraphic=1554;

SavSel=3;
}

function btnSelSltA_OnClick(GUIControl *control, MouseButton button){
btnSelSlt1.NormalGraphic=1552;

btnSelSlt2.NormalGraphic=1552;

btnSelSlt3.NormalGraphic=1552;

btnSelSltA.NormalGraphic=1554;

SavSel=4;
}


function btnSave_OnClick(GUIControl *control, MouseButton button){
  if (SavSel==1){
  gJournal.Visible=false;
  SaveGameSlot(1, "Save 1");
  gSaveGameScreen.Visible=false;
  }

  if (SavSel==2){
  gJournal.Visible=false;
  SaveGameSlot(2, "Save 2");
  gSaveGameScreen.Visible=false;
  }

  if (SavSel==3){
  gJournal.Visible=false;
  SaveGameSlot(3, "Save 3");
  gSaveGameScreen.Visible=false;
  }

  if (SavSel==4){
  gJournal.Visible=false;
  SaveGameSlot(4, "Auto Save");
  gSaveGameScreen.Visible=false;
  }

  else{
  
  }
}



What would I put in the header file, and how would I call the functions from the global file?  Thanks in advance!

Edit:  Alright. Solved in the very next post.  Thanks Khris.

Khris

Unfortunately, the handler functions for buttons and other GUI elements have to be in the global script.

However, you can move the contents to a module, and you don't need a separate function for each button. That's pretty tidy at least.
You can handle all clicks in one function or group them in a logical way by using the control parameter:

Code: ags
// GlobalScript.asc

// this handles all three slot clicks
function btnSelSlt_OnClick(GUIControl *control, MouseButton button) {
  // call module function:
  MySaveGame.ClickSlot(control.AsButton);
}

Now paste "btnSelSlt_OnClick" into all buttons' event properties.

The module would look like this:
Code: ags
// header

struct MySaveGame {
  import static void ClickSlot(Button* slot);
};

// main script

static void MySaveGame::ClickSlot(Button* slot) {
  int i = 2;   // first button's ID: 2
  while (i <= 4) {   // third button's ID: 4
    Button* b = gSaves.Controls[i].AsButton;
    if (b.ID == slot) b.NormalGraphic = 1554;
    else b.NormalGraphic = 1552;
    i++;
  }
  SaveSel = slot.ID - 1;     // (ID: 2 - 4: -> slot 1 - 3)
}

Revae

Snap.  That's pretty tricky.  The code you suggested makes about half-sense to me though since I'm muddling through this.  I only partially understand was a struct is, and I don't know what * means in this context (though I plan on looking it up).  I also don't understand why you started counting at 2 instead of 1.

But I can definitely get it to work, even with my limited capabilities, thanks to you.  So...  Thanks.  To you.  :)

I may see if I can enlist the help of a scripter in the future because, while I can knock out art pieces all day, learning the scripting along the way has really taken a toll on my time.

Khris

You don't really need a struct here; I just like to group my module functions by naming them "Module.Command()", and you need to make them struct members for that.
You can do this instead:

Code: ags
// header
import void DoSomething(int param);   // make function accessible by other scripts

// main module script
void DoSomething(int param) {
  Display("The parameter is: %d", param);
}


The * is needed because instead of a datatype like int or String which holds a discrete value like 5 or "Hello", I'm declaring a so-called pointer, a variable pointing to a script object (in this case a GUIButton).

I started counting at 2 as an example; ID's 0 and 1 could've been used by a label and a close-gui button for instance.
You can see the ID of the GUI elements in the dropdown menu where you can select them in order to edit them.
Also, computers usually start counting at 0, not 1. Whenever you're creating a GUI with several similar GUIControls (save slots, telephone keys, etc.) you should plan the numbering from the beginning.

SMF spam blocked by CleanTalk