Multiplayer Interactive-Fiction Game-Design Blog



Download 8.87 Mb.
Page103/151
Date02.02.2017
Size8.87 Mb.
#15199
1   ...   99   100   101   102   103   104   105   106   ...   151

Flow of Control


Describes flow-of-control commands.
MIFL supports several different ways to control program flow,

including if-then-else, while, do-while, for, and switch statements.

I won't go into detail because the constructs are standard to

all languages.


Debug
While technically not a flow-of-control statement, "Debug" fits

best in this category. When a "Debug" statement is encountered,



and the program is being run with debugging enabled,

the debuggin user interface will be brought up so the user

can step through the code.

In C or C++ this is similar to placing an Assert() in the code

and causing the development environment to bring up the debugger.


Debug;

From within the for() loop you can use the

following:





  • Break; - Exits out of the statement.





  • Continue; - Exits out of the statement, runs

    the increment expression, and

    returns to evaluating the conditional expression.






For
The for(;;) {}; loop works the same as C, C++, Java, and ActionScript:


for (init expression,

&tab;conditional expression,


&tab;increment expression)


&tab;statement;




From within the for() loop you can use the

following:





  • Break; - Exits out of the statement.





  • Continue; - Exits out of the statement, runs

    the increment expression, and

    returns to evaluating the conditional expression.





If, then, else
You can use the if-then-else statement to test conditionals.

The format is the same as C, C++, Java, and ActionScript:




if (expression)

&tab;statement;








if (expression)

&tab;if true statement;




else

&tab;if false statement;




You can also use the ? : operator, as in

other languages.




expression ?

&tab;if true statement




:

&tab;if false statement;




Return
Return works the same as C, C++, Java, and ActionScript.

Calling return without an expression will

return Undefined.


return expression;






return;


Switch
The switch(){}; statement is the same as C, C++, Java, and ActionScript:

Unlike C and C++, the switch() statement can compare strings, objects,

and any other data type, not just numeric values.


switch (expression) {


case expression 1:

&tab;statement;


&tab;break;


case expression 2:

&tab;statement;


&tab;break;


default:

&tab;statement;


&tab;break;




};

From within the switch() statement loops you can use the

following:





  • Break; - Exits out of the statement.




While and do-while
The while() {}; statement and do {} while (); statements

allow for looping.

The format is the same as C, C++, Java, and ActionScript:


while (expression)

&tab;statement;








do

&tab;if true statement;




while (expression)

From within the while() and do()-while loops you can use the

following:





  • Break; - Exits out of the statement.





  • Continue; - Exits out of the statement and

    returns to evaluating the expression.






    1. Objects – Basics


Tutorial on how to define and create objects.


MIFL objects vs. other languages
MIFL is an object oriented language. It supports objects that are

similar, but not exactly the same as those in C++, Java, and

ActionScript.

The basic premise of an object is a collection of related data

(called properties)

along with code (called methods) that are used to access or

modify that data.

For example: In an interactive fiction setting, two

objects might be a "Parrot" and a "Penguin". Each would probably

support the properties of "Weight" and "Height", but only

the parrot object would have "FlyingSpeed" properties. They

would both have "Walk" methods, but only the parrot would have

a "Fly" method, and the penguin would have a "Swim" method.

MIFL objects also include:







  • Super-classes - The ability to "inherit" properties

    and methods from other objects. Both "Parrot" and "Penguin" might

    inheret some attributes and methods from the "Bird" super-classs

    (also an "object"). That might in turn inheret properties from

    the "Animal" super-class.





  • Multiple inheritance - Objects can inherit properties

    and methods from more than one superclass. For example: The parrot

    might not only inherit from the "Bird" class, but from

    the "TalkingCreature" class. The penguin object might inherit

    from the "Bird" and "SwimmingCreature" class.





  • Private methods and properties - Objects have

    methods and properties that can only be accessed from within

    the object. Usually, private methods and properties are used

    for "safety" reasons to ensure that foreign code doesn't

    call the private method or set a private variable that

    will "mess things up".







  • Public methods and properties - All public methods

    and properties are globally defined. For example: Once the Walk() public

    method is defined, all objects that use the method Walk() must

    conform to the definition, including the number of parameters

    passed in. If they don't, compilation will fail. Conversely,

    C++ and Java allow a different Walk() to exist for each class.







  • Dynamically created methods and properties (called "layers") - MIFL

    allows methods and properties to be added and removed dynamically

    to objects. C++ does not allow this.





  • Timers - In MIFL, timers are associated with objects.

    C and C++ treat timers differently.







  • Containership - In MIFL, an object can be contained

    by an object, and contain its own objects. Containership

    is very useful for interactive fiction. C, C++, Java, and

    ActionScript do not have containership.







  • Blurring of the line between object and class - In

    C++, Java, and ActionScript, a class is a definition for an object,

    and an object is an instance of a class. To create an

    object the programmer must call "new object". MIFL also has this,

    but an class can automatically be created as an object and

    referenced in a global variable of the same name.

    This is useful for interactive fiction.





  • Unique ID - Every MIFL object has an ID

    (128-bit GUID) that uniquely identifies it from all other objects

    created on this or other systems. The unique ID makes it easier

    to write objects to disk and then restore them. It also

    provides protection if a program access an object after it

    has been deleted.








