aMoistKite Posted March 12, 2019 Share Posted March 12, 2019 How would i go about creating a 2D array in C++ and passing off to torquescript so that it would return like so: %data = funcThatMakeArrays(); echo(%data[1, "index2"]); Quote Link to comment Share on other sites More sharing options...
rlranft Posted March 12, 2019 Share Posted March 12, 2019 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? Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 13, 2019 Author Share Posted March 13, 2019 I guess it does not need to do exactly that. I am simply pulling data from a database but since i need to access the database very often I wanted as much of the work done in C++ as possible for performance. Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 16, 2019 Author Share Posted March 16, 2019 So could anyone give me some examples? Quote Link to comment Share on other sites More sharing options...
Bloodknight Posted March 16, 2019 Share Posted March 16, 2019 sqlite is integrated into the engine with a cmake option, if nothing else this should operate as a framework for adding any other database system Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 17, 2019 Author Share Posted March 17, 2019 I do not see any cmake option for sqlite, and all the resources i could find on it over on garagegames have dead links. SQLite would not work for my application anyway. Quote Link to comment Share on other sites More sharing options...
NeonTiger Posted March 17, 2019 Share Posted March 17, 2019 What about array objects? Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 18, 2019 Author Share Posted March 18, 2019 What about array objects?May I have an example? Quote Link to comment Share on other sites More sharing options...
NeonTiger Posted March 18, 2019 Share Posted March 18, 2019 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 Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 20, 2019 Author Share Posted March 20, 2019 I was meaning an example how to define one in C++ and pass one it to torquescript. Quote Link to comment Share on other sites More sharing options...
irei1as Posted March 20, 2019 Share Posted March 20, 2019 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 ishttps://github.com/GarageGames/Torque3D/blob/561f010f2e6411d8253d23f0cfcff794e81f60bf/Engine/source/console/arrayObject.hTo 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). Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 21, 2019 Author Share Posted March 21, 2019 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. Quote Link to comment Share on other sites More sharing options...
rlranft Posted March 22, 2019 Share Posted March 22, 2019 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? Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 22, 2019 Author Share Posted March 22, 2019 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. Quote Link to comment Share on other sites More sharing options...
rlranft Posted March 22, 2019 Share Posted March 22, 2019 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 Quote Link to comment Share on other sites More sharing options...
aMoistKite Posted March 23, 2019 Author Share Posted March 23, 2019 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.htmlI 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. Quote Link to comment Share on other sites More sharing options...
Happenstance Posted March 23, 2019 Share Posted March 23, 2019 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); Quote Link to comment Share on other sites More sharing options...
rlranft Posted April 7, 2019 Share Posted April 7, 2019 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). Quote Link to comment Share on other sites More sharing options...
rlranft Posted December 2, 2019 Share Posted December 2, 2019 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.