Basic variables
Strings
Integers
Floats
Scope
Local
Global
Shared
Promotion and Lack Thereof
Fields
Subfields
Lists
Lists are, at their heart, space-separated words in a sentence. This paragraph is nothing more than a list. Each word is a word or item in the list. The first item in a list is number 1, the second is 2, and so on. Words (or Items, the two names are synonymous) are separated by 1 or more spaces... the number of spaces does not matter, which means there are no 'empty' words.
For example,
cats dogs birds
is only 3 words, with word 3 being "birds".
Examples:
- set fruit apples oranges grapes bananas
- Puts the text into a variable. It is a string, and since it is space separated, is a list too!
- if (%fruit.3% == "grapes")
- If word (item) 3 of 'fruit' is "grapes"... this is true, since it is "apples oranges grapes ..."
- if (%fruit.word(2)% == "oranges")
- If word (item) 2 of 'fruit' is "oranges"... this is true, since it is "apples oranges grapes ..."
- set fruit %fruit.extract(1)%
- Sets fruit to be the same as fruit with the first item removed. The result will be
- "oranges grapes bananas"
- set numberOfFruit %fruit.count%
- count will return the number of words contained in the variable fruit
- set appleWord %fruit.find("apples")%
- find() will return the number of the item that matches "apples" in fruit, or 0 if it could not find "apples".
- pushfront fruit pomegranites
- Adds "pomegranites" to the front of fruit
- You pass the NAMES of the variables, do not use % %
- popfront fruit firstFruit
- Takes the first itemoff of fruit, and puts it in firstFruit. fruit is modified
- You pass the NAMES of the variables, do not use % %
- the second argument is optional if you don't need the first item
- pushback fruit peaches
- Adds "peaches" to the end of fruit
- You pass the NAMES of the variables, do not use % %
- popback fruit lastFruit
- Takes the last itemoff of fruit, and puts it in lastFruit. fruit is modified
- You pass the NAMES of the variables, do not use % %
- the second argument is optional if you don't need the last item
- foreach fruit edible
- For every item in fruit, run the loop of code between foreach and done, with the item in edible
- You pass the NAMES of the variables, do not use % %
- WARNING foreach destroys the list as it loops over it, so do this on a COPY.
Context
'Context' can be one of the most difficult concepts to grasp, for variables. Context is a way to have different data stored in the same variable, accessed by the 'context' in which the variable is used. The most useful type of context tends to come from the
%actor.id%, in which you can use the same variable to store different data for each actor (player or mob).
variable{ context }
Context must be a number. If no context is present (for example, the usual way a variable is used,
%variable%), the context is 0. If you try to use a variable (other than set or unset) with a context, and the variable with that context hasn't been set, it will return the default (0) context value rather than 0.
You may unset all the different contexts of a variable using
unset variable{*}
Examples:
- set running{%actor.id%} 1
- if (%running{%actor.id%}%)
Records
Records are a form of data storage, for storing more complex information than a variable can hold. A record may contain 1 or more named fields, each with it's own value. A record's fields are accessed just like a field in a reference, except that the fields can be changed, and can themselves contain records and lists.
Records are
human readable. That is, they are encoded in a format that a human being can read, comprehend, and write.
However, it is highly advised that the
only time you try to manipulate a record by hand is to either outright 'set' a record up from pre-entered data, via 'set' or via a pre-set variable on an object or mob.
Examples:
- set deck.cardcount 52
- if (%deck.cardcount% < 52)
Special Notes:
- Records are themselves individual items in a list; if no space separates 2 records or a record and a non-record, they are still 2 separate items!
- Record fields can contain another record, a list, or a list of items including records!
- Record fields MUST be named
- The name may not contain spaces and may not be a number, although it may CONTAIN digits.
- "52" is not valid.
- "threein3" is valid.
- Duplicate fields can only occur in hand-written records, and in that case the second field will be inaccessible, UNLESS the first is unset, then the second will still exist. However, they still aren't a good idea, and doing something like this crosses the line into 'extremely creative use'...
- Records with a lot of data (many lists/fields/subrecords) can be a potential slowdown, or even run the risk of crashing the system if they grow to too long (same can happen with lists and other variables, but if you use extremely complex/high data content records it might happen more easily). HOWEVER do not let this scare you off of records! I will gladly adapt the system if we need bigger buffers.
The human readable format of a record is:
[#field:value##field2:value##field3:value#]
A complex record might look something like:
[#field:item1 item2 item3 [#item4:value#]##field2:[#field1:value#][#field1:value#]item3 item4##
field3:value#]
Special Set
Set is a very powerful command now, allowing you to put values into variables, put values into variables on other objects, and even create or alter fields in Records. Eval functions the exact same way, but evaluates the value as a mathematical expression.
Set now allows you to:
- Set local, global, or shared variables
- Set global variables on other entities
- Create or alter fields of records
- Alter items in lists
Usage
set <variable> <value>
<Variable> can be a simple name or a series of field names and indices (numbers). It can be PREFIXED by a reference or room number.
Examples:
- set race Predator
- Puts "Predator" into the variable race
- set race.3 Marine
- Changes the third word of race to "Marine", or adds "Marine" to the end of race if race is less than 2 words in length.
- set %actor%.points 34
- Puts "34" into the global variable points on the entity referenced by actor
- set %actor%.enemies.%index%.enemy %victim%
- Changes the field enemy of %index% word enemies, as a record, on the entity actor to %victim%
- If enemies.%index% is not a record, it is replaced by a new record with the single field enemy
- If enemies.%index% is a record, but does not have the field enemy, the field is added
- If enemies has fewer than index words, a new record will be added at the end
Note that, while you can retrieve words in a list with the var.word() format, you cannot do the same when specifying the item in a list to change via set. Instead you put numbers or variables in like in the 2nd and 4th example above. ALSO, you can actually specify the field this way, if you want to be REALLY creative and confuse yourself!
unset works similiarly, and allows removing a field of a record, removing an item in a list, and removing global variables from other objects.
--
FearItself - 26 Jan 2005