Scripting, Code & Interaction: Difference between revisions

*>Ashen
 
(10 intermediate revisions by 7 users not shown)
Line 157: Line 157:


If the music is disabled, or there is no sound card, '''GetMP3posMillis()''' will always return "0". So enable music and buy a damn sound card. :)
If the music is disabled, or there is no sound card, '''GetMP3posMillis()''' will always return "0". So enable music and buy a damn sound card. :)
WARNING ! This function is now deprecated.
Use '''AudioChannel.Seek''' , instead


==Fatal error when using "=" and "==" with strings==
==Fatal error when using "=" and "==" with strings==
Line 180: Line 184:


Confused? First, we declare a String and name it '''my_name'''. Next, we use '''Game.GlobalString''' to store the string in slot #17 into the String we created. Next, '''Character.Say''' will be used and the '''%s''' is replaced with the string '''my_name'''. Got it?
Confused? First, we declare a String and name it '''my_name'''. Next, we use '''Game.GlobalString''' to store the string in slot #17 into the String we created. Next, '''Character.Say''' will be used and the '''%s''' is replaced with the string '''my_name'''. Got it?
You can also do this with int variables, e.g. to display numbers on GUI Labels:
You can also do this with int variables, e.g.:


   int my_int = 3;
   int my_int = 3;
Line 224: Line 228:


AGS 2.71 has support for longer strings using the '''String''' datatype, but some parts of AGS still have the 200 character limit, so beware.
AGS 2.71 has support for longer strings using the '''String''' datatype, but some parts of AGS still have the 200 character limit, so beware.
This error can also be thrown if you wrap your string in single-quotes instead of double-quotes.  You must use double-quotes in functions like player.Say("Hello World.");


==Slowing down your loops==
==Slowing down your loops==
Line 282: Line 288:


And just in case you're confused about the '[' and ']' in there, that just means that it's something you don't have to include. Your functions don't have to have any parameters, and they can have up to 9.
And just in case you're confused about the '[' and ']' in there, that just means that it's something you don't have to include. Your functions don't have to have any parameters, and they can have up to 9.
==Moving functions to separate script files==
As a game becomes more complex, the code in GlobalScript becomes harder and harder to maintain. So it makes sense to place certain functions into separate script files.
A script consists of two parts: A header file (ending on .ash) and the script code file (ending on .asc). When you add a new script via the Script section of the project tree (right-click -> new script), a pair of these files will automatically be generated.
As an example, let us try move a function doStuff() from GlobalScript to a new script MyScript.
  function doStuff() {
  //doing stuff here
  }
When moving the code of the function from GlobalScript.asc to MyScript.asc, you will most likely get an error when you try to run it. This is due to the fact that the function has not yet been made public to the project. In order to do so, you have to import it in the header file MyScript.ash:
  import function doStuff();
 
If you have some experience in other programming languages, this may be a bit confusing, since the import does not work like ''#include'' in C/C++ or ''import'' in python. It does not make code visible to your script, but rather makes the function from the script visible to the whole project. Every function not imported that way will only be usable within the script where it is defined.
Note by the way that unlike global variables, functions do not need to be exported (using the ''export'' command) in order to work.
''So now my function can be accessed from anywhere?''
Well, almost. In the project tree, all scripts are organized as a list. Each script can only see functions defined in scripts above them in that list (and of course its own functions). GlobalScript is usually at the bottom of the list, so it can access all other functions, but no other script can access functions in GlobalScript. Room scripts are an exception, as they do not appear in the Scripts list but have access to all other scripts, including GlobalScript.
In our example, doStuff() can now be accessed from GlobalScript, any room script and any script below MyScript in the Scripts list.
''So what to do if my script has to call a function defined in GlobalScript?''
It cannot call a function in GlobalScript directly. Therefore it would be advisable to move that function to MyScript as well, or place it in a new script file that lies above MyScript.
''I tried that, and it works, but when I move a character action to my own script it stops working.''
Certain built-in functions (e.g. ''repeatedly_execute()'') can be used in custom scripts and will be called before their counterpart in GlobalScript is called.
Most other built-in functions can only be called from GlobalScript, and that includes any character or item-related actions.
If you still want to move these functions to separate scripts, you have to use helper functions.
As an example, let us try to create a working character interact function for a bartender character in MyScript:
# Create a character interact action cBartender_Interact from the character's action menu in the project tree. The corresponding function will be generated in GlobalScript.asc.
# Write a new function bartenderInteractHandler() in MyScript.asc to contain the actual action code
# Import the function in MyScript.ash to make it visible to the project
# Call bartenderInteractHandler() from the cBartender_Interact() function defined in GlobalScript.asc


==Defining custom hotkeys and shortcuts==
==Defining custom hotkeys and shortcuts==
Line 382: Line 430:
''I can't seem to find in the manual how to turn objects etc, on and off from another room. Is there just not a command for this or am I going insane?''
''I can't seem to find in the manual how to turn objects etc, on and off from another room. Is there just not a command for this or am I going insane?''


No, you're not going insane - there's no direct way to do this. You'll have to use variables (GlobalInts or your own - see the earlier entry on [[Scripting%2C_Code_%26_Interaction#Working_with_variables:_the_basics|working with variables]]), and check them in the 'Player enters Room' interaction of the room the Object (or whatever) is in. If you're likely to need to do it a lot you might want to try the {{Thread|20650|OtherRoom module}}, which handles everything for you.
No, you're not going insane - there's no direct way to do this. You'll have to use variables (GlobalInts or your own - see the earlier entry on [[Scripting%2C_Code_%26_Interaction#Working_with_variables:_the_basics|working with variables]]), and check them in the 'Player enters Room' interaction of the room the Object (or whatever) is in. If you're likely to need to do it a lot you might want to try the {{Thread|OtherRoom module|20650}}, which handles everything for you.
 
''But what about character actions? They are not tied to a room and I can't use variables here.''
 
If you know that the character is in a specific room, you can access the room's objects/hotspots/regions with appropriate variables and the corresponding index of the item you want to access:
 
  if (player.Room == 3)
  {
    object[0].Visible = true; //make object with ID 0 visible
    hotspot[4].Enabled = false; // enable hotspot with ID 4
    region[7].Enabled = true; // enable region with ID 7
  }
 
Note that you are entering dangerous terrain by doing this. If you remove an object from a room, all subsequent objects will move up in the list, causing their ID to decrease by 1. If you don't adapt your ID in the object[] call, you will get some weird bugs.
 
Avoid another trap by making absolutely sure that the character is really in that room when you access the objects. If not and there is no hotspot with ID 4 in the current room, the game will crash.


[[Category:AGS Beginners' FAQ]]
[[Category:Beginner Tutorials]]
[[Category:Scripting]]
[[Category:Scripting]]
1

edit