Game Development Community

Type conversion between ConsoleMethod and function

by Andrew Edmonds · in Torque Game Engine · 03/27/2007 (3:48 pm) · 2 replies

Hi all,

I'm trying to add a new variable to my player object and ran through the code here.

However, like the original post, I'm getting a completely different (very large) number returned. I'm fairly new to c++ and learning as I go but I'm guessing that I have a problem with type conversion somewhere.

The attribute I've added to my player is called 'jumpType' and the two console methods I'm creating are setJumpType(); and getJumpType(x);

I know getJumpType is working as I can run it in the console and it returns 1 (the default value).

Here's my code:

player.h
void setJumpType(S32 newJumpType);
S32 getJumpType();

player.cpp
void Player::setJumpType(S32 newJumpType)
{
	Con::errorf("setJumpType method recieving %d", newJumpType);
	jumpType = newJumpType;
}

S32 Player::getJumpType()
{
	return jumpType;
}

//-----------------

ConsoleMethod( Player, setJumpType, void, 0, 0, "Sets the current jump type." )
{
	Con::errorf("ConsoleMethod sending %d", (int)argv[2]);
	object->setJumpType((int)argv[2]);
}

ConsoleMethod(Player, getJumpType, S32, 0, 0, "Gets the current jump type.")
{
	return object->getJumpType();
}

Console Output:
==>echo(1695.getjumptype());
1
==>1695.setjumptype(3);
ConsoleMethod sending 19249893
setJumpType method recieving 19249893
==>echo(1695.getjumptype());
This is the getJumpType method
19249893

I know the argv index is correct as if I change the %d in the Con::errorf to a %s It gives me the correct value (3).

I'm getting tired and grumpy now so if anyone can shed any light on this I would be incredibly grateful!

Thanks all...

About the author

Formed in 2005, EiKON Games is an indie games development project based in the UK working on the tactical first person shooter "Epoch: Incursion". See the Join Us or Contact Us pages at http://www.eikon-games.com/


#1
03/27/2007 (9:09 pm)
Hi Mike,

Try something like...

object->setJumpType(int(dAtoi(argv[2])));

There are several similar helper functions to convert from console strings to various data types...

dAtoi - int
dAtof - float
dAtob - bool
etc.

Hope this helps,

Ben
#2
03/28/2007 (12:12 am)
That's sorted it Ben, works a treat - Thanks so much.

Andy