Define a 2D array in C++ and passing to TS

Expanding and utilizing the engine via C++.
  • 1
  • 2
19 posts Page 2 of 2
irei1as
Posts: 84
Joined: Fri Feb 27, 2015 5:13 pm
by irei1as » Wed Mar 20, 2019 6:28 pm
If you need C++ you can just copy what array does. Having access to the code is quite convenient.

Define a struct with as many elements as you need. Array has strings key and value (you can set whatever values, types and quantity for your need) and then use the class Vector to set the, uh, vector.

Err, the header with that is
https://github.com/GarageGames/Torque3D ... ayObject.h

To get the values out use something similar of what you see in its arrayObject.cpp file with getKey and getValue (the Torquescript entry point is the DefineEngineMethod thing).
aMoistKite
Posts: 21
Joined: Wed Mar 06, 2019 10:07 am
by aMoistKite » Thu Mar 21, 2019 8:49 am
I don't think that implementation would work for what I need. I will go back to my original question, how do you define a 2D array in C++ and pass it to torquescript? I have been digging around in the source code and i can't find any examples of this.
rlranft
Posts: 308
Joined: Thu Feb 05, 2015 3:11 pm
 
by rlranft » Fri Mar 22, 2019 8:51 pm
Okay, can we get the actual use-case?
You can't find any examples of this because there is no need to do this. TorqueScript has multi-dimensional arrays. Why are you not just using them? What is the perceived bottleneck?
aMoistKite
Posts: 21
Joined: Wed Mar 06, 2019 10:07 am
by aMoistKite » Fri Mar 22, 2019 9:03 pm
Okay, can we get the actual use-case?
You can't find any examples of this because there is no need to do this. TorqueScript has multi-dimensional arrays. Why are you not just using them? What is the perceived bottleneck?

Code: Select all

%username = %mydata["row", "collum"];
something along those lines.
I can't find any examples how to create said array in C++ and pass it to tourqescript.
rlranft
Posts: 308
Joined: Thu Feb 05, 2015 3:11 pm
 
by rlranft » Fri Mar 22, 2019 10:25 pm
You can already define a 2d array in torquescript. Why are you fixated on thunking back and forth to C++?

http://www.roostertailgames.com/TorqueRef/index.html
aMoistKite
Posts: 21
Joined: Wed Mar 06, 2019 10:07 am
by aMoistKite » Sat Mar 23, 2019 2:13 am
You can already define a 2d array in torquescript. Why are you fixated on thunking back and forth to C++?

http://www.roostertailgames.com/TorqueRef/index.html
I am well aware. I need to DEFINE the array in C++ THEN pass it to torquescript. I am using MySQL and I have to access it in C++ then pass it to torquescript.
Happenstance
Posts: 93
Joined: Sat Apr 11, 2015 9:08 pm
by Happenstance » Sat Mar 23, 2019 7:07 pm
The problem is TorqueScript's concept of arrays are really just dynamic variables (strings specifically) with syntactic sugar to make their usage more familiar (myArray[0] is the same as myArray0 in script). There's no straightforward method of passing a C++ array to script, which is why ArrayObject was created.

If you can't use ArrayObjects as @ irei1as suggested you're going to have to get creative and probably write some janky code, unfortunately. One option might be to rework your C++ 'funcThatCreatesArrays()' function to instead spit out a tab-delimited string containing the row, column, and value of each array index. You could then use that string to make your own array on the script side, something like:
%arrData = funcThatCreatesArrays(); // returns "rowNum" TAB "colNum" TAB "value"
$myArray[getField(%arrData, 0), getField(%arrData, 1)] = getField(%arrData, 2);
rlranft
Posts: 308
Joined: Thu Feb 05, 2015 3:11 pm
 
by rlranft » Sun Apr 07, 2019 8:18 pm
One option might be to rework your C++ 'funcThatCreatesArrays()' function to instead spit out a tab-delimited string containing the row, column, and value of each array index. You could then use that string to make your own array on the script side, something like:
%arrData = funcThatCreatesArrays(); // returns "rowNum" TAB "colNum" TAB "value"
$myArray[getField(%arrData, 0), getField(%arrData, 1)] = getField(%arrData, 2);
I second this and have done it on several occasions. Also, any time I have used any sort of SQL back-end this is essentially how I have done it. Works like a charm and would result in neater script-side code than my following suggestion:

SimGroups are also handy (and can already be created engine-side and passed to script), since they can contain other groups - lists of lists, or "multidimensional arrays", with the added benefit that each object is unique and so can stand in as multimaps (dictionaries that allow multiple "same key" entries) since each SimGroup is a unique object but can have the same internalName (where I'd store the "key" if I wanted this functionality). If you're using SimGroups like this, keep in mind that adding a group to another group removes it from any group it is already a member of - a SimGroup can only belong to one parent. I used this (and dynamic code generation script-side using exec()) when working on the Tower Defense template used in the original 3 Step Studio project for the wave editor tool to allow users to create waves of arbitrary unit count and composition to track and update the GUI elements for displaying current wave contents.

So -
Create top parent group. Create "array 0 level" group entry 0. Set "array 0 level" entry 0 internalName field to "key" name/value. Create groups (or SimObjects) to add to "array 0 level" group 0 and set internalName to "value" value and add to "array 0 level" group entry 0 until satisfied.
Create "array 0 level" group entry 1. Set "array 0 level" entry 1 internalName field to "key" name/value. Create groups (or SimObjects) to add to "array 0 level" group 1 and set internalName to "value" value and add to "array 0 level" group entry 1 until satisfied.
Wash, rinse, repeat.
Push back to script side via trampoline (using existing examples in engine for guidance).
Loop through and populate script-side array from groups.

I suppose you could add some syntactic sugar to SimGroup/SimObject to allow script-side access to sub-elements by internalName using array notation if you wanted that - it would sure neaten up the script-side access of these structures and completely eliminate the need to convert the result to an array script-side (as both methods above would require).
rlranft
Posts: 308
Joined: Thu Feb 05, 2015 3:11 pm
 
by rlranft » Mon Dec 02, 2019 6:02 pm
You know, I just remembered that by default the TS functions getRecords()/getFields() are actually for dealing with exactly this in script.

Check the comments in Engine\source\console\consoleFunctions.cpp for details.
  • 1
  • 2
19 posts Page 2 of 2

Who is online

Users browsing this forum: No registered users and 0 guests