How do I Reduce Lag in Multiplayer and Connection problem

Get help using Construct 2

Post » Sun Jun 22, 2014 11:48 am

Im making a multiplayer game and its already working the multiplayer, for some reason it only works on LAN like the multiplayer examples.
The point here is that i have 2 problems:

1 - The multiplayer doesnt work online, only in LAN, when a friend tries to connect as peer and it says "attempting to stablish connection" and then he is kicked after some seconds. In LAN the game works fine. I also installed Hamachi and the game worked palying with my friend but I want it to play online without using hamachi.

2 - The other problem i have is that the peer input it's too laggy, it has a delay when i press down the buttons and only after some mseconds the object starts moving. I already tested the input prediction but it makes it worse... Only the peer is lagging the host is perfect.
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Post » Sun Jun 22, 2014 12:14 pm

As for the first problem, if you aren't using a server, make sure you're portforwarded and everything; that is likely to be the problem if you haven't.

The lag is probably (I'm basing this entirely on logic because I have little hands-on experience with netcode) caused by setting the peer position based on the "send input to server and retrieve" way of doing multiplayer. Best to avoid that as much as possible and just say to move to where the player now is, or move the peer to where the server thinks it is only sometimes.

Not sure how much sense that made. Maybe someone else can clear it up.
B
42
S
14
G
3
Posts: 165
Reputation: 5,115

Post » Sun Jun 22, 2014 12:35 pm

how do you mean portfowarded? i tested with different PC's and different networks and it never worked.. not even the examples by scirra..
as for the lag, yes, it's using that method to seend to the host the peer input information and then the position is retrieved.. its very laggy and i would like to do that other way if possible
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Post » Sun Jun 22, 2014 1:24 pm

Usually you need to port-forward to host on a PC, but I'm not sure how that applies to C2. If you can find the ports required (probably in the MP tutorial by Ashley) you can sign on to your router and allow that port. Otherwise you're IP is basically not going to work. I don't really know much about the multiplayer in Construct, but I'm pretty sure that applies to anything. It would explain why it only worked on LAN and Hamachi too.

I would suggest looking up how to port forward for your specific router. That could be far more specific and helpful than I.
B
42
S
14
G
3
Posts: 165
Reputation: 5,115

Post » Sun Jun 22, 2014 1:32 pm

@Ashley, which are the ports required to multiplayer?
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Post » Sun Jun 22, 2014 8:18 pm

The Multiplayer object is based on WebRTC, which does not use specific ports, it uses whichever it finds are available. See Multiplayer tutorial 1: concepts
Scirra Founder
B
398
S
236
G
88
Posts: 24,428
Reputation: 194,600

Post » Sun Jun 22, 2014 9:30 pm

@Ashley, I already saw the tutorial like 1000 times and i havent been able to fix my problem.
The multiplayer only works in LAN and i dont know why. I disabled the firewall, i opened ports, I can only put the game running online using hamachi with my friend.
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Post » Mon Jun 23, 2014 12:12 am

@joelbelo Per your message I wanted to post a new updated version of my multiplayer platform example that uses functions to perform multiplayer actions instead of synced objects to eliminate lag. I have commented the code as best i can to help better understand it but its still more complicated than syncing objects. The difference is that there is almost no lag when you do persisted function calling and simulated actions.

It supports unlimited players so just open as many browser windows as you want and click connect. Each player scrolls to them selves. Move them around using the arrow keys.

Note: you will need the moveto plugin by rex for the capx to work. I assume most people have that plugin by now :)
multiplayerExample.capx

How it works:
When "Player1" presses the left button instead of using the built-in behavior move i disable those on start and instead call a function "MoveLeft" and pass it a parameter of the peerid to move that simulates moving left for that player. At the same time i take the function parameters and the function name and package them into a dictionary. I then send the dictionary AsJSON in a multiplayer message to all peers. They load that JSON string back in to a dictionary on their end and then call the exact same function with all the same parameters that were sent in the dictionary. Both devices call the same function at near the same time.

Based on my experience function calling messages appear to be just as fast as synced variables but don't have any over head performing lag calculations. Its just save AsJSON and load AsJSON. The irony is the side effect of syncing objects looks like it induces lag by using a large amount of CPU performing the calculations to correct for lag. Function calling is just a straight call so you do all the lag correction your self.

To use as little bandwidth as possible I don't send the move left command every tick. I only send it once at the beginning (even though the user is holding down the move left key, the command is only sent once) and its repeated on the other end (persisted) until the player lifts his his finger off the key at which point a stop command is sent. This is accomplished by tagging the dictionary as a persisted action so when the peer receives it they lock it in and keep calling "MoveLeft" (even though they only received it once) until a dictionary message is sent with the same tag name but a different function telling it to stop. So for the full move event i send one dictionary telling it to move left. as soon as the player takes his finger off of the key i send a dictionary with a function to stop moving left and once the object has stopped moving i send the last dictionary with a moveto function call for the specific location to compensate for latency but ensure they are at the same position. 3 commands to move a player all the way across the screen with no lag using simulated functions. Now just to ensure its as accurate as possible while moving every 20-30 ticks or so the player will send a set pos command to account for any latency.

You will notice there is basically no lag and movement is smooth. I know that function calling is bit more complex but once you get used it it makes lag free actions very easy. Give it a try and let me know if it helps :)
You do not have the required permissions to view the files attached to this post.
Last edited by troublesum on Mon Jun 23, 2014 6:12 pm, edited 1 time in total.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Mon Jun 23, 2014 10:18 am

thank you soo much.
it's a bit hard to understand but i'll do my best :p
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Post » Mon Jun 23, 2014 11:25 am

but there isn't a way to change a bit Ashley multiplayer example in order to work without the lag for the peer? like moving the peer inside the peer and then update the position on the host.
B
16
S
3
G
1
Posts: 85
Reputation: 2,260

Next

Return to How do I....?

Who is online

Users browsing this forum: Microbex and 23 guests