AGS Pointers for Dummies: Difference between revisions

Jump to navigation Jump to search
Link to online manual
*>Monkey 05 06
mNo edit summary
(Link to online manual)
 
(5 intermediate revisions by 2 users not shown)
Line 3: Line 3:
<center>'''<font size="4">AGS Pointers for Dummies</font>'''</center>
<center>'''<font size="4">AGS Pointers for Dummies</font>'''</center>


<center>'''''<font size="3">A reference for the rest of us!</font>'''''</center>
<center>'''''<font size="3">A Reference for the Rest of Us!</font>'''''</center>


If you're reading this, then you've come looking for answers. ''What are pointers? How do I use them? What makes them better than just using integers?'' These questions, amongst others shall be answered. You're not alone in the confusion caused by pointers. Many people who are new to scripting, or haven't ever used a language that supported pointers don't understand them right away. That's why I'm here to help.
If you're reading this, then you've come looking for answers. ''What are pointers? How do I use them? What makes them better than just using integers?'' These questions, amongst others shall be answered. You're not alone in the confusion caused by pointers. Many people who are new to scripting, or haven't ever used a language that supported pointers don't understand them right away. That's why I'm here to help.
Line 13: Line 13:


==Variables==
==Variables==
A variable, in the context of scripting, is a way to represent a value. In AGS, there are five different {{link|Script language keywords|Data types|data types}} which can be used to represent a variable. These types include '''char''', '''short''', '''int''', '''String'''{{footnote parent|1}}, and '''float'''. A '''char'''-type variable is one that can hold only a single character (i.e., 'A', 'B', etc.) or a number within the range 0 to 255. A '''short'''-type variable can store integers within the range -32768 to 32767. An '''int'''-type variable can store integer values within the range -2147483648 to 2147483647. A '''String'''-type variable can hold a string of characters (i.e., "this is some text!") of ''virtually''{{footnote parent|2}} infinite length. A '''float'''-type variable can store floating-point decimals within the range -2147483648.0 to 2147483647.0, and has precision{{footnote parent|3}} up to approximately 6 decimal places, though this will vary based on the actual number.
A variable, in the context of scripting, is a way to represent a value. In AGS, there are five different [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm data types] which can be used to represent a variable. These types include '''char''', '''short''', '''int''', '''String'''{{footnote parent|1}}, and '''float'''. A '''char'''-type variable is one that can hold only a single character (i.e., 'A', 'B', etc.) or a number within the range 0 to 255. A '''short'''-type variable can store integers within the range -32768 to 32767. An '''int'''-type variable can store integer values within the range -2147483648 to 2147483647. A '''String'''-type variable can hold a string of characters (i.e., "this is some text!") of ''virtually''{{footnote parent|2}} infinite length. A '''float'''-type variable can store floating-point decimals within the range -2147483648.0 to 2147483647.0, and has precision{{footnote parent|3}} up to approximately 6 decimal places, though this will vary based on the actual number.


For information on defining and assigning values to variables read the entry in the manual {{link|Script language keywords|Data types|here}}.
For information on defining and assigning values to variables read the entry in the manual [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm data types].


==Pointers==
==Pointers==
Line 26: Line 26:


==Managed Types==
==Managed Types==
AGS has certain ''managed'' types{{footnote alt parent|4}} that you can not create an instance (variable declaration) of, but you ''can'' create pointers to{{footnote parent|5}}.  All of the variables of managed types are ''managed'' by AGS.  These include the types: {{link|GUI Button functions and properties||Button}}, {{link|Character functions and properties||Character}}, {{link|DateTime functions and properties||DateTime}}, {{link|DynamicSprite functions and properties||DynamicSprite}}, {{link|File functions and properties||File}}, {{link|Game / Global functions||Game}}, {{link|GUI functions and properties||GUI}}, {{link|GUI control functions and properties||GUIControl}}, {{link|Hotspot functions and properties||Hotspot}}, {{link|Inventory item functions and properties||InventoryItem}}, {{link|GUI InvWindow functions and properties||InvWindow}}, {{link|GUI Label functions and properties||Label}}, {{link|GUI List Box functions and properties||ListBox}}, {{link|Maths functions and properties||Maths}}, {{link|Object functions and properties||Object}}, {{link|Overlay functions and properties||Overlay}}, {{link|Parser functions||Parser}}, {{link|Region functions and properties||Region}}, {{link|Room functions||Room}}, {{link|GUI Slider properties||Slider}}, {{link|GUI Text Box functions and properties||TextBox}}, and {{link|ViewFrame functions and properties||ViewFrame}}.
AGS has certain ''managed'' types{{footnote alt parent|4}} that you can not create an instance (variable declaration) of, but you ''can'' create pointers to{{footnote parent|5}}.  All of the variables of managed types are ''managed'' by AGS.  These include the types: [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags45.htm AudioChannel], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags46.htm AudioClip], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags54.htm Button], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags47.htm Character], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags48.htm DateTime], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags49.htm Dialog], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags50.htm DialogOptionsRenderingInfo], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags51.htm DrawingSurface], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags52.htm DynamicSprite], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags53.htm File], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags54.htm Game], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags55.htm GUI], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags56.htm GUIControl], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags63.htm Hotspot], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags64.htm InventoryItem], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags58.htm InvWindow], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags59.htm Label], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags60.htm ListBox], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags65.htm Maths], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags66.htm Mouse], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags68.htm Object], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags69.htm Overlay], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags71.htm Parser], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags72.htm Region], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags73.htm Room], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags61.htm Slider], [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags62.htm TextBox], and [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags74.htm ViewFrame].


