Multiplayer Interactive-Fiction Game-Design Blog



Download 8.87 Mb.
Page100/151
Date02.02.2017
Size8.87 Mb.
#15199
1   ...   96   97   98   99   100   101   102   103   ...   151

Variables and operations


Discusses how variables and operations work.


Variables
MIFL variables behave almost exactly like those in Flash's

ActionScript because they are "un-typed".

They are very different to variables in Java, C, and C++,

which are "typed".


In MIFL, a variale it a storage slot for data that can

store any type of data. MIFL handles several

data types. The important ones are:







  • Number - This is a floating point number.

    For those of you familiar with C, it's a "double".

    (Example: 123.435)





  • Boolean - This can either

    be "True" or "False".







  • Character - A single character, such as

    'a' or '!'. Characters are identified by the

    single-quotes (') surrounding it.





  • Null - This can only be one

    value, Null. It represents an empty value.







  • Undefined - This can only be one

    value, Undefined. Undefined implies that the

    value is not only empty, but it wasn't even initialized.





  • String - A series of characters.

    Strings are identified by the quotes (") surrounding them.

    (Example: "Hello world!")





  • List - A list if an array of variables.

    Lists are identified by the brackets ([ ]) that surround it.

    (Example: [1, 3, "hi there", true] is a list with the 1st

    element contain the number 1. 2nd is 3. 3rd is the

    string "hi there", and 4th is the boolean, true.





  • Object - This is reference to an object.

    I'll explain this in detail when I get into objects.







  • Function - A reference to a function.

    In C or C++ this is equivalent to a pointer to a function.







  • Method - A reference to a method within an object.

    In C or C++ this is equivalent to a pointer to a method.







  • String table entry - I'll discuss this later.





  • Resource - I'll discuss this later.



Unlike C, C++, and Java, a variable can change data types at

run-time. So, even if "x" were initialized to 54.45, it

could be changed to "Hi there" later one.



Using variables in functions
Creating a variable in a function is easy. In your

HelloWord() code, just add the following line:


var x = 54.3534;


Trace (x);




If you then Run this the "Debug trace" will

also display "54.3534".


(Actually, it probably won't do this unless you first

run "Compile" under "Misc." again.)

In C or C++ you would have typed in "double x = 54.3534" or

"int x = 53" or "char x[] = "hello there!"" because variables

are "typed". You don't need to do this in MIFL. It's

just "var x = 54.3534", "var x = 53", or "var x = "hello there!"".

As with C, C++, Java, and ActionScript, you can also create

several variables at once using "var x,y,z;" or

"var x=43, y="hi there", z=true;".




Note: All names (including variables)

are case insensative in MIFL. That means that

varible "x" is equivalent to "X", and functon "HelloWorld"

is equivalent to "helloWORLD". C, C++, and Java work

just the opposite, being case sensative, and differentiating

between HelloWorld() and helloWORLD().



Operators
Once you have variables, you'll want to do operations on them,

such as addition and assignment. The operators in MIFL are

almost identical to C, C++, Java, and ActionScript. They are (in

order of precedence):



























































































































































































. Object property or method access, like "object.property".
[ ] Creating a list or indexing into a list or string.
( ) Parenthesis or parameters for a function call.
++ --

Increment or decrement a variable. If ++ (or --) is to the right of

the variable the variable's value is put on the stack and then incremented/decremented.

If ++ (or --) is to the left, the variable is first incremented or

decremented, and then the variable's value is put on the stack.

- Negation, such as in "-(x+y)"
~ Bitwise not.
! Logical not.
* / % Multiply, divide, and modulo.
+ - Addition and subtraction.
<< >> Bitwise shift left or right.
< <= > >= Less than, less than or equal, greater than, greater than or equal
== != Equality or not equality
=== !==

Strict equality or strict not equality. These do not appear

in C, C++, or Java. They test equality but are strict about it.

Because of automatic conversions between numbers and strings,

43 == "43" but 43 !== "43", although 43 === 43. Strict equality does not

do automatic conversions.



& Bitwise AND
^ Bitwise XOR
| Bitwise OR
&& Logical AND
|| Logical OR
? :

Contional. Example: "(1 < 2) ? 5 : 3" will return "5".



= += -= *= /= %= <<= >>= &= ^= |= Various forms of assignment
, Comma. Separates parameters, list elements, and (sometimes) lines of code.

You can combine variables and operators to make your

HelloWorld() function do some calculations. Try tying

in and then running:


var x = 18;


var y = 6;


var z = x * y + 89;


z *= 2;

Trace (z);




Debugging
MIFL includes a debugger that allows you to step through the

code and see the flow of control along what variables

are at each step of the execution.

To test this:







  1. Type in the code (as per above).



  2. Select "Compile" underneath

    the "Misc." menu.



  3. Select "Test compiled code" underneath

    the "Misc." menu.



  4. In the "Test compiled code" page, check

    the "Debug" checkbox.



  5. Type in "HelloWorld()" and

    press "Run this".



  6. The "Debug" page will be displayed with "HelloWorld()" being

    highlighted. Press "Step in" to step in.


  7. The Debug page will not display the code from the HelloWorld()

    function.


  8. To see the values of the variables, x, y, and z, type

    in "x", "y", and "z" (each on their own line)

    into the "Watch variables" list. At first each will

    be "undefined" since they haven't been set.



  9. Press "Step" to step to the next line of code.

    If, for example, you stepped over the "x = 18;" line then

    you'll see the watched variable, x, change to "18".





  10. Keep on pressing "Step" until the code finishes

    executing. You can watch how the variables change with each

    step.



  11. If your function called another function (other than Trace()),

    you could press "Step in" to debug that function.

    (It's not worth stepping into Trace() because it just ends up

    calling a built-in function, which can't be debugged.)

    You can also press "Run" to run the code without

    stepping, or "Exit" to stop running the code.



Passing parameters to functions
Just like other languages, you can pass parameters to functions.

To do this:





  1. Select "HelloWorld" under

    the "Functions" menu.



  2. Press the "Parameters" tab in the "Modify

    function" page.



  3. Type in a parameter name, "Input" and

    a description for the meaning of the parameter.


  4. If you want more than one parameter, just

    press "Add more parameter slots" to add another

    slot. When you fill that one in, you can press "Add more

    parameter slots" again to add more.


  5. Type in a short desciption for the Return value of

    HelloWorld(), such as "Does complicated math on Input and then

    returns that."





  6. Switch to the "Code" tab.







  7. Type in the following code:




  8. var x = 18 + Input;


    var y = 6;


    var z = x * y + 89;


    z *= 2;

    Trace (z);

    return z;








  9. Compile the project.



  10. Select the "Test compiled code" menu item.





  11. Type in "HelloWorld (53)" into the edit field,

    and then press "Run this".


  12. If you have debuggin turned on you'll be able to watch the

    value of "Input", in addition to that of the variables,

    "x", "y", and "z".

    If you look at the debug log, you'll see that the Trace(z)

    outputs "1030" and then the return value is printed out

    as "Returns: 1030".


In addition to the parameters you add to the function, MIFL

functions (and methods) support two "hidden" parameters:





  • Arguments - This is a list of all the parameters

    passed into the function. In the case of the HelloWorld()

    example it would be [53]. If more parameters were passed in

    the list would have more elements.







  • This - When you run HelloWorld(), This will be

    set to Undefined. However, if HelloWorld() were

    a method in an object, This would contain a reference to the

    object. C++, Java, and ActionScript have a similar construct.






Global variables
Like other languages, MIFL supports "global variables" which

are variables that can be accessed by any function or method, and

which persist from one function call to the next.

To create a global variable:





  1. Select the "Add new variable" menu item

    under the "Variables (global)" menu item.



  2. In the "Modify global variable" page that appears, type

    in a "Name" for the variable, such as "MyGlobal".



  3. Under "Initialized to", type in the value you'd

    like the global to being with, such as 54.456. If you leave

    this blank the global will initially be set to Undefined.




  4. You can also type in a short and long description, just like

    with the function.


  5. Re-Compile the project and return

    to Test compiled code.





  6. Type "MyGlobal" into the watch list to see

    it's value, which should be 54.456.




  7. You can also see the value of MyGlobal by clicking

    on the "Global variables..." link on the right

    side of the test window. This will display the list of all

    global variables. Hovering the mouse over the "MyGlobal" entry

    will show it's value in a tooltip.

  8. You can change the value of MyGlobal by writing some code

    into HelloWorld(), and then compiling and running it, or

    by typing "MyGlobal = 12345" in the test

    edit window and pressing "Run this".





Warning: A global variable (or object property)

can initialize itself (with "initialized to") to use the contents of another global

variable. This has two potential problems:





  • Undefined - If the global variable that's being

    referenced has not yet been initialized due to the

    semi-arbitrary initialization errors of globals, then the

    global variable will be undefined. This problem

    won't happen when initializing an object's property to a global

    variable because globals are always initialized first.







  • Fracturing - If a global variable or property is

    initialized by referencing a global variable which is in turn

    initialized by a string or list, then the new global variable

    or property will contain the correct string/list but the

    string/list will be different pieces of data.


  • Hence, if

    global variable A is initialized to "Hello", and global variable

    B is initialized to A, then global variable

    B will also be initialized to "Hello".

    However, If A.StringAppend(" there") is called, A will

    be "Hello there", but B will still be "Hello".





    1. Download 8.87 Mb.

      Share with your friends:
1   ...   96   97   98   99   100   101   102   103   ...   151




The database is protected by copyright ©ininet.org 2024
send message

    Main page