Author Topic: [Script] New Torquescript Arrays  (Read 3492 times)


1.0 - JS/C++ Arrays in Torque - basically beta testing this stuff


Description


this is a script i've been working on that makes it easier
to create and maintain arrays in torquescript. you can
now find a matching item, filter items, get the length,
make static and dynamic arrays...


Download




Documentation


  • simObjectID Array(%length,%dynamic,%i0...%i13)
  • Array Object "constructor". Creates an array you can later edit and manipulate
    with the assigned class functions.
    %length is the length of the new array,
    %dynamic is a bool and determines wether the resulting array can use ::pushNew() and
    ::popLast(),
    %i0 through %i13 are optional values to create in the new array in their respective indexes.
    The return value is the constructed array.

  • bool isArray(%array)
  • %array is a SimObject ID. The function determines if the Object ID you've
    entered is of an Array Instance, which will return true if it is, and false otherwise.
    You might not need this, but it is used by all following functions.

  • bool ArrayInstance::isDynamicArray()
  • The function determines if the array you're testing for is a Dynamic Array,
    which will return true if it is, and false otherwise.
    A dynamic array is an array that can get its length changed using ::pushNew()/::popLast()

  • int ArrayInstance::getLength()
  • Gets the length of the array instance you're testing for.
    Returns the length on success, -1 otherwise.

  • bool ArrayInstance::setIndex(%index, %value)
  • Sets input index %index to %value. Returns true on success, false otherwise.

  • bool ArrayInstance::pushNew(%value)
  • Creates a new entry with %value in array, consequently increasing the array length.
    Returns the new length on success, -1 otherwise.

  • int ArrayInstance::popLast()
  • Removes the latter entry in array, consequently decreasing the array length.
    Returns the new length on success, -1 otherwise.

  • any ArrayInstance::getIndex(%index)
  • Gets the value at index %index.
    Returns the value on success, "" otherwise.

  • bool ArrayInstance::forEach(%mode,%name,%i0...%i13)
  • Performs an action determined by %mode.
    There are two possible forEach modes:
    "method" : Calls a class function on all array items using arguments %i0 through %i13.
    "field" : Changes all object/tagged fields named %name on all array items to %i0.
    Returns true on success, false otherwise.

  • string ArrayInstance::toString(%emptyfilter)
  • Outputs a list of all values ordered by index and separated by spaces.
    Empty indexes can be left blank or replaced with %emptyfilter.
    Returns the list on success, "" otherwise.

  • bool ArrayInstance::remove(%index)
  • Removes the value at index %index, this does not shorten the length of the array.
    Returns true on success, false otherwise.

  • int ArrayInstance::find(%value)
  • Finds the first index to match the value of %value.
    Returns the index on success, -1 otherwise.

  • arrayID ArrayInstance::filter(%value)
  • Creates a new array with all values matching %value.
    Returns the new array on success, -1 otherwise.



Feel free to report bugs and things that need fixing.

« Last Edit: May 01, 2018, 05:27:19 AM by Vitawrap »


arrays start at 1

array offsets start at 0, array positions start at 1

i like the blue theme you have going but it does make it noticeably more difficult to read

also can you provide some benchmarks compared with global variables

i like the blue theme you have going but it does make it noticeably more difficult to read

also can you provide some benchmarks compared with global variables
it is effectively slower than global variable tables, but this way i can use table methods and nest arrays, i'll probably write another version that doesn't use objects

it is effectively slower than global variable tables, but this way i can use table methods and nest arrays, i'll probably write another version that doesn't use objects
??
i know its slower but I want to know how slower

do array variables have their own object? like if i did $array[5] = 3; and tried like, $array.method(), would that work? or are primitive data types not instantiated in torquescript

also how is this better or worse compared to the Arrayobject
« Last Edit: May 01, 2018, 03:15:37 PM by thegoodperry »

do array variables have their own object? like if i did $array[5] = 3; and tried like, $array.method(), would that work? or are primitive data types not instantiated in torquescript

also how is this better or worse compared to the Arrayobject

Arrays don't exist in this version of TorqueScript.

ArrayObjects weren't added until Torque3D
« Last Edit: May 01, 2018, 03:19:49 PM by Electrk.. »

ArrayObjects don't exist in TGE 1.3
oh lol i never even realized. why not just port it over to 1.3?

oh lol i never even realized. why not just port it over to 1.3?

because it's an engine-defined class lol

because it's an engine-defined class lol
oh that makes sense. so im guessing remaking it in torquescript would be too slow compared to the engine version?

I mean that's pretty much what he did here

it's the same functionality, just different method names
« Last Edit: May 02, 2018, 01:27:21 PM by Electrk.. »

I mean that's pretty much what he did here

it's the same functionality, just a different method names
oh lol full circle mb.



Quote
bool ArrayInstance::forEach(%mode,%name,%i0...%i13)
Performs an action determined by %mode.
There are two possible forEach modes:
"method" : Calls a class function on all array items using arguments %i0 through %i13.
"field" : Changes all object/tagged fields named %name on all array items to %i0.
Returns true on success, false otherwise.
wouldn't it be easier to use an iterator function in conjunction with a foreach like ArrayInstance::forEachNext() and have it return the next object? it seems less complex and easier to use than the current foreach setup

oh lol full circle mb.


wouldn't it be easier to use an iterator function in conjunction with a foreach like ArrayInstance::forEachNext() and have it return the next object? it seems less complex and easier to use than the current foreach setup

at this point you can just do $array.getIndex(%i) in a loop and go from there