Home › Forums › Programming › Passing arrays as parameters to puzzles
- This topic has 7 replies, 4 voices, and was last updated 5 years, 8 months ago by Yuri Kovelenov.
-
AuthorPosts
-
2019-03-22 at 3:25 pm #13143scalareCustomer
Hello All,
we need to pass a parameter to the puzzles with an array of object names through the runCode function, and have that array converted into a list that will drive a loop that will change the material for all those objects. Is there a way to do that through the puzzles? I mean, a way to convert the array into a list?
Thank you!
2019-03-22 at 4:33 pm #13144jemCustomerHi scalre, sorry to hijack your question, but I am looking for something similar. I am passing multiple values into the puzzles through a runCode function, but each of those values passes via its own function parameter. This limits the number of values to a fixed quantity. The large number of parameters makes the function declaration inelegant.
I would rather pass a single JSON string into my custom runCode function and parse that data in the puzzles. The parser would stuff the values from the JSON into block variables and arrays.
I could write such a parser in JS and call it from a puzzle, but I tried this a few months ago and I could not make it work. As I recall, the problem was variable scoping. My parser script could not reference the variables is the visual_logic.js from the “main_app”.js I should revisit this again. It would be a useful tool.
Jeremy Wernick
2019-03-22 at 4:39 pm #13146scalareCustomerHi Jem,
yes, we were evaluating the possibility of editing a function inside the visual_logic.js, but then I thought I should ask here first, in case we were missing something. The JSON isn’t a bad idea, probably better since it would allow internal structure.
2019-03-22 at 11:37 pm #13149elkCustomerHi guys.
I had a poke around in the code (still got a lot to learn about the verg3d/three.js codebase tho) but I made this little “snippet” that atleast work in the console with the app “class” exposed in Chrome and FF on my PC, did not test it in runCode tho;
app.scene.getObjectByName(“Icosphere.001”).material = app.materials.find(x => x.name === “Material.003”);
Replace “Icosphere.001” with your objects name, and “Material.003” with the material you want it to change too. You could just code a loop around this if you got the objects and materials listed in an array or JSON file or whatever.
PS! The find method/function I just found on a quick search, and according to my findings it might be an EcmaScript6 thing, so might not be usable everywhere. So this is just to maybe point you in the right direction, and I suspect the Verge3D has a thing or to to correct me on for this single line of code Atleast there should be a better way to get the material by name I suspect.
I could not see the reason for getting it through the puzzle system tho, maybe elaborate on what you want to do, or why you need it to work a specific way.
2019-03-23 at 12:11 am #13151elkCustomerAlso just realized there is a “make list from text <puzzle> with delimiter <puzzle>” puzzle. You should be able to then condense (concatinate) the entire array or JSON into a coma separated list (as one singe string/variable) for instance and pass that around as one variable either through JS calls or the load data for instance, if you got the data in a separate file.
2019-03-23 at 10:30 am #13157Yuri KovelenovStaffI would rather pass a single JSON string into my custom runCode function and parse that data in the puzzles. The parser would stuff the values from the JSON into block variables and arrays.
This is now possible thanks to Dictionary data type. This is a JSON-like structure that you can supply form JavaScript and parse with the puzzles.
2019-03-27 at 3:03 pm #13333jemCustomerYuri,
That is a great suggestion. I was able to pass a JSON object from the ExternalInterface JavaScript into the puzzles. The “in dict” dictionary puzzle parses the message into values. These values were then stored in local puzzle variables. This works for strings, numbers, and arrays. I will be using this technique in our work.For the others on this thread, here is my code and puzzles.
function prepareExternalInterface(app) { app.ExternalInterface.updateScene = function() { var jsonMessage = {"count" : 3, "color":"red", "object_names": ["sphere", "suzanne", "torus"]}; app.ExternalInterface.setValues(jsonMessage); } }
Jeremy Wernick
2019-03-28 at 6:47 am #13343Yuri KovelenovStaff -
AuthorPosts
- You must be logged in to reply to this topic.