Creating a new player variable
by Chris Labombard · in Torque Game Engine · 04/14/2005 (2:56 pm) · 12 replies
I have created a new private variable in the Player calss but I have no idea if I did it write.... Can someone verify or correct this code so I know how to do it from now on?
EDIT: see below code posts...
Does this make it so I can declare zone as a dynamic variable in the editor for my aiMarkers, so that they will be assigned the zone value when they are made? Or would I call setZone(%marker?.zone) when I spawn the aiPlayers from the markers?
EDIT: see below code posts...
Does this make it so I can declare zone as a dynamic variable in the editor for my aiMarkers, so that they will be assigned the zone value when they are made? Or would I call setZone(%marker?.zone) when I spawn the aiPlayers from the markers?
About the author
I have been a professional game programmer for over 5 years now. I've worked on virtually every platform, dozens of games and released a few of my own games, including 2 iPhone titles and a title waiting release on Big Fish Games.
#3
04/14/2005 (3:28 pm)
If you look under the box where you type the text for your post, there is a link to a page explaining the markup codes you can use.
#4
04/14/2005 (3:33 pm)
It's [code.] [/code.] just take out the dots.
#5
player.h:
player.cc:
packUpdate():
unpackUpdate:
Player.h:
Player.cc:
Is that better?
Is this set up properly? i don't think it is, since I can't seem to get it to work (1758 is the id tag of one of my aiPlayers)
1758.getZone(1758);
from the console
or:
1758.getZone();
04/14/2005 (3:33 pm)
Thanx...player.h:
S32 mZone;
player.cc:
mZone = 1;
packUpdate():
stream->writeFloat(getEnergyLevel() / mDataBlock->maxEnergy,EnergyLevelBits); stream->writeInt((S32)mZone,13); // HERE IS MY ZONE return retMask;
unpackUpdate:
F32 energy = stream->readFloat(EnergyLevelBits) * mDataBlock->maxEnergy; setEnergyLevel(energy); S32 newZone = stream->readInt(13); setZone(newZone); }
Player.h:
void setZone(S32 newZone); S32 getZone();
Player.cc:
void Player::setZone(S32 newZone)
{
mZone = newZone;
}
S32 Player::getZone()
{
return mZone;
}
ConsoleMethod( Player, setZone, void, 2, 2, "Sets the current zone.")
{
object->setZone((int)(argv[0]));
}
ConsoleMethod( Player, getZone, S32, 1, 1, "Gets the current zone.")
{
return object->getZone();
}Is that better?
Is this set up properly? i don't think it is, since I can't seem to get it to work (1758 is the id tag of one of my aiPlayers)
1758.getZone(1758);
from the console
or:
1758.getZone();
#6
it spits out 1 like it should...
I then call
and
EDIT:
I set my setZone min and max args to 3, since it appears to be how they are doing it elsewhere.... and changed my argv index to
argv[1]
EDIT:
Tried argv[2] as well.....
EDIT:
Tried removing the cast to an int....
04/14/2005 (3:43 pm)
Ok.... I changed teh getZone min and max args to 2, that appeared to work... Now when I call echo(1734.getZone());
it spits out 1 like it should...
I then call
1734.setZone(3);
and
echo(1734.getZone());and it spits out something like 81244643....
EDIT:
I set my setZone min and max args to 3, since it appears to be how they are doing it elsewhere.... and changed my argv index to
argv[1]
EDIT:
Tried argv[2] as well.....
EDIT:
Tried removing the cast to an int....
#7
05/03/2005 (2:07 pm)
@Chris: Did you ever get this to work? If so, how did you do it? I am having a similar problem.
#8
There are two ways of doing it. One is a server side script variable that isn't the way you want to do it for multiplayer... That involves declaring a script variable in the player.cs file in the scripts directory.
then you can access it by calling
OR to do it multiplayer safe you add the mVariable to the header file and initialize it in the .cc file. . . then you pack and unpack it (I followed the examples of other things that were being packed and unpacked)
This is done in the (un)packUpdate() functions . . . That basically syncronizes it over the network.
Then you need console methods to get and set the variable. Which is this:
Though I cant remember if that was teh right arg value. . .
I used the script version, which worked very well for me, and since it is server side and I was usign it to trigger server side events like spawning AI, it was all I needed. My client never needs access to the variable so I didnt go the class route.. good luck to you.
05/03/2005 (5:26 pm)
Yes, I got it set up. There are two ways of doing it. One is a server side script variable that isn't the way you want to do it for multiplayer... That involves declaring a script variable in the player.cs file in the scripts directory.
$myVariable = 1;
then you can access it by calling
%player.myVariable
OR to do it multiplayer safe you add the mVariable to the header file and initialize it in the .cc file. . . then you pack and unpack it (I followed the examples of other things that were being packed and unpacked)
This is done in the (un)packUpdate() functions . . . That basically syncronizes it over the network.
Then you need console methods to get and set the variable. Which is this:
void Player::setZone(S32 newZone)
{
mZone = newZone;
}
S32 Player::getZone()
{
return mZone;
}
ConsoleMethod( Player, setZone, void, 2, 2, "Sets the current zone.")
{
object->setZone((int)(argv[0]));
}
ConsoleMethod( Player, getZone, S32, 1, 1, "Gets the current zone.")
{
return object->getZone();
}Though I cant remember if that was teh right arg value. . .
I used the script version, which worked very well for me, and since it is server side and I was usign it to trigger server side events like spawning AI, it was all I needed. My client never needs access to the variable so I didnt go the class route.. good luck to you.
#9
05/04/2005 (10:54 am)
@Chris: Thanks. I need to create the variable to modify the jump impulse, so I can charge the jump (like spiderman 2), so I am pretty sure I need to do it in code. The thing is I seem to get everything done right and then when I jump the program crashes, with a "Bad world box!" error from sceneobject.cc. I didn't even touch that file! So idk. Thanks tho for your help and posting your code.
#10
05/04/2005 (10:55 am)
Oh yeah, one more thing. I created the global varibale $currentJumpCharge in default.bind.cs. How do I access that in player.cs?
#11
05/04/2005 (5:46 pm)
Why did you place it in default.bind.cs ? Place it in Player.cs
#12
05/06/2005 (4:15 pm)
I put it in default.bind.cs because that is where i was doing all my work. I changed it to the player.cs editing the jump trigger, which seems to make more sense.
Torque Owner Dreamer
Default Studio Name