karim.salama Posted January 13, 2022 Posted January 13, 2022 Hello, I'm trying to use the Unigine Editor API to get the currently selected nodes into a UnigineScript variable used by the System Interpreter. The first approach that comes to mind is a function returning a vector: Unigine::Vector<Unigine::NodePtr> getEditorSelectedNodes(); This doesn't compile when I add the function to the interpreter using the following code for some reason. Unigine::Interpreter::addExternFunction("getEditorSelectedNodes", Unigine::MakeExternFunction(getEditorSelectedNodes)); I found this page on Unigine's documentation https://developer.unigine.com/en/docs/2.15/api/library/containers/class.arrayvector?rlang=cpp. The Unigine::ArrayVector can be used as a wrapper for UnigineScript vectors. But this class does not have an accessible constructor. So, instead of returning it in a function, I pass it as a parameter and append the selected nodes like this : void getEditorSelectedNodes(const Unigine::ArrayVector& nodes) { if (Editor::SelectorNodes* snodes = Editor::Selection::getSelectorNodes()) { for (const auto& n : snodes->getNodes()) { nodes.append(Unigine::Variable(Unigine::Interpreter::get(),n)); } } else { nodes.clear(); } } When I declare the array/vector like this Node selected_nodes[0]; and pass it to the function, an error occurs : "arrays can't be used in expressions". So instead, I used Unigine::Vector in UnigineScript. Now I get an error saying that it's impossible to convert a user class to Unigine::ArrayVector. So I change the argument type to Unigine::Variable : void getEditorSelectedNodes(const Unigine::Variable& nodes_variable) { auto nodes = Unigine::ArrayVector::get(Unigine::Interpreter::get(), nodes_variable); if (Editor::SelectorNodes* snodes = Editor::Selection::getSelectorNodes()) { for (const auto& n : snodes->getNodes()) { nodes.append(Unigine::Variable(Unigine::Interpreter::get(),n)); } } else { nodes.clear(); } } An error occurs on the function's first line : "Unigine::ArrayVector::get(): unknown variable type". I also tried with Unigine::Engine::get()->getSystemInterpreter() and getEditorInterpreter() and still got the same error. Am I missing something here? Thanks in advance
alexander Posted January 14, 2022 Posted January 14, 2022 Hi karim.salama, What if to use something like this? Interpreter::addExternFunction("getEditorSelectedNode", MakeExternFunction(getEditorSelectedNode)); Interpreter::addExternFunction("getEditorNumSelectedNodes", MakeExternFunction(getEditorNumSelectedNodes)); NodePtr getEditorSelectedNode(int i) { return Editor::Selection::getSelectorNodes()->getNodes()[i]; } int getEditorNumSelectedNodes() { return Editor::Selection::getSelectorNodes()->getNodes().size(); } Best regards, Alexander 1
karim.salama Posted January 14, 2022 Author Posted January 14, 2022 Hi alexander, This works! Thank you. But I'm still curious to know how ArrayVectors are ment to be used to wrap Vectors to the UnigineScript interpreter. Are there any examples for this? Thanks
alexander Posted January 16, 2022 Posted January 16, 2022 Hi, Look at the C++ API samples -> Scripts -> Arrays in the SDK Browser. void my_array_vector_generate(const Variable &id) { ArrayVector vector = ArrayVector::get(Interpreter::get(), id); vector.clear(); for (int i = 0; i < 4; i++) vector.append(Variable(i * i)); vector.remove(0); vector.append(Variable("128")); } Best regards, Alexander
alexander Posted January 16, 2022 Posted January 16, 2022 "arrays can't be used in expressions" - where are you trying to run UnigineScript? WorldExpressions?
karim.salama Posted January 17, 2022 Author Posted January 17, 2022 It's running in a system script that has been initialized with -system_script argument.
alexander Posted January 17, 2022 Posted January 17, 2022 Hmm... Okay, "C++ -> Arrays" should work then in your case.
Recommended Posts