Creating an object/class
Creating an object (or class of objects) is easy:



  1. Select the "Add new object" menu

    item in the "Objects" menu.



  2. In the "Modify object" page that appears,

    type in a "Name" for the object, such as "Parrot".

    It must be unique.





  3. You can also type in descriptions for the object and under

    what help categories it will be placed.



The "Modify object" page has several tabs:







  • Description - You just visited this.





  • Super-classes - This lets you specify the

    super-classes of the object.







  • Properties - The properties page lets you add

    or remove properties to the object.







  • Methods - The methods page lets you add

    or remove methods to the object.







  • Misc. - This page lets you move the object

    to a different library, and other settings.






Making it an object
MIFL makes it easy to automatically create an object from

a class:




  1. Click on the "Super-classes" tab.





  2. Check "Automatically create as an object".




  3. In future, if you want this object to be created within

    another object then select the other object from

    the drop-down immediately below the check-box.

    You could use this for interactive fiction to create objects

    that are contained with or held by other objects, such as

    a "Peanut" being held by the "Parrot" object.

    At the

    moment you can't do this because you only have one object.



    You can also change the ID object (underneath the super-class).

    In general, you only need to do this if you want to make sure

    that an object whose definition you deleted and then recreated

    will still use the same ID. This doesn't happen too often.


If you didn't check the "Automatically create as an object" button

then your definition would be more of a class (in C++ and Java

terms).

To test that your object actually works:





  1. "Compile" your project.



  2. Switch to the "Test compiled code" page.





  3. Click on the "Objects..." link on the right

    side of the page.


  4. This will expand to list all the objects (not classes).
  5. Click on the "Parrot" object.




  6. Underneath the "Parrot" object will be sub-tables

    for properties, methods, and timers supported by the

    object. (Right now they should be empty.) You can also hover

    your mouse over the "Contains" and "Layers" entries to

    see what objects your object contains (or is contained by),

    and what layers it has. Right now there won't be anything

    in the contains list, and only one layer, "Parrot".




Adding properties
To add public properties to your object, you must first

define a public property. To do this:





  1. Select the "Add new property" menu

    item under "Property defs".



  2. In the "Property definition" page, type in

    a "Name" for your property, such

    as "FlyingSpeed".




  3. Note: If you leave the property

    value blank then the property

    will not be added to the object. However,

    if the property is inhereted from a superclass (discussed

    later) then that property will be used.

  4. You can also type in descriptions and help

    categories so that other authors will know what the

    "FlyingSpeed" property means.





Now that you have created the public property definition,

you can add the public property to your Parrot object:



  1. Select the "Parrot" menu item

    from the "Objects" menu.



  2. Click on the "Properties" tab.





  3. Under the "Property name" column, select

    the "FlyingSpeed" property from the drop-down

    list box.




  4. Once you have selected the property the description will

    automatically be filled in. You cannot modify the description

    of a public property from the properties page.

  5. To have the property automatically default to a value, type

    one in under "Initial value". In C++ you would set the value

    in the object's constructor. You can do the same under MIFL,

    but setting it in the GUI is easier and better when you're

    trying to save the object to disk (since MIFL then knows if

    the property has changed from its initial value; MIFL doesn't

    save the property unless it has changed).





  6. You can test the property out by doing a Compile and

    then running Test compiled code. You can either

    see the property in the right-hand column, or

    bold type Parrot.FlyingSpeed in the watch list.


  7. Tip: If you change the "Scope" of the watch list to

    "Parrot" then you only need to type in "FlyingSpeed".


