Jump to content

Circular #include directives & accessing member variables of another namespace


photo

Recommended Posts

Hi guys,

 

We are having some trouble setting up our #include directives so that scripts can access functions and member variables of other, related scripts. I've set up a small project with the hope of tracking the problem down but am still having trouble.

 

Here is the world script as well as two script files which reference functions and variables in the other. Accessing functions works fine but trying to access a variable will bring up an "unknown token" error.

 

//WORLD SCRIPT

 

#ifndef WORLD

#define WORLD
 
#include <core/unigine.h>
#include <test1.cpp>
#include <test2.cpp>
 
int init() {
 
PlayerSpectator camera = new PlayerSpectator();
camera.setPosition(Vec3(2.0f,0.0f,1.5f));
camera.setDirection(Vec3(-1.0f,0.0f,-0.5f));
engine.game.setPlayer(camera);
 
return 1;
}
 
int shutdown() {
return 1;
}
 
int update() {
test1::updateValue1();
test2::updateValu1();
 
log.message("v1 = " + test1::v1 + " v2 = " + test2::u2 + "\n");
 
return 1;
}
#endif

 

//TEST SCRIPT 1

 

#ifndef t1

#define t1
 
#include <test2.cpp>
 
namespace test1
{
int v1 = 0;
int v2 = 0;
 
void updateValue1()
{
v1++;
test2::updateValu2();
}
 
void updateValue2()
{
v2++;
}
}
#endif

 

//TEST SCRIPT 2

 

#ifndef t2

#define t2
 
#include <test1.cpp>
 
namespace test2
{
int u1 = 0;
int u2 = 0;
 
void updateValu1()
{
u1++;
test1::updateValue2();
}
 
void updateValu2()
{
u2++;
 
if (test1::v1 > 0)
log.message("\n variable has been accessed \n");
}
}
#endif

 

The result, as said above, is an unknown token for "test1::v1" in test script 2. If that block is removed, everything runs fine and the console logs out the ever-increasing values. test1::updateValue2() and test2::updateValu2() are both working just fine.

 
Any help would be greatly appreciated, thanks!
Link to comment

You can forward declare classes to work around this:

class A;

class B {
    A a;
    void foo();
};

#include <B.h>

void B::foo() {
    a.bar();
}

We had to use this a lot to avoid circular includes. You may have to change some namespace variables into classes, as I am not sure you can forward declare global variables the same way in UnigineScript.

Link to comment
×
×
  • Create New...