Eyeon:Script/Reference/Language/Variables

From VFXPedia

< Eyeon:Script | Reference | Language
Revision as of 01:37, 10 May 2007 by Daniel (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Variables

Introduction to Variables

The idea of variables are universal to almost all programming languages. A variable is essentially a place holder for information stored in memory. A value is assigned to a variable, so that the value can be accessed later on in the software. In essence, a variable is the scripts short term memory.

In this rather simple example of a variable we assign the number '1' to the variable 'a'.

a = 1

Some other examples of variable assignment are:

message = "This is a string!"
 
cost  = 49995.95
 
counter = 1 + 1


Valid Names For Variables

Almost any combination of alphanumeric values [a-z, 0-9] can be used in the name of a variable, excepting those which are reserved as keywords by eyeonScript itself. It is also possible to use the underscore (_) character as part of a variables name. No other character is permitted, and variable names cannot contain spaces. A variable's name cannot start with a number, so test1 is a valid variable name while 1test is not.

The following words are reserved, and cannot be used as variable names:

and            
break
do
else
elseif  
end
false
for
function
if              
in      
local
nil
not
or
repeat
return
then
true
until
while

eyeonScript is a case-sensitive language: 'and' is a reserved word, but 'And' and 'AND' are two different, valid names. As a convention, names starting with underscore followed by uppercase letters (such as _INPUT) are reserved for internal variables.

Types

eyeonScript variables are dynamically typed; that is to say that a variable can contain any sort of value. For example, if we have a local variable a, we can assign different types to it at will:

  local a
  a = 1           -- a holds a number
  a = "hello"     -- a now holds a string
  a = { 1,2,3 }   -- and now a table 

There are eight different types of values in eyeonScript, each of which represents a different form of data. All of the types are listed below accompanied by a short description.

  • nil

a special type used to indicate an empty or uninitialized value.

  • string

any sequence of alphanumeric characters, and associated special codes such as line feeds and end-of-file characters.

  • number

any numeric value which can be used in a mathematical equation.

  • boolean

a true or false value.

  • function

a function is a chunk of script packaged to make it easy to use over and over again. eyeonScript allows functions to be assigned to variables. See Functions.

  • table

a table is a list of index keys and values. The index key is any numeric or string identifier. The value associated with that index can be any type of value, even another table. If it can be assigned to a variable, it can be stored as a value in a table.

  • userdata

this datatype usually contains information and objects from Fusion. The Fusion object, Composition objects and Opterator objects are all examples of userdata types.

  • light userdata

a special type used internally by eyeonScript, never directly manipulated.


Variable Assignment

As seen in the examples above, a variable is assigned a value with an equals (=) sign. The name of the variable is on the left of the equals, and the value to be assigned is on the right.

An interesting feature of variables in eyeonScript is the ability to assign more than one variable at a time, as shown below.

variable_1, variable_2, variable_3 = 1, 2, "test"

The first value will be assigned to the first variable, the second value to the second variable, and so on. Each entry is separated by a comma.

If there are more variables that values, the extra variables will have a value of nil. If there are more values than variables, the extra values will be discarded.

It is possible to assign a variable to another variable, as shown below

variable_1 = 15
variable_2 = variable_1
 
print( variable_2 )
 
15

In the above example variable_2 would be assigned a copy of the value of variable_1. Any changes made to variable_1's value would not affect variable_2, and changes to variable_2 would not affect variable_1.

Variable Scope

The scope of a variable refers to how long a variable remains active before eyeonScript reclaims the memory used by that variable for other purposes. The scope also affects what portions of a script can read and change a variables value. There are two types of variable scope, Global and Local.

A Global variable is a variable which exists for the entire time the script runs. Once a global variable is defined it exists in memory until the variable is assigned the value nil, or until the script exits, whichever happens first. The value of a Global variable can be read and modified from anywhere in a script. It is not necessary to declare a variable as a global variable, as this is the default for newly created variables.

Local variables must be declared as local when they are created using the 'local’ keyword - for example local x = 100. A local variable cannot be accessed outside of the chunk where it is created. For example, a local variable declared in the main body of a script would not be visible from within a function, or inside a for loop.

function test_local()
    local var1 = 100
 
    print(var1)
end
 
test_local()
 
print(var1)

The output from this example would be

100
 
nil

If a local variable is declared with the same name as a global variable, the local variable 'shadows' the global until the local variable goes out of scope. In the following example we create two instances of the function x, and print its value at different stages to demonstrate the concept of scope.

x = "I am global x"
 
-- we use do and end to force a chunk
-- any statement that terminates with end is a chunk
-- i.e. for loops, do while loops and function calls
 
do  
 print(1, x)
 
 local x = "I am local x"
 
 print(2, x)
end
 
print(3, x)

The output from the above example would be:

1 I am global x
 
2 I am local x
 
3 I am global x

Chunks

A chunk is any section of code which terminates with the end command. For example, a function is considered a chunk because it starts with function x() and terminates with an end statement. For loops and do loops also qualify as chunks. All code between the start and end statements is considered part of the same chunk. Chunks are important because the local variables within a chunk are recycled after the end statement.

You can force a chunk without calling a function or loop statement by calling do...end, as in the following example.

-- this entire script is considered a chunk
 
print("main body of script")
 
for i = 1, 10 do
  -- the code between the do and end statement here 
  -- is considered part of the same chunk
 
  print("This is pass "..i.." through the loop")
end
 
do 
  print("This is a third chunk in the script")
end

When you run a script using the Scripts menu in Fusion, through the Tool context menu, or using the require(), RunScript(), dofile() or dostring() functions the script runs within a chunk of its own.

Persistent Global variables

Global variables can also be stored in a globals table (to get information about the environment, see getfenv()) that will be stored persistently after a script is run. It cannot be passed between compositions, nor can it be a persistent after Fusion is closed. Essentially all composition and tool scripts run inside another larger environment -- the one that the console uses. To add a variable to the globals table from a composition or tool script, use the following syntax:

globals.hello = "hi"

If you now go into your console window and print the hello variable, it will now return the value "hi". This is useful for passing variables between scripts that store information that cannot be saved by the SetData function (custom event suites, userdata).


Tips for Variables (edit)

EyeonTips:Script/Reference/Language/Variables