Jump to content

[SOLVED] Array VS Vector VS ArrayVector


photo

Recommended Posts

Posted

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

Posted

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

  • Thanks 1
Posted

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

Posted

Hi,

Look at the C++ API samples -> Scripts -> Arrays in the SDK Browser.

изображение.png

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

Posted

"arrays can't be used in expressions" - where are you trying to run UnigineScript? WorldExpressions?

Posted

It's running in a system script that has been initialized with -system_script argument.

Posted

Hmm... Okay, "C++ -> Arrays" should work then in your case.

  • silent changed the title to [SOLVED] Array VS Vector VS ArrayVector
×
×
  • Create New...