Mappers' Corner Mappers and Modders

Go Back   Omnip)o(tentS Forums > SERVERS > Mappers' Corner
Reply
 
Thread Tools Display Modes
Old 10-04-2007, 01:38 PM   #1
Optimus_P-Fat
Insider

 
Optimus_P-Fat's Avatar
 
Join Date: Jan 2006
Location: New Jersey
Posts: 3,633
UT2k4 UScript Tutorials 201: Advanced Variables (modifiers and arrays)
Reply With Quote


Now that you've learned how to declare and use a simple variable in the 101 tutorial, this tutorial will explain some of the most common values that you can use for the <modifiers> during declaration, and then explain another 'advanced' concept for variables: arrays.



Common Variable Modifiers
This is a list of commonly used variable modifiers and what they do. A more complete list (for some of the more obscure modifiers) can be found at:
http://wiki.beyondunreal.com/wiki/Variable_Syntax


config - this allows the variable to be saved to the ini file when someone calls the SaveConfig() function on the object that contains the variable. This is class specific, and will only save and load the value for the specific class you have the variable defined in. That means that if you extend a child class from it, the child class will NOT use the same values persisted for the parent one when save/loaded.

globalconfig - this is the same as config, but will share these values for all child classes.

editconst - this means that the variable will show up in UEd, but will not be changeable

localized - this means that the default value for this variable will be in a localization file (.int) - this is for having the ability to support multiple languages. For example, if you have a string variable, and you want to be able to support spanish and english, you can declare it localized, and then create separate .int files that have the correct default value in spanish and english.

private - this variable cannot be seen by any class (even child classes) except for the class it was originally declared in. This is how you "turn off" inheritance (see tutorial 102 on objects) access for a variable. (Note: the variable still exists, it just cant be changed or read by anything other than the original class's functions)

protected - this variable is only visible to the class it was declared in and all child classes. I.E. an object external to that class chain cannot directly access this variable, but a class that is extended from the class it was declared in would still be able to access it. This can basically be thought of as a "selective" inheritance, which would work the same as private, except it would allow the child classes to still access the variable.

travel - this variable will keep whatever value it had between map switches (i.e. it will not be reinitialized to its default value)
__________________

http://www.p-fat.net/
Optimus_P-Fat is offline  
Old 10-04-2007, 02:25 PM   #2
Optimus_P-Fat
Insider

 
Optimus_P-Fat's Avatar
 
Join Date: Jan 2006
Location: New Jersey
Posts: 3,633
Default
Reply With Quote


Arrays
In programming in general, there is often used a concept called an "array". An Array is, in concept, nothing more than a group of items. More specifically in many programming languages, it is a group of variables that all have the same TYPE. In some advanced languages, it also guarantees that the variables are all in contiguous blocks of memory (that is, in memory, they are all right next to each other), but for the case of UnrealScript this is irrelevant.

An array is normally accessed by INDEX, starting at 0. That is, if you have an array of 10 variables, and you want to get the value of the first variable in the array, you would ask for item 0. To get the value of the second item, you would ask for item 1, etc. A common term for this type of access (where you ask for n-1 when you want the nth item) is "0-based." Unrealscript arrays are 0-based.

There are also two types of arrays that you can use in UnrealScript. The first type, is sometimes referred to as a "static array", meaning that the array size is fixed from the start and will never change. If you create a static array of 8 strings, there will ALWAYS be 8 and only 8 strings in that array, and you can never add or remove from it.

The second type of array is called a "dynamic" array. These arrays start with zero items in them, and you can add and remove items (and check it's length) in the script itself.

In both cases, you can access values the same way.

Extra important point to note for later use: static arrays are the only arrays that will be replicated if placed in a replication block, which will be discussed in a later tutorial. Dynamic arrays will not be replicated, even if placed inside a replication block!


static arrays

Declaring static arrays is done in the same syntax as declaring regular variables, except you must add the size of the array after the name in square brackets, as such:

Code:
var string MyStringArray[5];
The above statement creates a static array of 5 strings, with the name "MyStringArray".
Accessing an item in the array later in code is also done with square brackets, and the index:

Code:
function string GetFirstArrayItem()
{
	return MyStringArray[0];       
}
You can also request an index by using a variable (of type int), as such:

Code:
function string GetArrayItem(int index)
{
	return MyStringArray[index];
}
One thing you need to check for, especially in the case where you are using a variable, is that the index you are trying to access isn't passed the "end" of the array. I.E. if you have created a variable array of 5 items, you should never ask for index 5 or higher. The only valid indexes are 0,1,2,3, and 4.

One important thing to note: the variables in a static array ALWAYS exist, regardless of if you ever actually "use" the value at a certain index. Declaring an array of five strings, is the same as declaring 5 separate string variables, except that you can now access them via the same name, using an index. So, declaring vary large static arrays will eventually become a memory burden.

Why should one use a static array? Use a static array when you want to are sure you only need a finite list of values. For example: these are used sometimes in some of the message classes, such as the killing spree award strings, since there are only a certain amount of sprees in the game, and no game action could ever "add" a new spree to the game.



dynamic arrays

Declaring dynamic arrays of a variable is slightly different than declaring a static array. To declare a dynamic array, you do not use square brackets in the declaration statement. Instead you use the keyword array before the TYPE of the variable, and then enclose the TYPE of the variable in angle brackets <>. For example:

Code:
 var array<int> MyIntArray;
The statement above declares a dynamic array of ints, with a name of "MyIntArray". At this point, the array has zero items, and for all intensive purposes can be considered to be taking up no memory (obviously there is some memory overhead with the actual array declaration itself, but in concept, you can think of it as taking up no memory). To check the length of the array, you can check the "Length" property using the "." operator, like so:

Code:
function int GetArrayLength()
{
	return MyIntArray.Length;
}
To access a variable in the array, you use the square brackets, the same way you would access a variable in a static array. Although it is extra important to check your index against the length now, because the length can change at any time! The index you may be looking for may have been removed!

To insert items into the array, you use the "Insert" function on the array, using the "." operator. The insert function takes two parameters: the index at which to begin inserting items, and how many items to insert. Inserted items will be inserted with their default value. (0 for numbers, blank string for strings, etc). Example:

Code:
function AddTwoItemsToArray()
{
	MyIntArray.Insert(0, 2);
}
The code above inserts two new items before the item at index 0. This means that when this operation is done, the item that WAS at index 0 is now at index 2, and the two inserted items are at positions 0 and 1.

To remove items from the array, you use the "Remove" function on the array, using the "." operator. The remove function takes two parameters: the index at which to begin removing items, and how many items to remove. Any items that still exist after the removed group are then shifted downward in index to "fill the gap" so to speak. Example:

Code:
function RemoveTwoItemsFromArray()
{
	MyIntArray.Remove(0,2);
}

function ClearAllItemsFromArray()
{
	MyIntArray.Remove(0, MyIntArray.Length);
}
The first function above will remove the first two items from the array. This means that the item at index 2 (if it existed) would "slide down" to index 0, the item at index 3 (again, if it existed) would "slide down" to index 1, and so on.

The second function demonstrates how to pass a variable to the Remove function, in this case, we pass the Length of the array using the "Length" property I explained above, starting at the first item, to tell the Remove function to remove all of the items in the array, clearing it.

Be especially careful to check your array bounds when using dynamic arrays. This means doing you rbest to ensure that you never try to remove, or access, values outside the range (size) of the array. You shouldn't be trying to erase 20 items from an array that only has 5. You shouldn't be trying to access item 17 of an array that only contains 3.

Why use dynamic arrays? Usage of dynamic arrays is mostly done to handle lists of items that come and go during runtime, or that change a lot. The list of banned players, for example, is a dynamic array, since an admin can add or remove the player from the ban list at any time. One would typically use these when you aren't sure how many items you will need right off the bat (i.e. if the array size may be determined by the amount of players in the game, etc).
It is also important to note again, that dynamic arrays will NOT be replicated, even if you tell the code to do so. However, since replication is an advanced topic, we will not discuss it here.
__________________

http://www.p-fat.net/
Optimus_P-Fat is offline  
Reply


Go Back   Omnip)o(tentS Forums > SERVERS > Mappers' Corner

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
UScript tutorials 103: Functions Optimus_P-Fat Mappers' Corner 3 09-30-2007 11:47 PM
UScript tutorials 102: Objects (Classes) Optimus_P-Fat Mappers' Corner 1 09-30-2007 03:01 AM
UScript Tutorials 101: Variables Optimus_P-Fat Mappers' Corner 3 09-29-2007 02:44 AM
UScript question Optimus_P-Fat Mappers' Corner 10 03-10-2007 07:23 AM
Advanced Physics Processors? dr.mindkiller Games and Technology Workshop 12 11-26-2005 01:21 PM


All times are GMT -5. The time now is 01:52 AM.