Jump to content

addExternVariable


photo

Recommended Posts

https://developer.unigine.com/en/docs/2.7.2/code/cpp/usage/variables

I'm trying to follow the above example on making a variable usable in UnigineScript from C++.  

When I attempt the above I get the error that the function MakeExternVariable is not defined.  I also couldn't find it anywhere else in the documentation.  When I try to put anything else into the second argument of addExternVariable I get the error "argument of type float is incompatible with Unigine::ExternVariableBase* .

my code:

 

void AppWorldLogic::entity_control(Cigi::ICigiEntityControl* control) {
    // getting values from the incoming Host packet

    Math::dvec3 receivedPosition;

    int entityType = control->getEntityType();

    if (entityType == 0) {

        receivedPosition = control->getPosition();

        Math::dvec3 cartesianPosition;
        //Math::vec3 eulerAngles;

        vec3 eulerAngles = control->getRotation();
        float roll = eulerAngles.x;
        Interpreter::addExternVariable("myRoll",roll);

        Log::warning("\nEuler angles for aircraft %d (roll, pitch, yaw) = (%f, %f, %f) \n", entityType, eulerAngles.x, eulerAngles.y, eulerAngles.z);
        
    } // if type 0

}// entity_control

Link to comment

Hi Robert,

the MakeExternVariable() function is defined in the UnigineInterface.h file, so you have to include it too:

#include <UnigineInterface.h>

This should solve the problem.

Seems like there was a bug, we shall restore missing description in the documentation by the upcoming release. Sorry for the inconvenience caused!

Thank you!

Link to comment

Thanks!  I can now compile the C++ code without error, however, I'm still having issues getting the script side to recognize the exported variables.

from the C++ side

#include "AppWorldLogic.h"
#include <UnigineEditor.h>
#include <UnigineGame.h>
#include <UnigineApp.h>
#include <UnigineInterpreter.h>
#include <UnigineInterface.h>

using namespace Unigine;
using namespace Cigi;
using namespace Math;

void AppWorldLogic::entity_control(Cigi::ICigiEntityControl* control) {
    // getting values from the incoming Host packet

    Math::dvec3 receivedPosition;

    int entityType = control->getEntityType();

    if (entityType == 0) {

        receivedPosition = control->getPosition();

        Math::dvec3 cartesianPosition;
        //Math::vec3 eulerAngles = vec3(1.0f,2.0f,3.0f);

        vec3 eulerAngles = control->getRotation();
        //float roll = eulerAngles.x;

        
        // export a variable and specify a name to access it from Unigine scripts
        Interpreter::addExternVariable("eulerAngles", MakeExternVariable(&eulerAngles));

        Log::warning("\nEuler angles for aircraft from cpp %d (roll, pitch, yaw) = (%f, %f, %f) \n", entityType, eulerAngles.x, eulerAngles.y, eulerAngles.z);
        
    } // if type 0

}// entity_control

from the unigine script side:

#include <core/unigine.h>
#include <core/systems/tracker/tracker.h>
#include <helicopter.h>
#include <ship.h>
#include <quality_settings/quality_settings.h>

using Unigine::Tracker;

int update() {

/*....lots of other code here...*/

    log.message("Euler angles accessed from the script are " + eulerAngles + " \n");
    return 1;
}

Here is the resulting error message.

log.message("Euler angles accessed from the script are " + eulerAngles + " \n");
demos/oil_platform/oil_platform.cpp:955: Interpreter::parse_expression(): unknown token "eulerAngles" in ""Euler angles accessed from the script are " + eulerAngles + " \n"" expression

Is this a scope issue?  I thought that the exported variable would be global and accessible from any part of the script.

Link to comment

The first thing I notice is that your eulerAngles variable is going out of scope at the end of entity_control() - in the doco you linked to the variables never go out of scope because they're declared in main().

Link to comment
×
×
  • Create New...