Thread: ScriptObject
View Single Post
  #1 (permalink)  
Old 01-04-2008, 05:04 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
ScriptObject

What is a ScriptObject?

A script object is basically an object that can store data, it's normally used in place of lots of global variables.

How to use a ScriptObject

To create the object all you need to do is:
Code:
new ScriptObject();
But that's not very useful!
How to make it useful you say?
Code:
new ScriptObject(MyScriptObject){
   dynamicField = "Whatever";
 };

MyScriptObject
is the name of our ScriptObject, This is used for when we need to get information later on. It can be named anything, but if there is already and object with that name it will get overwritten. You do not need to give your ScriptObject a name.

dynamicField is the bit that stores the script objects data, it can be named anything and it's also legal to use arrays with it.

"Whatever" is whatever you want it to hold, it can hold anything, objects, vector, numbers strings. If you make it call something, eg myfunction(), it would store whatever that function returns, the " "'s are only necessary if the thing it's storing is a string.

How to get information from your ScriptObject

All you need is your ScriptObject's Name or Variable, that's if you assigned it one and the dynamic field that you want to get the data from.

Here are two examples:
Code:
%a = MyScriptObject.dynamicField; 
//MyScriptObject is the name of our ScriptObject.
//dynamicField is the data we want to get from the ScriptObject.
Code:
%a = %scriptObject.dynamicField; //%scriptObject is our ScriptObject.
//dynamicField is the data we want to get from the ScriptObject
A few more things about ScriptObjects

You can assign ScriptObjects classes, these allow you to make functions without giving it a name:
Code:
new ScriptObject(){
class = MyScriptObject;
};
Script objects can have their own functions:
Code:
function MyScriptObject::function(%this, %var1, %var2, %var3){ //%this is the script object.
//whatever
}
To make your own ScriptObject functions your ScriptObject needs to have a name or class. In this case MyScriptObject was the class of our ScriptObject.

ScriptObjects can be stored in variables local and global:
Code:
%ScriptObject = new ScriptObject();
$ScriptObject = new ScriptObject();
If the script object is outside of a function it will be created when that file is executed, if it's in a function it will be created whenever that function is called.

You can copy a script object:
Code:
new ScriptObject(MyScriptObject : CopyThis);
CopyThis is the name of the ScriptObject that we're copying.

Hope this helped

__________________
PM me and tell me your name or a phrase and I shall write it in Elf for you. English Tengwar to be precise.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by MrPickle : 01-22-2008 at 10:34 AM. Reason: COMPLETELY forgot about classes
Reply With Quote
The Following 2 Users Say Thank You to MrPickle For This Useful Post:
ccoonen (01-05-2008), HelloWorld (01-05-2008)