Jump to content

Binding new C++ function to a key in default.bind.cs


fr0zi
 Share

Recommended Posts

Hello guys,


I'd like to use Torque 3D to some vehicle simulation game. For that I need to add some new functions to Wheeled Vehicle class, for example - gearbox.

I'm thinking to add new datablock with gearbox parameters and new methods to WheeledVehicle class in C++ source code.


So I have created this in wheeledVehicle.h:

struct WheeledVehicleGearbox: public SimDataBlock
{
	typedef SimDataBlock Parent;
 
	U32 gearCount;		// number of gears in gearbox
	...
 
	WheeledVehicleGearbox();
	DECLARE_CONOBJECT(WheeledVehicleGearbox);
	static void initPersistFields();
	virtual void packData(BitStream* stream);
	virtual void unpackData(BitStream* stream);
};

 

and in wheeledVehicle.cpp I have added:

IMPLEMENT_CO_DATABLOCK_V1(WheeledVehicleGearbox);
 
ConsoleDocClass( WheeledVehicleGearbox,
	"@brief Defines the properties of a WheeledVehicle gearbox.\n\n"
	"@ingroup Vehicles\n"
);
 
void WheeledVehicleGearbox::initPersistFields()
{
	addField( "gearCount", TypeS32, Offset(gearCount, WheeledVehicleGearbox),
      "@brief Number of gears in the gearbox.\n\n"
      "Number of gears in the gearbox." );
 
	Parent::initPersistFields();
}

 

in WheeledVehicle class implementation I have added

void shiftUp();
void shiftDown();

 

and at the end of the wheeledVehicle.cpp

DefineEngineMethod( WheeledVehicle, shiftUp, bool, (),,
	"@brief Shift gear up.\n"
	"@return true if successful, false if failed\n\n" )
{
	object->shiftUp();
	return true;
}
 
DefineEngineMethod( WheeledVehicle, shiftDown, bool, (),,
	"@brief Shift gear down.\n"
	"@return true if successful, false if failed\n\n" )
{
	object->shiftDown();
	return true;
}

 

Now my question is - how can I bind those functions to a key in default.bind.cs so I can switch gears with keyboard?


Should I create "serverCmd..." for them in scripts? But I think shifting gear in a vehicle is irrelevant for server and should be done only on client. Isn't it?

I'm little confused here :) Maybe someone can give me some advise here?

Link to comment
Share on other sites

You can do it without creating a serverCmd function... I'm not totally sure of the best way to make the script know which vehicle you're referring to, but there are a lot of ways to hack it together. Basically, in your default.binds you could bind the key to a script function, and in that script function figure out what vehicle your player is mounted to, and call your shift function for that vehicle.


Re: whether the server needs to know, if you're making a typical multiplayer game the player and vehicle movement is handled by the server and pushed down to the clients to prevent cheating, so yeah, you'd probably want to run it through the server.


Here's an example of something similar I'm doing with default binds, in my case I have a SimGroup called SceneShapes that I've put all of my physics shapes into, and I'm calling each of them to do something. Your specifics will be different but the mechanism of calling a script function from default binds should be the same. If you can store a $myPlayer variable or something when you make your player, or store a reference to the vehicle when you create or mount it, then you should be good to go.

GlobalActionMap.bindCmd(keyboard, "alt g", "shapesAct();","");

function shapesAct()
{
  for (%i=0;%i<SceneShapes.getCount();%i++)
  {
     %shape = SceneShapes.getObject(%i);        
     %shape.setHasGravity(false);
     %shape.setDynamic(1);       
  }   
}

Link to comment
Share on other sites

I think you might want to consider a serverCmd() for it - mainly because I'm guessing you want shifting gears to modify vehicle speed. This has to be done on the server side as far as I am aware. But you would still call the CommandToServer() in the client-side control bind just as Chris shows.

Link to comment
Share on other sites

Yeah as rlranft says above, you will need this stuff server side. Even if you where not using it to drive the vehicle and just ran say your sound from it, other clients would need that data too so you will want it server side in any case. Even if you no intention of having multi player support, T3D still uses a client-server architecture regardless.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...