Game Development Community

Different Users

by Ted Lilljegren · in Technical Issues · 06/26/2007 (4:52 pm) · 8 replies

Sorry for asking such an basic qustion that im about to ask, but here we go:

A player logs onto to the server, and has a datablock asigned to him. The datablock has values dynamic depending on the user, say that the user has 150 health point wheras another user migh have 120.
Another player logs onto the server, he is asigned a datablock with 120 health points. The data is fetched from a db. The code would be something along the lines to:

datablock PlayerData(genericUser)
{
Connect to Db
Fetch the users healthdata
myHealth = data fetched from DB;
}

There are now two datablock called PlayerData, how do i tell them apart? i have tried using a variable inside the "()" thingies, so that:

$user = users id;
datablock PlayerData($user)
{
Connect to Db
Fetch the users healthdata
myHealth = data fetched from DB;
}

But Torque does not like that. Thanks for interest

#1
06/26/2007 (4:57 pm)
The Console tells me that it canot find the datablock, but if you canot use variables inside the () i dont realy know whats its good for.
#2
06/26/2007 (5:10 pm)
Datablocks are used for data which is shared between ALL instances of a particular type of object.
For example player.

If your players use the same mesh, animations, etc,
they should all use the same datablock.

Data which is specific to each individual player (eg HitPoints, Name, Guild Membership, Inventory etc) should be associated with the player object itself, not the player datablock.
#3
06/26/2007 (6:04 pm)
Also, the "PlayerData" portion is not the name of the (datablock) object, but the underlying c++ class that the datablock is a member of.

The name of the object is what is within the parentheses.

Finally, to reinforce what Orion said--datablocks are for static data that is shared between multiple objects--they should never be dynamically changed or created once the game has started in normal use.
#4
06/27/2007 (4:46 am)
Allright. Showed how much i knew =) So for dynamic data, i have to use objects, got it. Have to find some tutorial on this.
#5
06/27/2007 (5:16 am)
Thanks for the earliers answers.
Im sorry i keep posting retarded questions, but im very new on this stuff so u have to go easy on me:

//Create Player Object

%player = new Player() {
dataBlock = PlayerGenericstats; //States all stats that wont be changed during gameplay, like model mass n //friction
client = %this;

// dynmic values that can be changed
playerMaxHp = 150;
playerCurentHp = 150;

}

Now, this creates a UNIQUE object that is specific to one user only, if i got it right. And the %player variable could perhaps be a identifications number, so that i could create some kind of sessions which registers the players ID nmbr from a database on log in, give %player that number, and then apply changes to this object, for example lower the players curent hp by 20 or something?

What use is the Datablocks? When is it good to have static data? Does it save bandwidth? Am im suposed to use Datablocks all time when the data is static, and Objects when the data is dynamic? I guess there is some advantage with datablocks, otherwise they would not be there?
#6
06/27/2007 (8:00 am)
Ted - Yes the new player() code block you've posted is where the server side of torque creates an instance of a player object, assigns the initial datablock used by the player object and where you can add all of your dynamic variables relating to players like skills, current status, etc.

If I'm understanding your thoughts on sessions then you're not quite right with your understanding of %player, when you create an instance of a player object (with the new Player() call) it creates an object on the server side of torque and assigns it a unique id for that object on the server, the engine itself uses that unique id to track and use the object and you cannot change it's value - this is true for every object in the game - water, trees, skyboxes, players, ai players, buildings (open the world editor and click on something and you'll see it's 4 digit object id).

Now if you login with the same character again the object id you get could be different, so think of %player and this number as just the servers identification for your player this time of logging in. If you want to implement your sessions then in your database setup your character table with an auto-increment column in mysql that would be with something like:

CREATE TABLE characters (
  character_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  character_name VARCHAR(20) NOT NULL,
  password CHAR(32) NULL,
  PRIMARY KEY(character_id),
  UNIQUE INDEX characters_name(character_name)
)

Then when you insert a row into the database it generates your persistant character id for you, in Torque you just need to read that variable from the table when the user logs in and I'd suggest storing it above either as part of the player object i.e.

%player = new Player() {
dataBlock = PlayerGenericstats; //States all stats that wont be changed during gameplay, like model mass n //friction
client = %this;
[b]character_id = %value_read_from_the_db;[/b]

// dynmic values that can be changed
playerMaxHp = 150;
playerCurentHp = 150;

}

or as we have done for our game store it as variable in the client object - reasoning behind this is if you look at the process of a client connecting to the server it doesn't create the player object until after you've started all of the mission download stuff (datablocks, lighting, etc) and we want the user to login and be authorized before any of this stuff occurs.

Once you have that character_id stored on either your client/player object you can then use it to read/write/delete any data from the database and into the engine i.e player skills, stats, items, etc.


As for Static data it is always a good thing because you can rely on all clients having the same information and yes as you've guessed they save on bandwidth as they only need to be transmitted once, so anything that doesn't change per client i.e. MaxHealth, MinHealth, Weight, Max Carrying weight, friction, max run speeds, etc should all be in datablocks. Perhaps it's harder to see the benefit with a player but think of weapons - an M16 is an M16 - it's rate of fire, magazine size, weight, size, etc are always static.

And yes datablocks for static data and objects for dynamic.
#7
06/27/2007 (9:49 am)
This is usefull stuff. Thanks for the great input
#8
06/27/2007 (3:09 pm)
Any time Ted, it's a great community here at GG and so long as people have tried to search for an answer before creating a new post then I'll always try and answer if I can (somewhat of a newcomer to torque myself).