You can add a private property in a similar manner:





  1. Select "Parrot" from the "Objects" menu.





  2. Switch to the "Properties" tab.





  3. Under the "Properties, private" section, type in

    a "Name" for your private property. (Remember,

    for a public property you selected from the public property

    list.)



  4. Set a "Initial value" just like the public property.





  5. Type in a "Description" of the private property.

    This doesn't appear in help, but it's always good to document

    what things mean.





  6. You can view the private property the same as a public

    property. Be aware that "Lantern.YourPrivateProperty"

    will not work unless you set the watch

    "Scope" to "Lantern". Private properties can only be accessed

    from within the object/class.







Adding methods
To add a public method to your object, you must first

define a public method. To do this:





  1. Select the "Add new method" menu

    item under "Method defs".



  2. In the "Modify method definition" page, type in

    a "Name" for your property, such

    as "Fly".





  3. You can also type in descriptions and help

    categories so that other authors will know what the

    "Fly" method does.





  4. The "Method method definition" page is just like the

    "Modify function" page that you familiarized yourself

    with when you created HelloWorld(). The only difference

    is that it doesn't provide a tab for

    modifying the code, since a method definition does not include

    code; that's specific to an object.



Now that you have created the public method definition,

you can add the public method to your Parrot object:



  1. Select the "Parrot" menu item

    from the "Objects" menu.



  2. Click on the "Methods" tab.





  3. At the "Methods, public" section, select

    the "Fly" method from the drop-down list box.



  4. Press the "Add a new public method" button just

    above it. This will add the public method that you have selected

    from the list box.




  5. This switches to the "Modify method" page. Again it's

    just like the "Modify function" page, except that

    while you can modify the code, you cannot change the

    method's name, description, or parameters since these are

    set by the method definition.

  6. Switch to the "Code" tab and type in some

    code to run so you can see the method working:


  7. Trace ("Fly() method called");


    return FlyingSpeed;





  8. You can test the property out by doing a Compile and

    then running Test compiled code.



  9. Type "Parrot.Fly()" in the edit field and

    press "Run this" to execute the code.


  10. You'll see the Trace() in the Debug Trace display, along

    with the value returned from Parrot.Fly(), which should be

    the value of FlyingSpeed.

You can add a private property in a similar manner:





  1. Select "Parrot" from the "Objects" menu.





  2. Switch to the "Methods" tab.





  3. Under the "Methods, private" section, press

    the "Add a new private method" button.


  4. This switches to the "Modify method" page. Again it's

    just like the "Modify function" page. Unlike the public

    method modification, you can change everything, including

    the name, decription, parameters, and code.


  5. In the "Description" page type in

    a unique "Name" for the method.



  6. Switch to the "Code" tab and type in some

    code to run so you can see the method working.



  7. You can verify that the private method works the same

    way you tested the public method. However, you will

    need to set the watch variable's "Scope" to "Parrot"

    in order to access the private method.






Dynamically creating and deleting objects
You can also dynamically create and destroy objects:





  1. Compile the project and switch

    to the Test compiled code page.





  2. In the top edit field, type "MyGlobal = new Parrot" and

    press "Run this".


  3. Note: This assumes that you kept your test global variable,

    "MyGlobal", in the code. If not, you'll need to add it.


  4. A second parrot will have been added. You can see it

    by clicking on "Objects..." in the right

    side of the "Test compiled code" page. The objects list will

    have two parrots, one named "Parrot" and another named

    "Parrot" with lots of numbers next to it.




  5. The parrot with all the numbers next to its name is the one

    you created. The numbers are the new parrot object's ID.

    Objects that are automatically created (such as the original

    Parrot) do not show this number.


  6. To delete the newly created parrot object, type "delete MyGlobal" and

    press "Run this".


  7. Note: You're not deleting the global variable "MyGlobal", but

    the object referenced by the variable. There are ways to delete

    global variables, but they won't be covered in the tutorial.




    1. Download 8.87 Mb.

      Share with your friends:
1   ...   99   100   101   102   103   104   105   106   ...   151




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

    Main page