SCI Programming.com
Welcome, Guest. Please login or register.
October 18, 2017, 01:52:59 AM
Home  >  Tutorials  >  Chapter 10: Getting Familiar With Objects
Chapter 10: Getting Familiar With Objects
About Objects

SCI is an object oriented virtual machine. This means that all of your program code is organized in blocks, called objects. Each object serves it's own unique purpose. Some objects are used for the characters in your game, some for windows, some for buttons, some for menus, some for getting input, and so forth.

The objects in your game are made up two types, classes and instances. Classes are the base objects, and instances are the sub-objects, derived from them. Classes are generally only used for creating object bases. Instances are used for objects you'll be working with.

Classes can be created from scratch, or derived from other classes. Instances, however, must be always derived from a class.

Classes can be accessed globally, while instances can only be directly accessed locally (within the same script). To globally access instances, you would have to load it's script, store it's address in a global variable, and reference it from the variable. This will be discussed more later.

Classes and instances consist of two parts, properties and methods , which are explained below.

Understanding The Ego Object

You can think of your game like a movie. The characters in in it, called "Actors", make up the "cast". Each actor has it's own instance (object). The main character in your game, called "Ego", is derived from the Actor class.

Each Actor object contains it's own properties and methods. The properties are it's characteristics. The methods are it's code blocks.

The Ego object has properties defining it's view, loop, cel, among other things. It has methods to accomplish tasks such as positioning it, setting it's direction, and so on.

The Ego instance is pointed to by the global variable "gEgo". To access it's methods and properties, you use the send command.

There are five main ways to access the properties and methods in objects:
Method #1: Setting An Object's Properties
Here's an example of setting the ego's view loop to 2:
Code:
(send gEgo:loop(2))
  • The send tells the compiler that we want to access an object (a class or instance).
  • The gEgo is the name of the variable we are using to to store a pointer to the ego object.
  • The loop is the name of the property we want to set.
  • In the brackets following "loop", a 2 is put, setting the loop property's value to 2.
  • All send operations are surrounded with a pair of brackets.

Method #2: Obtaining An Object's Properties
Obtaining the ego's loop property is basically the same. You simply don't provide any parameter.
Code:
(send gEgo:loop)
  • The send tells the compiler that we want to access an object (a class or instance).
  • The gEgo is the name of the variable we are using to to store a pointer to the ego object.
  • The loop is the name of the property we want to get.
  • All send operations are surrounded with a pair of brackets.

Here's another example:

Code:
(var numberOfLoops) = numberOfLoops (send gEgo:loop)

Method #3: Calling An Object's Methods
Calling a method in an object is very similar as well. As an example, we will call the posn() method in the ego object to set it's position.

The posn() method takes two parameters, the x and the y values to position the ego at.

Code:
(send gEgo:posn(160 120))
  • The send tells the compiler that we want to access an object (a class or instance).
  • The gEgo is the name of the variable we are using to to store a pointer to the ego object.
  • The posn is the name of the method we want to call.
  • The 160 and 120 are the parameters for the posn() method.
  • All send operations are surrounded with a pair of brackets.

Method #4: Calling An Object's Methods and Obtaining it's Return Value
If a method in an object returns a value, you can obtain it just by calling it. As an example, we will check to see if ego has item #3 in it's inventory. To do this, we call the has() method.
Code:
(var hasItem)
= hasItem(send gEgo:has(3))
  • The send tells the compiler that we want to access an object (a class or instance).
  • The gEgo is the name of the variable we are using to to store a pointer to the ego object.
  • The has is the name of the method we want to call.
  • The 3 is the parameter for the has() method.
  • All send operations are surrounded with a pair of brackets.

This calls the ego's has() method to check if ego has item #3 in it's inventory. If it does, it will return TRUE, otherwise, it will return FALSE.


Method #5: Accessing Multiple Properties/Methods in an Object
Finally, send methods allow you to set as many properties and call as many methods as you want. To set the ego's view, position it, and then find out if item #3 is in it's inventory, you could do this:
Code:
(var hasItem)
= hasItem
   (send gEgo :
      loop(2)
      posn(160 120)
      has(3)
   )

This sets the ego's loop to 2, the position to 160,120, and checks if ego has inventory item #3. If ego has it, hasItem will be TRUE, otherwise, it will be FALSE.

In Conclusion
Understanding objects is the trickiest part to learn if you are new to object orientated programming languages. If you don't fully understand them yet, don't worry. Continue on with the tutorial, doing the step by step examples. When done, you should have a good grasp on them. If you still do not, come back to this chapter and read it again.
<< Prev Back to Tutorials Next >>

The SCI Programming Community
Copyright © 2007 - 2017, All Rights Reserved.