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

Expanding and utilizing the engine via C++.

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

fr0zi
Posts: 4
Joined: Mon Jan 18, 2016 4:23 pm
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()
{
"@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?

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

chriscalef
Posts: 375
Joined: Mon Feb 09, 2015 7:48 pm
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.

Code: Select all

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); } } 

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

fr0zi
Posts: 4
Joined: Mon Jan 18, 2016 4:23 pm
Thanks, chriscalef.
I will give it a try.

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

rlranft
Posts: 298
Joined: Thu Feb 05, 2015 3:11 pm

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.

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

Timmy
Posts: 361
Joined: Thu Feb 05, 2015 3:20 am
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.

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

fr0zi
Posts: 4
Joined: Mon Jan 18, 2016 4:23 pm
Ok, thanks guys I tried with the serverCmd() and it worked. Therefore I think I will use that.

Thanks for help!

#### Who is online

Users browsing this forum: Bing [Bot] and 2 guests