==Working With Managed Types==
==Working With Managed Types==
Line 37: Line 37:
==Array of Pointers==
==Array of Pointers==


It should be noted here that when defining pointers, you can also create an {{link|Script language keywords|Arrays|array}} of pointers. When you create an array you are simply defining a set of variables (or in this case, pointers) which all have the same name. You access each one individually using an index between brackets ([ and ]).
It should be noted here that when defining pointers, you can also create an [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm#Arrays array] of pointers. When you create an array you are simply defining a set of variables (or in this case, pointers) which all have the same name. You access each one individually using an index between brackets ([ and ]).


Defining an array of pointers works the same way as defining any other array does, so to define an array of GUI*s called myguis to hold 5 GUI*s, you would type:
Defining an array of pointers works the same way as defining any other array does, so to define an array of GUI*s called myguis to hold 5 GUI*s, you would type:
Line 44: Line 44:


With arrays you can't assign initial values, and the valid indices are from 0 to the size of the array minus one (in this case, 0 to 4). You treat an array of pointers just like you would ordinary pointers.
With arrays you can't assign initial values, and the valid indices are from 0 to the size of the array minus one (in this case, 0 to 4). You treat an array of pointers just like you would ordinary pointers.
===Dynamic Array of Pointers===
As of AGS 3.0, you can have [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags37.htm#DynamicArrays dynamic arrays] of the built-in types, including the managed types. The assignment here works a little differently:
  GUI *daguis[] = new GUI[5];
Notice that we don't use an asterisk after the '''new''' keyword. Keep that in mind if you plan to use dynamic arrays of pointer types.


=Assigning A Pointer A Value=
=Assigning A Pointer A Value=
Line 100: Line 108:
The String type isn't one of AGS's managed types, nor can you create a pointer to it. So why then am I bringing it up? The fact is, the String type is actually internally defined as a pointer, which is how it is able to have it's virtually infinite maximum length.
The String type isn't one of AGS's managed types, nor can you create a pointer to it. So why then am I bringing it up? The fact is, the String type is actually internally defined as a pointer, which is how it is able to have it's virtually infinite maximum length.


Prior to AGS 2.71, AGS used the now deprecated string type. The string type was internally defined as an {{link|Script language keywords|Arrays|array}} of 200 characters ('''char'''s). This meant that strings had a maximum length of 200 characters themselves.
Prior to AGS 2.71, AGS used the now deprecated string type. The string type was internally defined as an [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm#Arrays array] of 200 characters ('''char'''s). This meant that strings had a maximum length of 200 characters themselves.


With the introduction of AGS 2.71 came the new String type which removed that limit. And how did it do it? It used a pointer. Not an AGS-style pointer, but a pointer nonetheless. In programming languages such as C and C++, a pointer-to-char (char*){{footnote parent|7}} creates a special type of pointer. Instead of just pointing to one single variable, a char* can point to a virtually infinite number of chars in the form of what is known as a string-literal (such as "this is some text").
With the introduction of AGS 2.71 came the new String type which removed that limit. And how did it do it? It used a pointer. Not an AGS-style pointer, but a pointer nonetheless. In programming languages such as C and C++, a pointer-to-char (char*){{footnote parent|7}} creates a special type of pointer. Instead of just pointing to one single variable, a char* can point to a virtually infinite number of chars in the form of what is known as a string-literal (such as "this is some text").
Line 137: Line 145:
This also provides advantages when working with the player's active inventory item.
This also provides advantages when working with the player's active inventory item.


===Player.ActiveInventory===
====Player.ActiveInventory====


In an integral system to access the player character's active inventory, you would have to do something like this:
In an integral system to access the player character's active inventory, you would have to do something like this:
Line 157: Line 165:


===File*===
===File*===
Another example can be seen if we look at the {{link|File functions and properties||File}} type.
Another example can be seen if we look at the [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags53.htm File] type.


In an integral system, you would access an external file like this:
In an integral system, you would access an external file like this:
Line 209: Line 217:
==Extending The Built-In (Managed) Types==
==Extending The Built-In (Managed) Types==


Now that we've seen what pointers are, how they are used, how they relate to AGS, and some basic uses of them, let's take a look at a different kind of usage. In AGS we can create our own custom-defined datatypes using {{link|Script language keywords|struct}}.
Now that we've seen what pointers are, how they are used, how they relate to AGS, and some basic uses of them, let's take a look at a different kind of usage. In AGS we can create our own custom-defined datatypes using [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm#struct struct].


You define a struct like this:
You define a struct like this:
Line 247: Line 255:


You can extend any of the managed types that you can create pointers to in this manner.
You can extend any of the managed types that you can create pointers to in this manner.
====Extending the Character Type for AGS 3.0+====
The above example was originally written and designed around the 2.7x branch of AGS. As of AGS 3.0 however, we have the ability to use extender methods. For this particular example, I would recommend adding global extenders such as Character.GetHealth and Character.SetHealth instead of using a separate structure globally. You could still use the structure inside of your script (perhaps in a different script, where the extenders would actually be defined), but it would make it simpler to integrate your extensions into existing scripts by using extenders instead. Check them out if you're using a 3.x version of AGS!
==Dynamic Arrays Are Pointers Too==
In addition to the managed types, there are another type of pointer you should be aware of: [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags37.htm#DynamicArrays dynamic arrays]. You can create a dynamic array of the base types (such as int) or of pointers to a managed type (such as Character*). For creating a dynamic array of pointers, see {{link||Dynamic Array of Pointers}}.
Unlike the managed types, you use the '''new''' keyword to create a new array dynamically. The name you give it is treated as a pointer. The manual gives us this example:
  int characterHealth[];
  characterHealth = new int[Game.CharacterCount];
Initially, ''characterHealth'', just as with other pointers, holds the value of '''null'''. When you assign its value on the second line, you are telling it, as with other pointers, to point to the array that you've newly created.
This is particularly important if you pass a dynamic array as a function parameter. If you change the value of a dynamic array passed as a function parameter, it will change the value of the array itself. Keep in mind that the parameter is pointing to the same array as what you passed into the function. Very useful if it's what you want, but it can be confusing if you're not aware why it's happening.


=Closing=
=Closing=


So you came to me with questions, and I hope I've answered some of them at least. In any case I hope I answered the ones you had about pointers and their usage in AGS. If you have any questions or comments you can {{forumPM|ags_id=2015|name=me}} on the AGS forums, or email me at {{email|monkey.05.06@gmail.com}} any time. Thanks for reading my article, and I hope you've enjoyed it as much as I enjoyed writing it.
So you came to me with questions, and I hope I've answered some of them at least. In any case I hope I answered the ones you had about pointers and their usage in AGS. If you have any questions or comments you can {{forumPM|2015|me}} on the AGS forums, or email me at {{email|monkey.05.06@gmail.com}} any time. Thanks for reading my article, and I hope you've enjoyed it as much as I enjoyed writing it.


monkey_05_06
monkey_05_06
Line 259: Line 284:
{{footnote text|2|The length for Strings is limited by your computer's physical memory. A String will take up 4 bytes of memory, plus 1 byte for each character it contains.}}
{{footnote text|2|The length for Strings is limited by your computer's physical memory. A String will take up 4 bytes of memory, plus 1 byte for each character it contains.}}


{{footnote text|3|Floating-point decimals won't always evaluate as you might expect when doing certain mathematical operations (this is due to their precision levels). See the manual entry on {{link|Script language keywords|Data types|data types}} for more information.}}
{{footnote text|3|Floating-point decimals won't always evaluate as you might expect when doing certain mathematical operations (this is due to their precision levels). See the manual entry on [https://www.adventuregamestudio.co.uk/manual/index.html?page=ags44.htm#DataTypes data types] for more information.}}


{{footnote text|4|AGS's managed types are those listed here. You cannot create a new managed type within AGS's scripts; to create a new managed type you would need to write a plugin. Some module writers may use the keyword ''managed'' to prevent users from creating instances of structs that are meant to be used statically. This does not however make the type managed. Only AGS's built-in managed types and any managed types created via plugins are actually managed, and therefore are the only types that can have pointers to them.}}
{{footnote text|4|AGS's managed types are those listed here. You cannot create a new managed type within AGS's scripts; to create a new managed type you would need to write a plugin. Some module writers may use the keyword ''managed'' to prevent users from creating instances of structs that are meant to be used statically. This does not however make the type managed. Only AGS's built-in managed types and any managed types created via plugins are actually managed, and therefore are the only types that can have pointers to them.}}

Navigation menu