Jump to content

Adding a new UnigineScript function from C++


photo

Recommended Posts

Hi,

I want to define a UnigineScript function from text dynamically in C++.

This is the approach that I have for now : 

Unigine::Expression(Unigine::Engine::get()->getSystemInterpreter(), "{void func(){log.message(\"hello\");} }",1).run();

Now, when I call this function (either by UnigineScript in the system interpreter or by running another expression in C++), it seems that the function that I added above (`func`) is not defined.

Changing the second parameter to 0 doesn't seem to change this behavior.

I know I can add a C++ as an extern function to the interpreter, but here, this is not what I want to do. I want to port some old UnigineScript implementation directly from C++ as text.

Is there something that I am missing here?

Thanks in advance

Link to comment

Hi Karim,

I'm afraid that's not possible to achieve with expressions. They are executing code in isolated manner.

The only possible way to achieve that is to store all the required UnigineScript functions directly in the currently loaded world script (world_name.usc).

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment

Depending on how much freedom you have to augment the original UnigineScript, you could try something crazy like running an expression from an external function. Maybe like the following (expanded from your original example to demonstrate arguments and return values):

Unigine::Variable func(const char* message)
{
    Unigine::Expression e{
        Unigine::Interpreter::get(),
        R"({
          int func(string message) {
            log.message("%s\n", message);
            return 1;
          }

          string arg0;
          return func(arg0);
        })"};

    e.setVariable("arg0", Unigine::Variable{message});
    return e.run();
}

Unigine::Interpreter::addExternFunction("func", Unigine::MakeExternFunction(&func));

 

  • Like 2
Link to comment
×
×
  • Create New...