How do I use turrets behavior in a multiplayer game

  • Hi,

    I'm trying to make a multiplayer game. BTW I'm not using the multiplayer object. I'm using websocket.

    Each player can create turrets. When a turret is created it should get another turret as target.

    If I try to use a single sprite object for everyone 's turret (they look identical but I can use a simple text to see who "owns" a turret) and the event:

    Turret -> Add Target -> Turret[/code:1esy9ver]
    Turret does not make difference from "my" turret and "enemy" turret (also the turret will self target).
    
    I can solve this by using two sprites objects; let say my turrets use the sprite "Turret" and enemy's turrets use the sprite "Enemy_turret".
    So I can do:
    [code:1esy9ver]
    Turret -> Add Target -> Enemy_turret
    Enemy_turret -> Add Target -> Turret
    [/code:1esy9ver]
    
    In this way my turrets should only target Enemy's Turrets and viceversa.
    
    But what if I want to have an undeterminated number (or a max of 5) of players?
    Should I clone all those sprites, events and condition? Is there (I think surely there is) a better way to do it?
    
    BTW I have an instance var in each turret with the player ID. But I can't find a way to use this as filter.
    Thank you!!!
  • Well with the instance variable and the palyer ID, you have basically all precomponents for a filter. :)

    You still need the EnemyTurret and the - let's call him - PlayerTurret- sprites.

    The sprites all look the same, but for every player, the own player is the PlayerTurret and the enemies are EnemyTurrets (yes, all the same).

    The way you can differentiate the different EnemyTurrets is by their instance var "PlayerID".

    You can target a specific enemy turret by putting a "Pick by comparison" condition before the target selection.

    This would look like this:

    Pick "EnemyTurret" by comparison: [whatever player id turret you want to target] =equal to= EnemyTurret.PlayerID
          PlayerTurret: Set EnemyTurret as target[/code:2vpc3rk0]
    
    What happens here is the following:
    [ul]
    	[li]the "pick" condition has the consequence that the PlayerTurret only sets the EnemyTurret as a target who has the PlayerID you specify in the "pick" comparison[/li]
    	[li]having two different sprites (which are essentially the same, but have different names and are no instances of the same object) will prevent the picking condition to exclude the PlayerTurret, so it can still target the enemy.[/li]
    [/ul]
    Now you just need the PlayerID of the turret you want to target. That's it.
    
    Hope I could help. :)
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for your aswer.

    Anyway I had more problems using the turret behavior.

    It should work if every player joins before the game starts. But if a player joins after others players it's a little more complicated. Because I send all existing turrets from the server to the new player so the client can create those. But I have to send the all turrets in the same order those were created. Because (I think) if there are two targets at the same distance, the turret acquire the first created one. And if I do not send turrets in the same order those were created, in a client the target can be different from anothe client.

    Also my clients create objects at runtime that are not created on others clients. So uid and targetuid can differ from client to client (i.e. because while a new client creates turrets that it receives from the server it can create another object on the fly addng +1 to the next turret uid).

    All those poblems surely have solutions. Anyway it's little too bit complicated.

    I have found a workaround in this topic:

    It's not using the turret behavior but storing custom turret IDs and target IDs client and server side makes clients be synced.

    Thanks again

  • You could also make the turret aiming server-sided.

    So that the server decides which turret aims at which turret. The server would then send each turrets TargetID to each player and they would then simpy "render" the view.

    Like that, the game would also be less vulnerable to be hacked.

    You could even go further and even make the turrets angles and everyting server-sided.

    This would solve the problems of players joining later since the client can update the number of players, turret angle etc. as often as he wants and can then send the data over to the players who then just have to "draw" the image.

    (And make their game decisions of course)

  • I'm trying the server side approach (just the first part in wich the server send each turret a targetID).

    It's workin in a 2 player game (player/enemy). I use:

    [ul]	[li]pick turred by turretID[/li]
    	[li]pick enemyTurret by enemyturretID[/li]
    	[li]turret add target enemyturretID.[/code:3dganqr1][/li]
    [/ul]
    The problem when it comes a third player. He saw all other's turrets as enemy turret. I can't use:
    [code:3dganqr1]
    [ul]
    	[li]pick enemyTurret by enemyturretID   <------[/li]
    	[li]pick enemyTurret by enemyturretID   <------[/li]
    	[li]enemyTurret add target enemyturretID.[/code:3dganqr1][/li]
    [/ul]
    I think because the the econd enemyTurret pick "overWrites" the first one. I think I should find a way to do something like
    
    [code:3dganqr1]
    [ul]
    	[li]pick enemyTurret by enemyturretID   AS A[/li]
    	[li]pick enemyTurret by enemyturretID   AS B[/li]
    	[li]A add target B.[/code:3dganqr1][/li]
    [/ul]
    Making all server side as you suggest in the second part of your answer could be a solution. But in this way I will not use the turret behavior at all.
    Maybe it's also better... I'm thinking about it.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)