Jump to content


Photo

Need a little help with my multiplayer game


  • Please log in to reply
3 replies to this topic

#1 Slasher_X

Slasher_X

    GMC Member

  • GMC Member
  • 70 posts
  • Version:GM8

Posted 10 August 2012 - 08:42 PM

Hi guys.

I'm in the process of adding multiplayer to this top down tank game I'm working on and I'm stuck at adding the turret object. I've got the x, y and image_angle of the base to send to the server and then to all clients and that works perfectly. I'm a newbie when it comes to 39dll but I do unterstand the basis. I'm using sabriath's tutorial as a base. I want to know how I would assign a turret object to the specific client's base object.

Here is the client:
obj_dll's CREATE event:
dllinit(0, true, false);

server = tcpconnect("127.0.0.1", 12000, 2);
if (server < 0) game_end();

//These set the old x, y and image angle coordinates
dx = -1;
dy = -1;
angle = -1;

dspid = ds_map_create();

obj_dll's DESTROY event:
if (server >= 0)
{
    clearbuffer();
    writeint(9);
    sendmessage(server);
}

obj_dll's STEP event:
if !instance_exists(obj_player) exit;
//These set the new x, y, and angle coordinates
sx = obj_player.x;     //the base x coordinate
sy = obj_player.y;     //the base y coordinate
sangle = obj_player.image_angle;    //the base image angle
if (server >= 0) && ((dx != sx) || (dy != sy) || angle != sangle) //This makes sure the coordinates are only sent if they change
{
    dx = sx;
    dy = sy;
    angle = sangle;
    clearbuffer();
    writeint(1);  //case 1 of BEGIN STEP event
    writeint(dx);
    writeint(dy);
    writefloat(angle);
    sendmessage(server);
}

obj_dll's BEGIN STEP event:
if (server >= 0)
{
    size = receivemessage(server);
    if (size == 0) game_end();
    if (size > 0)
    {
        code = readint();
        switch (code)
        {
            case 1:
                ic = readint();
                for (i = 0; i < ic; i += 1)
                {
                    iod = scr_translate_client(readint());  //the following script is used here
                    tx = readint();
                    ty = readint();
                    angle = readfloat();
                    if (iod != -1)
                    {
                        (iod).x = tx;
                        (iod).y = ty;
                        (iod).image_angle = angle;  //I get stuck adding the turret angle here
                    }
                }
                break;
            case 4:
                ic = readint();
                if ds_map_exists(dspid, ic)
                {
                    with ds_map_find_value(dspid, ic) instance_destroy();
                    ds_map_delete(dspid, ic);
                }
                ds_map_add(dspid, ic, -1);
                break;
            case 9:
                ic = readint();
                with scr_translate_client(ic) instance_destroy();
                ds_map_delete(dspid, ic);
                break;
            case 11:
                server = -1;
                instance_destroy();
                game_end();
                break;
        }
    }
}

This script is used in the CLIENT's obj_dll BEGIN STEP event:

This is the scr_translate_client script:
//scr_translate_client(id)
//  looks for id in our list
//  if it finds it, it returns the true id
//  if none found, it creates a new obj_others and adds it to the list

if ds_map_exists(dspid, argument0)
    return ds_map_find_value(dspid, argument0);
io = instance_create(0, 0, obj_others);
ds_map_add(dspid, argument0, io);
return io;

obj_player has general create and movement code and obj_others is all blank.

Here is the Server:
obj_dll CREATE event:
dllinit(0, true, false);

listen = tcplisten(12000, 4, 1); //listen at port 12000, max players = 4
if (listen < 0) game_end();

obj_dll STEP event
client = tcpaccept(listen, 1);
if (client >= 0)
{
    o = instance_create(0, 0, obj_client);  //would I add the turrent's instance_create event here?
    o.client = client;
}

if (client >= 0)
{
    clearbuffer();
    writeint(4);
    writeint(o);
    sendmessage(client);
}

obj_dll END STEP event:
ic = instance_number(obj_client);
if (ic == 0) exit;
clearbuffer();
writeint(1);
writeint(ic);
for (i = 0; i < ic; i += 1)
{
    oi = instance_find(obj_client, i);
    writeint(oi.id);
    writeint(oi.x);
    writeint(oi.y);
    writefloat(oi.image_angle);
}

for (i = 0; i < ic; i += 1)
{
    oi = instance_find(obj_client, i);
    sendmessage(oi.client);
}

obj_client:
obj_client CREATE event:
client = -1;

obj_client DESTROY event:
if (client != -1)
{
    clearbuffer();
    writeint(9);
    writeint(self.id);
    ic = instance_number(obj_client);
    for (i = 0; i < ic; i += 1)
    {
        oi = instance_find(obj_client, i);
        if (oi != self.id)
            sendmessage(oi.client);
    }
    closesocket(client);
}

obj_client STEP event:
if (client != -1)
{
    clearbuffer();
    size = receivemessage(client);
    if (size == 0) instance_destroy();
    if (size > 0)
    {
        code = readint();
        switch (code)
        {
                case 1:
                        x = readint();
                        y = readint();
                        image_angle = readfloat();
                        break;
        }
    }
}

obj_client RIGHT BUTTON event:
if (client != -1)
{
    clearbuffer();
    writeint(11);
    sendmessage(client);
    instance_destroy();
}

So where about would I add the turret stuff? I've got to the part of adding the turrets coordinates and turret angle to obj_dll's STEP event but then I get stuck at obj_dll's END STEP event at case 1 after if (iod != -1). As far as I know "iod" is set to the base object so I can't add another "(iod).image_angle = angle" for the turret.

Has anyone made a top down tank game and done something like this? Any suggestions?
I've added the entire client and server programs above. I don't mind sharing it as the only thing different between it and sabriath's tutorial is the addition of image_angle sending and receiving.

Thanks.

Edited by Slasher_X, 11 August 2012 - 12:21 PM.

  • 0

#2 youngjake

youngjake

    GMC Member

  • GMC Member
  • 87 posts

Posted 10 August 2012 - 09:06 PM

What I do is put all the objects on an array when they are created in the server. Then, I set up the same array on the client. When you send coordinates through the socket lead it off by sending the array index of that object so that the client and server both know which object you are talking about.
So if it is object 312 in an array of 1000 at position 11,19
writefloat(312)
writefloat(11)
writefloat(19)
or
writefloat(arrayindex)
writefloat(x)
writefloat(y)

You can also send other variables preceeding the arrayindex if you want to specify what array you are talking about or something
  • 0

#3 Slasher_X

Slasher_X

    GMC Member

  • GMC Member
  • 70 posts
  • Version:GM8

Posted 11 August 2012 - 12:27 PM

What I do is put all the objects on an array when they are created in the server. Then, I set up the same array on the client. When you send coordinates through the socket lead it off by sending the array index of that object so that the client and server both know which object you are talking about.
So if it is object 312 in an array of 1000 at position 11,19
writefloat(312)
writefloat(11)
writefloat(19)
or
writefloat(arrayindex)
writefloat(x)
writefloat(y)

You can also send other variables preceeding the arrayindex if you want to specify what array you are talking about or something


Is there any way you could be more specific? I tried this suggestion yesterday (to the best of my knowledge) and unfortunately couldn't get it to work properly. I've supplied the entire program above. Could you take me through the turret addition that you speak of step by step please?

Thanks for all your help :)
  • 0

#4 Slasher_X

Slasher_X

    GMC Member

  • GMC Member
  • 70 posts
  • Version:GM8

Posted 13 August 2012 - 02:13 PM

bump
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users