Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: shaungaryevans on Sun 31/05/2009 13:48:33

Title: Scripts (new question at bottom)
Post by: shaungaryevans on Sun 31/05/2009 13:48:33
Hi,

My game at the moment in samll so all the coding is on the rooms and the global scrpits. The gobal scprit is filling up but I want to build a large game but the gobal scrpit will get too full and it will be very hard finding what I want. I know you can make more scripts but if you made a scipt called 'Misson 2'. how would you go about linking a GUI (which is for 'Mission 2') to the 'Mission 2' script please?

Do you have the link the GUI to the 'Mission 2' script through the global script?

Thank you in advance

Title: Re: Scripts
Post by: on Sun 31/05/2009 13:57:37
Straight from the *manual*:

The main global script still has to contain all the event functions (Look At Character scripts, Interact With Inventory scripts and so forth) and all the GUI handlers (btnSave_Click, etc).
But if you have any custom functions then you can put them in a separate script in order to divide up your code.


So you can move any selfmade functions into an additional script to avoid clutter- this will essentially allow you to build a small library you can easily move between projects. And of course you can CALL your selfmade stuff from the GUI code in your global script, just as you said.
Title: Re: Scripts
Post by: shaungaryevans on Sun 31/05/2009 15:42:00
How do you link a GUI button to your own script?
Title: Re: Scripts
Post by: Khris on Sun 31/05/2009 17:38:45
Create a script by right clicking the scripts node in the project tree. Make sure it is above the global script (you can right-click a script and select "move up/down").
In there, put the custom function, e.g.

function my_loadbutton_click() {
  ...
}


Import the function in the new script's header to make it available in the other scripts.

import function my_loadbutton_click();

In the global script, inside the button's OnClick, call the function:

function loadbutton_OnClick() {
  my_loadbutton_click();
}
Title: Re: Scripts
Post by: shaungaryevans on Sun 31/05/2009 17:46:03
Quote from: KhrisMUC on Sun 31/05/2009 17:38:45
In there, put the custom function, e.g.

function my_loadbutton_click() {
 ...
}


Is this in the global script or the scrpit i made?
Title: Re: Scripts
Post by: shaungaryevans on Sun 31/05/2009 18:08:33
Can you import the function on_key_code with the int keycode
Title: Re: Scripts
Post by: on Sun 31/05/2009 19:35:29
a) The button's script will be in the GLOBAL SCRIPT. Because it is a GUI function, and GUI functions need to be in the global script. From within the button's script, you can call a selfmade function.

b) function on_key_press(eKeyCode keycode). Part of the global script too, so it's already available to the whole game. Where do you want to import that, and why??
Title: Re: Scripts
Post by: shaungaryevans on Sun 31/05/2009 19:42:21
it alright, i done it now, thanks
Title: Re: Scripts
Post by: shaungaryevans on Sun 31/05/2009 22:09:26
I have tried the new script out and I did it like this,

I put

function doyouwant_1_OnClick(GUI *theGui, MouseButton button){}

in the global script, then I imported this function into the .ash file  of my scrpit. then I put it in my main script (.asc) and then put what a want it to do when i click on it.

Am i doing it right? 
Title: Re: Scripts (New Question at bottom)
Post by: shaungaryevans on Sun 31/05/2009 23:27:17
Want does this mean please...

Error running function 'doyouwant_1_OnClick':
Runtime erroe: wrong number of paeameters to exported function 'doyouwant_1_OnClick' (expected 1, supplied 2)
Title: Re: Scripts (new question at bottom)
Post by: Trent R on Mon 01/06/2009 01:41:07
You need 2 parameter to called that function. A GUI, and a MouseButton.

But why are you exporting/importing that function into another script? It would make more sense to create a do_you_want function and then call that in the button's OnCliick function, and wherever else it is needed.


~Trent
Title: Re: Scripts (new question at bottom)
Post by: on Mon 01/06/2009 02:38:46
Yes, Trent's right. There's really no need for you to import or export stuff into files where it doesn't belong, and won't work.

Look, try it like this.

For examples sake, let's say you want a button that makes the player say a random quote. Each time you press the button, the player will come up with a funky one-liner.

First, create the button.
Second, create the button's OnClick event. This will go into the global script:


btnGuiButton1_onClick(GUIControl *control, MouseButton button)
{
}


You do NOT need to import or export this at all. What you do now is: You write your own function. This can be in the global script or in ANOTHER script file you create. (as Khris explained already, you need to have these OVER the global script)

Let's say it looks like this:


function SayRandomQuote()
{
  int i = Random(3);
  if (i == 0) { player.Say("We all live in a Yellow Submarine!"); }
  else if (i == 1) { player.Say("Hehey... I am the walrus!"); }
  else if (i == 2) { player.Say("Here comes the sun..."); }
  else { player.Say("Strawberry Fields forever!") };
}


And now, in the button's OnClick (in the global script), you call your selfmade function.


btnGuiButton1_onClick(GUIControl *control, MouseButton button)
{
  SayRandomQuote()
}


And that's it. Now each time you click the button, your character will come up with a random Beatles song, called from the function SayRandomQuote.

Think of all these events for Gui elements as "entry points" where you can add a lot of stuff, including functions you wrote yourself.

Hope that clears it up a little.

edit:
Removed incorrect code.
Title: Re: Scripts (new question at bottom)
Post by: Lt. Smash on Mon 01/06/2009 08:34:32
Quote from: Ghost on Mon 01/06/2009 02:38:46

btnGuiButton1_onClick(GUIControl *control, MouseButton button)
{
  function SayRandomQuote()
}

you may overlook but you call a function only by its name, its parambrackets and a semicolon.
so it should be

btnGuiButton1_onClick(GUIControl *control, MouseButton button)
{
  SayRandomQuote();
}
Title: Re: Scripts (new question at bottom)
Post by: on Mon 01/06/2009 10:47:14
Quote from: Lt. Smash on Mon 01/06/2009 08:34:32
you may overlook but you call a function only by its name, its parambrackets and a semicolon.

Oh yes. Sorry, I didn't know. I mean, how should I? Duly corrected.
Is parambrackets a word?
Title: Re: Scripts (new question at bottom)
Post by: shaungaryevans on Mon 01/06/2009 17:44:09
It works now. I was doing it the wrong way round.

Thank you guys (and girls) for all your help.
Title: Re: Scripts (new question at bottom)
Post by: on Mon 01/06/2009 17:54:08
Glad it helped.

Where's the girls?
Title: Re: Scripts (new question at bottom)
Post by: Lt. Smash on Mon 01/06/2009 18:34:12
Quote from: Ghost on Mon 01/06/2009 10:47:14
Quote from: Lt. Smash on Mon 01/06/2009 08:34:32
you may overlook but you call a function only by its name, its parambrackets and a semicolon.

Oh yes. Sorry, I didn't know. I mean, how should I? Duly corrected.
Is parambrackets a word?
Not directly. I meant the brackets ( ... ) in which you send your parameters to the function.
Title: Re: Scripts (new question at bottom)
Post by: on Mon 01/06/2009 19:26:27
Sorry- could be read the harsh way, that wasn't intentional...