Chapter VI
Getting Input from Players
After that last chapter, how about we do something simple to let our brains recover. Let's go ahead and get the input (forces) from each of the clients. This is actually no different from a non-multiplayer game until we have to figure out how to send the forces to the server and have the server do the actually movement, which will be covered in the next chapter.
First, we need to add some skills to our players' entities that will hold each players current forces. So in our declarations under Skill Definitions lets add the forces for the 3 possible axises.
define armor_class, skill7; // armor class of ent
define force_x, skill8; // current forces for this ent
define force_y, skill9;
define force_z, skill10;
After that, let's create our input function. Add the following function below init_display().
function init_display()
{
txt_people_connected.pos_x = screen_size.x - 350;
txt_player_number.pos_x = screen_size.x - 350;
txt_number_of_players.pos_x = screen_size.x - 350;
}
function input_scan()
{
while (1)
{
// if player has been created get inputs for him
if (player)
{
player.force_x = 0; // set force to 0 until we get key input
player.force_y = 0;
// <w> or <cursur up> moves forward
if ((key_w == 1)||(key_cuu == 1))
{
player.force_x = 1;
}
// <a> or <cursor left> turns left
if ((key_a == 1)||(key_cul == 1))
{
player.force_y = 1;
}
// <d> or <cursor right> turns right
if ((key_d == 1)||(key_cur == 1))
{
player.force_y = -1;
}
// <s> or <cursor left> moves back
if ((key_s == 1)||(key_cud == 1))
{
player.force_x = -1;
}
// <shift> for run
if (key_shift == 1)
{
player.force_x = player.force_x * 1.5; // run is 1.5 times faster than walk
}
// The Code below uses 3DGS's force protocol
// get new force values (gruadually speed up force)
// uses more bandwidth until reaches min or max force
// but does work
//player.force_x = key_force.y; // get force x
//player.force_y = -key_force.x; // get force y
//player.force_z = 0; // force z always 0 for now
}
wait(1);
}
}
Not much explaining to do here. First we make sure the player exist (has been created or has not been killed).
if (player)
If the player exist, we allow the client to input data to move his entity. Our key commands are as follows.
<w> or <cursor_up> moves the player forward
<a> or <cursor_left> turns the player to the left
<d> or <cursor_right> turns the player to the right
<s> or <cursor_down> moves the player backwards
<shift> is used to make the player run
You will also notice I have left in but commented out 3DGS's force input protocol. You can also use this if you like. The main difference between the 3DGS force input and ours is that we immediately go to force = 1 when a key is hit, while 3DGS's forces gradually gets to force = 1 over time. I haven't studied exactly how 3DGS's code increases forces, but if you want to increase forces until it's maximum or minimum value is reached, I would suggest you test your code and get it so there is the minimal possible force changes until the player reaches the minimum or maximum force value while still keeping game play feeling like you want.
For instance, if you are increasing your force while the forward key is held down at force_x += .001 per frame you will be sending tons of packets (even how my transfer code is written - next chapter) because the forces will be continually changing over a long period of time. So if your game still feels good with force_x += .1 , you would be actually sending 100 times less data increasing/decreasing force at that rate compared to the force_x += .001. In our example we go directly to force_x = 1, which sends 10 times less that force_x += .1 would and 1000 times less data than force_x += .001 would. So you can see the importance of finding the point that game play feels good without over burdening the network.
Now let's add the call to our input_scan function from main() in the if(connection != 1) statement. If the connection is dedicated server we skip calling input_scan() because the dedicated server is not playing a character.
create_player(); // create player
input_scan(); // get inputs
I wish I could be there to see the astonished look on your faces, when I say, "That's it for this chapter."
Previous Page Contents Next Page