Jump to content

Joystick Unigine 2.9


photo

Recommended Posts

I need to read when a specific joystick button is pressed.  When the button is pressed I will make changes to a IG Entity.  I have tried to create a joystick directly in the C++ api but without an example the syntax has me stumped.  I have imported the ungine script example and that works, however, I need to use the button press to manipulate entities on the IG side in C++.  I have tried the following in C++ to get the value of the updating script variable but it never changes.  First here is the unigine script direct from the sample with the addition of a new variable for a specific button that I will try to read into the C++ side.

#include <core/scripts/samples.h>

using Unigine::Samples;

#ifdef HAS_JOYSTICK

/*
 */
Info info;
ControlsJoystick joystick;

/*
 */
int init() {
	
	createInterface("samples/controls/joystick_00.world");
	engine.render.setShadowDistance(100.0f);
	setDescription("ControlsJoystick");
	
	info = new Info();
	joystick = new ControlsJoystick(0);
	joystick.updateEvents();
	//int b1state = 2;
	return 1;
}

/*
 */
int update() {
	
	string str;
	
	joystick.updateEvents();
	
	if(joystick.isAvailable()) {
		
		str = joystick.getName() + "\n";
		
		str += format("\nAxes %d:\n",joystick.getNumAxes());
		forloop(int i = 0; joystick.getNumAxes()) {
			str += format("%s:\t%5.2f\n",joystick.getAxisName(i),joystick.getAxis(i));
		}
		
		str += format("\nPovs %d:\n",joystick.getNumPovs());
		forloop(int i = 0; joystick.getNumPovs()) {
			str += format("%s:\t%d\n",joystick.getPovName(i),joystick.getPov(i));
		}
		
		str += format("\nButtons %d:\n",joystick.getNumButtons());
		forloop(int i = 0; joystick.getNumButtons()) {
			str += format("%d ",joystick.getButton(i));
		}
		int b1state = joystick.getButton(0);
		str +=format("button 1 state: %i\n",b1state);
	}
	
	else {
		
		str = "Not available";
	}
	
	info.set(str);
	
	return 1;
}

#else

/*
 */
int init() {
	
	createInterface("samples/controls/joystick_00.world");
	engine.render.setShadowDistance(100.0f);
	setDescription("This binary is built without ControlsJoystick support");
	
	return 1;
}

#endif

Next on the C++ side:

#include <UnigineInterpreter.h>
Interpreter* interp;

int AppSystemLogic::init()
{
  ...
	interp->get();
  ...
}

int AppSystemLogic::update() {
...
Log::message("b1 state from script export is %i\n",interp->getExternVariable("b1state"));
...
}

It always returns 0 even when I see the user interface change to 1 in the world window from the script update.

 

Thanks for the help.

Link to comment
×
×
  • Create New...