Scripts (new question at bottom)

Started by shaungaryevans, Sun 31/05/2009 13:48:33

Previous topic - Next topic

shaungaryevans

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


Ghost

#1
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.

shaungaryevans

How do you link a GUI button to your own script?

Khris

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.

Code: ags
function my_loadbutton_click() {
  ...
}


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

Code: ags
import function my_loadbutton_click();


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

Code: ags
function loadbutton_OnClick() {
  my_loadbutton_click();
}

shaungaryevans

Quote from: KhrisMUC on Sun 31/05/2009 17:38:45
In there, put the custom function, e.g.

Code: ags
function my_loadbutton_click() {
  ...
}


Is this in the global script or the scrpit i made?

shaungaryevans

Can you import the function on_key_code with the int keycode

Ghost

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??

shaungaryevans

it alright, i done it now, thanks

shaungaryevans

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? 

shaungaryevans

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)

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Ghost

#11
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:

Code: ags

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:

Code: ags

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.

Code: ags

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.

Lt. Smash

Quote from: Ghost on Mon 01/06/2009 02:38:46
Code: ags

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
Code: ags

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

Ghost

#13
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?

shaungaryevans

It works now. I was doing it the wrong way round.

Thank you guys (and girls) for all your help.

Ghost

Glad it helped.

Where's the girls?

Lt. Smash

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.

Ghost

Sorry- could be read the harsh way, that wasn't intentional...

SMF spam blocked by CleanTalk