Game Development Community

Adding new class and using it in script

by game4Rest · in Torque Game Builder · 01/25/2006 (7:34 pm) · 10 replies

Hi, all!

I made a new class "CharacterInfo" to hold updates sent from TCP/IP based Server. I added it under game\net. One of its functions is "Update", which is called everytime any updates are made from the Server. But in this function, callback "onUpdated" has some problem and I can not figure out.

//**********************************************************
DECLARE_CONOBJECT(CharacterInfo); //at the end of the CharacterInfo class in CharacterInfo.h
IMPLEMENT_CONOBJECT(CharacterInfo); //at the beginning of the CharacterInfo.cc

void CharacterInfo::Update(unsigned short type)
{
switch(type)
{
case TM_DTS2C_ANSWER_AUTH:
convert2ClientCoord(m_stPos.m_nPosX, m_stPos.m_nPosY); // to client coordination
Con::executef(this, 4, "onUpdated",TM_DTS2C_ANSWER_AUTH, m_stPos.m_nPosX,
m_stPos.m_nPosY); //<------this makes problem....
break;

case TM_DTS2C_ANSWER_MOVE:
Con::executef(this, 4, "onUpdated",TM_DTS2C_ANSWER_MOVE, m_sSpeed, 0);//<------ this makes problem....
break;

case TM_DTS2C_ANSWER_STOP:
-----------------------
break;
}
}
//**************************************************************
In script,

//*****************************************************
$CharInfo = new CharacterInfo(AvatarInfo);
function AvatarInfo::onUpdated(%this, %type, %arg0, %arg1)
{
switch(%type)
{
case 1: //Answer for Auth
$player.setPosition("%arg0 SPC arg1");

case 3: //Answer for Move
$player.runSpeed = %arg0;

case 6: //Answer for Stop
moveToServerSentPos(%arg0, %arg1);
}
}

I can not make onUpdated function work any how.
Console.log says

"Con::execute - 0 has no namespace: onUpdated".

So I think I made some mistakes instanciating CharacterInfo. But I don't know what the mistakes are. I can not go any further from here. I'm wondering if I'm missing something here.
So, please someone give me any advice.

Thanks in advance.

Hong Jin

#1
01/25/2006 (7:41 pm)
What class did you derive your CharacterInfo class from? Plus, have you exec'd in a file that has a datablock defined as AvatarInfo?
#2
01/25/2006 (8:39 pm)
Stephen, nice to see you again and thanks for your answer.

I derived it from SimObject like this.
class CharacterInfo : public SimObject
{
}

And I'm afraid I didn't make AvatarInfo datablock. Do I have to define AvatarInfo datablock to solve my problem? I'm afraid I have no idea about that.

Thanks in advance.

Hong Jin
#3
01/25/2006 (8:43 pm)
Actually, no now that I look at it a bit more clearly, it appears that your issue is how you are calling the function itself. How specifically are you calling your AvatarInfo::onUpdated() method?
#4
01/25/2006 (9:12 pm)
Let me come out with right direction movement request and answer between client and server.


(client)right key down -> (client )Reqeust for Move to Server->

(server)Accept Request and issues the Direction ->
(client)Update Direction of character issued by Server. Here calls "CharacterInfo::Update(kind of change)" ->

(client)Let the Script side know the updates by calling "onUpdated()" ->
(client)Render as updated

I wonder if it helps to make it clear.

edit:
If it helps....
I examined Con::executef(this, 1, "onConnected"); in the TCPObject.cc. When it is excuted, in the debugger, "this" contains the SimObject name and it is "TCPConnection" as I declared "$TownConn= new TCPObject(TCPConnection);" in the script.

But when I excute "Con::executef(this, 4, "onUpdated",TM_DTS2C_ANSWER_MOVE, m_sSpeed, 0);" as above mentioned, this doesn't have SimObject name. Instead, it outputs "BadPtr". But I don't know why.

In addition, I found this.

Thanks in advance.

Hong Jin
#5
01/25/2006 (11:45 pm)
I've found I made something ambiguous.
In script, as I mentioned before, I declared

$CharInfo = new CharacterInfo(AvatarInfo);

Also, in engine, I declared "CharacterInfo *pCharInfo=new CharacterInfo;" for some needs.

I wonder if these two instances can be crashed or not.

Thanks in advance.

Hong Jin
#6
01/26/2006 (7:13 am)
Short answer: you don't want/need that second creation within the source code. In addition, whenever you do create an object like that in source code that should be part of your simulation (your game), you need to register it with the simulation using:


pCharInfo->registerObject("AvatarInfo");


The thing is, you've already registered it with your script creation.

Instead of trying to create a new one in code, instead go find the one you've already created from script by getting it from the simulation:


CharacterInfo *pCharInfo = dynamic_cast (Sim::findObject("AvatarInfo") );

Long Answer:

WIP Article on the Torque Hierarchy
#7
01/26/2006 (7:02 pm)
I've tried "pCharInfo->registerObject("AvatarInfo");" and "CharacterInfo *pCharInfo = dynamic_cast (Sim::findObject("AvatarInfo") );" both.
But both gave me Access violation error.

I tried to find the reason, and it seems like the reason for it is because "Con::init()" were not called from anywhere. As I'm afraid I don't fully understand the engine, could you tell me where I have to add that init() function?

Thanks in advance.

Hong Jin
#8
01/27/2006 (5:43 am)
Could you go ahead and post the full new class implementation (.h and .cc)? There are so many things that -could- be wrong it's just hard to try to step through them.

If you aren't comfortable with posting your full implementation for whatever reason, feel free to email them to me (my email is in my profile), and I'll try to take a look.
#9
01/27/2006 (8:00 pm)
Dear Stephen,

I sent a e-mail as I made too many changes to post here.

Always thank you!

Hong Jin
#10
01/27/2006 (8:47 pm)
Not a problem--hope my responses helped, and if they did we'll summarize here and/or point to the TDN articles that are appropriate for the solution so others can share.