How do I Reduce Lag in Multiplayer and Connection problem

0 favourites
From the Asset Store
For problem solving, puzzles, bubbles, platforms and other adventures.
  • 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.

  • 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.

  • 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

  • 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.

  • Ashley, which are the ports required to multiplayer?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • 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

    [attachment=0:3u0zys99][/attachment:3u0zys99]

    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

  • thank you soo much.

    it's a bit hard to understand but i'll do my best :p

  • 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.

  • i already implemented Ashley example in my game and it's working, the only problem is the lag. also, i tested on the School wi-fi and the game works online now, it's weird, i cant get it working at home and i tried at like 4 different homes..

  • Unfortunately i don't think so. I had the same problem and ended up coming up with this solution. Pros and cons because Ashley's method is so much easier but you end up sacrificing functionality and speed for it. I tested by sending messages back and forth at first with x,y pos and noticed they were lightning quick so the lag has to be due to something else which i can only assume is CPU related to back end code linking the objects (the part that makes it easy).

  • so, I only need to the the part of the movement? So i understand i dont need to sync objects. So just send the positions and angles maybe?

  • the thing that makes more confusion about your example is the lot of using functions..

    i almost never use functions xD

  • troublesumIs your example still available ? I don't find it :(

Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)