Objects

[ Back ]

Introduction

Objects are variables that can contain other variables. They work much like arrays, but the fields (elements) of an object needn't be of the same type. You declare an object with a questionmark, and you create and set a field with the . character followed by a field name. Ex:

rem Create an object and add some fields.
my_object?
my_object.an_int = 5
my_object.a_float# = 3.14
my_object.a_string$ = "Hello"

rem Create an object and add a field at the same time.
my_other_object?.a_string$ = "there"

rem Output the fields.
wln "my_object.an_int = ", my_object.an_int
wln "my_object.a_float# = ", my_object.a_float#
wln "my_object.a_string$ = ", my_object.a_string$
wln "my_other_object.a_string$ = ", my_other_object.a_string$

wait keydown

Output:

5
3.140
Hello
there

Object fields are type sensitive. If you've added a string field named a_string$ you need to use the $ character every time you refer to the field. The fields may, however, change type during program execution. Ex:

rem Create and object and add an integer field with the value 5.
obj?.x = 5
wln obj.x

obj.x# = 3.14
wln obj.x#

obj.x$ = "Aha"
wln obj.x$

Output:

5
3.140
Aha

All of this is caused by the fact that object fields are dynamically created, deleted and modified at runtime. The compiler can't know what type a field has, and therefor you need to use # and $ all the way.

Object arrays

You can declare arrays (with any number of dimensions) of objects. But an object can not contain arrays (or other objects). Ex:

rem Create an object array and add some fields to its objects.
an_obj_array?[2]
an_obj_array[0].x = 5
an_obj_array[0].y# = 3.0
an_obj_array[1].x = 10
an_obj_array[1].y# = 6.0

wln an_obj_array[0].x
wln an_obj_array[0].y#
wln an_obj_array[1].x
wln an_obj_array[1].y#

Output:

5
3.000
10
6.000

With procedures and functions

You can pass objects as parameters to procedures and functions, by value or by reference. You can pass an object field, of the correct type, to a sub routine that wants a primtive, both by value and reference. And functions can return objects. In other words, object and their fields work just like normal variables.

Example 1

rem Create two objects.
an_object?.txt$ = "Hello"
another_object?.txt$ = "Hello"

rem Call a procedure that modifies its parameters.
proc ModifyObjects an_object, another_object

rem Write the result.
wln "an_object.txt$ = ", an_object.txt$
wln "another_object.txt$ = ", another_object.txt$

wait keydown

procedure ModifyObjects(obj_by_value?, &obj_by_reference?)
  obj_by_value.txt$ = "This will not have any effect."
  obj_by_reference.txt$ = "This will have effect."
endproc

Output:

an_object.txt$ = Hello
another_object.txt$ = This will have effect.

Example 2

rem Get an object from function.
my_obj? = GetNewObject(10, 4)

rem Output the fields.
wln "my_obj.x = ", my_obj.x
wln "my_obj.y = ", my_obj.y

wait keydown

rem Return an object with two fields, x and y.
function GetNewObject?(x, y)
  obj?.x = x
  obj.y = y
  return obj
endfunc

Output:

my_obj.x = 10
my_obj.y = 4

Example 3

rem Create an array with two objects.
two_objects?[2]
two_objects[0].x = 0
two_objects[1].x = 0

rem Send objects from array to function.
proc ChangeX two_objects[0], two_objects[1]

rem Output result.
wln "two_objects[0].x = ", two_objects[0].x
wln "two_objects[1].x = ", two_objects[1].x

wait keydown

rem Modify x field of a and b, one by value and one by reference.
procedure ChangeX(a?, &b?)
  a.x = 5
  b.x = 5
endproc

Output:

two_objects[0].x = 0
two_objects[1].x = 5

[ Back ]