You can now define variables and functions inside object types. Basically, they're treated as global variables and functions by the compiler and engine... but at times, it makes more sense to include certain functionality and values within your objects. For example, take the following example:
objectType coins ("bag of coins") { var total = 10; sub collect (num) { total += num; playSound ('cha-ching!.wav'); } event lookAt { say (ego, "I've got " + total + "coins."); } }
Here, a member variable has been created called "total" and a there's a member function called "collect" - plus a "lookAt" event. You can see here that an object type's member functions and events can use member variables just as if they were globals.
You can also access member variables and functions from outside the object type... simply precede the name of the member variable or function with the name of the object type followed by a period. For example...
sub talkToSomeone () { say (someone, "How many coins have you got?"); say (ego, "Er, " + coins.total + " at the moment."); say (someone, "Well, here... have 100 more."); coins.collect (100); say (ego, "Thanks!"); }
You can create member functions and variables with the same name as member functions and variables within other object types... it's completely safe, they won't confuse the engine or compiler.
You can also create member functions and variables with the same name as global functions and variables...
var foobar = "This is a global variable!"; sub doStuff () { say (ego, "I'm in the global doStuff function!"; } objectType something ("something") { var foobar = "This is a member variable!"; sub doStuff () { say (ego, "I'm in the member doStuff function!"; } event lookAt { doStuff (); say (ego, foobar); } } objectType somethingElse ("something else") { event lookAt { doStuff (); say (ego, foobar); } }
In the lookAt event inside the "something" object type, above, the member variable and member function would be used. However, inside the "somethingElse" object type, the global variable and function would be used.
Or in other words, it works pretty much as you'd expect, and in much the same way as member variables and functions of classes in object-oriented languages, such as Java and C++.
SLUDGE and this SLUDGE documentation are copyright Hungry Software and contributors 2000-2012