Basic scripting – minimizing duplicate code in Global Script.

Started by , Mon 10/07/2006 04:52:54

Previous topic - Next topic

Auto

I have what I think is a simple question.Ã,  I've been unable to find the answer in the search because I'm having trouble knowing what words to use to describe what I'm looking for.Ã,  Basically I have a lot of duplicate code in my Global Script.Ã,  I want to create a “function” or something and then “call” it from a lot of different places in the Global Script to cut down on all this repetition of code in the Global Script.Ã,  I know this is really basic, please just let me know the words to use to search for this and I'll be happily on my way.

Incase this is helpful, so you know more of what I'm doing…Ã,  I have a lot of GUIs with a lot of buttons.Ã,  When I click on many of the buttons they make lots of the GUIs open or close because that is what they are for.Ã,  A lot of the buttons make the same GUI's open or close the same way.Ã,  So I have a lot of repetitive code for so many buttons opening and closing GUIs the same way.Ã,  It seems so sloppy and redundant in my Global Script.Ã,  I wish I could write the redundant open and close GUIs list once, then refer to that list for each of the separate buttons.

scotch

You used the right words.

Create a function like this:

Code: ags
function myFunction() {
Ã,  Ã, Code here;
}


and call it like:

Code: ags
myFunction();


You can also specify parameters to use in the function like:

Code: ags
function myFunction(String a) { Display(a); }


and you can also return values from a function like:

Code: ags
int add(int a, int b) { return a+b; }


You should define your functions at the top of your global script. If you want to be able to call the function in room scripts then you are required to "import" it from the global script by adding a like like this to the script header:

Code: ags
import function myFunction(String);


and it will try and import the function that matches that prototype.

Ashen

That's also covered in the BFAQ, and the manual.

In this specific case, though, there's another (similar) answer. Multiple GUI controls (Buttons, in this case) can be set to use the same control function (the code that runs when you click them). So, rather than having btnOne use btnOne_click, btnTwo use btnTwo_click, btnThree use btnThree_click, etc  - that all contain the same code - you can set them all to run btnDoStuff_Click, by changing the 'Click' property on their 'Properties' window. (Basically, since AGS will create a control function for you, you don't need to worry about making your own, just re-use it. However, getting to grips with making functions in general will definately help minimise dulpicate code in your script in general.)

Note that only Controls of the same type can share a function - multiple buttons can run the same one, multiple TextBoxes can run the same one, but a Button and a TextBox need their own.
I know what you're thinking ... Don't think that.

Auto

This is all very helpful, exactly what I was looking for.Ã,  I am pointed in the right direction and I am progressing well now.

SMF spam blocked by CleanTalk