file The ArrayObject()

  • Jeff
  • Jeff's Avatar Topic Author
  • Offline
  • Developer
  • Developer
  • PlatinumQuest Programmer
More
30 Dec 2012 02:45 #1 by Jeff
The ArrayObject() was created by Jeff
Torque3D feature implimented for marble blast in pure torqueScript.

I hope everybody had a fantastic year. As a end-of-the-year gift, I present to you the array object, an extension to the script object. Although not exciting and for non-programmers this will be boring, this allows you to finally have an array-based object just like in javascript, php, you name it! This code was tested and works. If you have trouble with the code please post here.

Main Use: so you don't always have to use global variable arrays
(not only that, the array object organizes itself if an index is deleted!)

The current version of this script is: Version 1.01

Changelog:

Version 1.01 :
-Added a clear method to clear the contents of the array

Anyways, here is the code:

Code://
// arrayObject.cs
// Version 1.01
//
// Open source file that lets marble blast's torqueScript expand upon
// the ScriptObject to use an ArrayObject
//
// To declare a new array object, please use the following syntax:
// for an array object without a name:
// %obj = Array();
// for an array object with a name choose either a or b:
// a:
// %obj = Array(helloworld);
// b:
// Array(helloworld);
//
// All values are stored by numerical, integer indexes. However, because
// TorqueScript is pretty much string-based, you can make values strings,
// ints, decimals, objects, ect.
//
// Note: all array objects carry all the methods from the ScriptObject
// base class, but any method with the Array namespace is only available
// to array objects and not other kinds of script objects.
// Special methods available to just the array object:
// fillArray(string list)
// getEntryByIndex(int index)
// addEntry(variable entry)
// listValues()
// removeEntryByIndex(int index)
// clear()
//
// Example:
// %obj = Array(Pie);
// for (%i = 0; %i < 5; %i ++)
// %obj.addEntry(%i);
// echo(Pie.listValues());
//
// Please credit me whenever you use this code. Although not required,
// it is much appreciated. :)
// Thank you!
// ~ Jeff, MBP Programmer
//

// Jeff: create a new array
function Array(%name) {
%array = new ScriptObject(%name) {
class = Array;
size = 0;
};
return %array;
}

// Jeff: fill an array all at once
function Array::fillArray(%this, %list) {
%size = getFieldCount(%list);
for (%i = 0; %i < %size; %i ++)
%this.val[%i] = getField(%list, %i);
%this.size = %size;
}

// Jeff: add an entry to the array object
function Array::addEntry(%this, %entry) {
%this.val[%this.size] = %entry;
%this.size ++;
}

// Jeff: remove an entry from the array object by specifing the index
function Array::removeEntryByIndex(%this, %index) {
if (%index >= %this.size || %index < 0) {
error(%this.getId() @ ::removeEntryByIndex(index) SPC -> invalid index.);
return;
}

// Jeff: reorganize array by lowering each value by the index of where it
// was removed
%this.size --;
for (%i = %index; %i < %this.size; %i ++) {
%val = %this.val[%i + 1];
%this.val[%i] = %val;
}
%this.val[%this.size] = ;
}

// Jeff: get a value from the specified index of the array
function Array::getEntryByIndex(%this, %index) {
if (%index >= %this.size || %index < 0) {
error(%this.getId() @ ::getEntryByIndex(index) SPC -> invalid index.);
return;
}
return %this.val[%index];
}

// Jeff: get all the values of the array in a TAB based list
function Array::listValues(%this) {
%list = 0;
for (%i = 0; %i < %this.size; %i ++) {
%val = %this.val[%i];
%list = (%list $= ) ? %val : %list TAB %val;
}
return %list;
}

// Jeff: clear array
function Array::clear(%this) {
for (%i = 0; %i < %this.size; %i ++)
%this.val[%i] = ;
%this.size = 0;
}

I am a programmer. Most here know me for being one of the major contributors to Marble Blast Platinum and PlatinumQuest.

Please Log in or Create an account to join the conversation.

  • Jeff
  • Jeff's Avatar Topic Author
  • Offline
  • Developer
  • Developer
  • PlatinumQuest Programmer
More
30 Dec 2012 06:23 #2 by Jeff
Replied by Jeff on topic The ArrayObject()
Updated to version 1.01

Version 1.01 :
-Added a clear method to clear the contents of the array

I am a programmer. Most here know me for being one of the major contributors to Marble Blast Platinum and PlatinumQuest.

Please Log in or Create an account to join the conversation.

  • Beack
  • Beack's Avatar
  • Offline
  • Professional Marbler
  • Professional Marbler
  • Beack ewe
More
31 Dec 2012 21:05 #3 by Beack
Replied by Beack on topic The ArrayObject()
One question.

What exactly the code does?

The weirdest signature that I ever requested
[/size]

Please Log in or Create an account to join the conversation.

More
31 Dec 2012 21:37 #4 by Don.Gato
Replied by Don.Gato on topic The ArrayObject()
It.... Allows you to use arrays in MB codes.


Seems pretty obvious to me.

Please Log in or Create an account to join the conversation.

  • Jeff
  • Jeff's Avatar Topic Author
  • Offline
  • Developer
  • Developer
  • PlatinumQuest Programmer
More
31 Dec 2012 21:59 #5 by Jeff
Replied by Jeff on topic The ArrayObject()
Thanks Don


Quote:Main Use: so you don't always have to use global variable arrays
(not only that, the array object organizes itself if an index is deleted!)

I am a programmer. Most here know me for being one of the major contributors to Marble Blast Platinum and PlatinumQuest.

Please Log in or Create an account to join the conversation.

Moderators: AayrlKalle29Regislian