Jump to content

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


Recommended Posts

Do you need it to do exactly that? Because it'd be pretty simple to create a SimGroup in your engine function, populate it, and return that. SimGroups can be indexed and iterated over in TS.


Easier still would be to just make a quick "SimGroup manager" class script-side.


What's the exact use-case?

Link to post
Share on other sites

http://docs.garagegames.com/torque-3d/reference/classArrayObject.html


 

new ArrayObject(numArray);
 
numArray.add(1,4);
numArray.add(2,3);
numArray.add(3,8);
 
numArray.add("1,1",5); 
numArray.add("1,2",9); 
 
function ArrayObject::getKeyFromValue(%this,%value){
   return %this.getKey(%this.getIndexFromValue(%value));
}
function ArrayObject::getValueFromKey(%this,%key){
   return %this.getValue(%this.getIndexFromKey(%key));
}
 
numArray.getValueFromKey(1); // will return 4
numArray.getValueFromKey("1,1"); // will return 5
Link to post
Share on other sites

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/blob/561f010f2e6411d8253d23f0cfcff794e81f60bf/Engine/source/console/arrayObject.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).

Link to post
Share on other sites

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?

%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.

Link to post
Share on other sites

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);
Link to post
Share on other sites
  • 3 weeks later...
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).

Link to post
Share on other sites
  • 7 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...