UNITY: Synchronizing variables across a network - unity3d

Ok, so I'm trying to implement networking for a school project. (Part of my team's final project)
It's supposed to operate on a turn-based system. IE, when a player is done, they hit "next turn" and they lose control, while the other player gains control.
So, to test my networking and get the basics going, I've made a little program that spawns a cube whenever a server is joined or created (so one cube for each player), and each player is able to move only their own cube.
So far, I can have the cubes moving around and their transforms are perfectly synced up on both instances of the game simultaneously, and each player can only control their own cube.
However, when I click "Next turn", the turn only changes on ONE of the instances.
According to this, it SHOULD be watching the turn change:
And this is my turncontrol code (It's a little sloppy for now until I find a hardcore way to set the code's player number depending on whether they're the host or client)
How the heck do I make sure that when I click "Next Turn", the turn changes in both instances, and how do I make sure that the cube's player number is dependent on whether they're the host or client?
That and I have the very strong suspicion that the assignment of a player's "player number" (IE, whether they're player 1 or 2) is being assigned locally (Not reflected across instances) as well, since each player can only move when the turn is set to 1 (Meaning that when both players spawn, they're both set to be player 1. :s)
Any assistance would be appreciated. Networking is completely new to me and I'm wigging out trying to wrap my head around it.

Related

Client side prediction with a Shared object(Player - Ball relationship)

Our team is developing a football game. But we are stuck in a step.
We can't move forward because of this.
When we make client side prediction, the local player plays on the current time.
But the other objects like balls and other players play in the past(due to latency).
Normally for other objects we use interpolation but this doesn't work for the ball because local player interact with it while moving and it causes the other players to intertwine with the ball.
We tried to take the ball to the current state simulating physics and run all work-flow on the local. But this time when other players interact with the ball, they seems like moving the ball from far behind. Because of the other players in the past.
What is the best method to use for this situation?

Unity Photon Player Instantiation

I have a SteamVR Unity project which I'm converting to multiplayer.
When another client joins the game, instead of two different players seeing each other, each player has it's own version of the game where he controls all fo the player instances.
For example, while one player is connected everything is fine, but when a second player joins, the game just adds another Player prefab which the first player controls as well.
I tried replacing the Player with a simple cube and everything seems fine.
both the Player and the cube have Photon Transform View and Photon View scripts.
I would appreciate any help I can get.
This is a common problem, when you start with PUN. You probably setup a player prefab with network synchronization and instantiate that for each player. All scripts on the instances will act on the local input, which is what you see now.
You want two variants of the prefab, for local and remote representation. As it's impractical to always configure two prefabs, instead you build one which initializes itself (in Awake or Start) as local or remote. Your scripts should check the object's PhotonView if it's .isMine or not.
This can be done per Component (in each distinct script) or you could add a component which enables/disables the scripts on a GameObject, depending on isMine.
The Basics Tutorial does this, for example.
Unity doesn't know if it's multiplayer or not. When you give an input all of the scripts who are waiting for input takes it and behaves accordingly. To solve this basically create another player that doesn't take any input and spawn it for other players.

how to sync GameObject variable in photon network?

I am making Network game using Unity. I want to configure my network using PUN2. In particular, I notice that I can synchronize several basic variables through the OnPhotonSerializeView () method. However, this does not include custom classes such as GameObject and Transform. So what should I do to synchronize the Target Transform in that inspector view?
There are a few ways to tackle this. It depends on what you need this target for, and if it makes since for the object to exist on the other clients if it's not being targeted by any object.
Suppose you have Player that is synced, and a blue ball and a red ball in the scene.
The player has a ball that they are "holding". You want to trigger a synchronization so on all clients, if the player is holding a red ball it will hold it for all clients, and if they hold the blue ball it will do so for all clients.
The problem linking the blue ball in one client to all the blue balls in everyone else's client.
Option a) Give each ball a name and have some static dictionary mapping their name to the object in the scene (you can make them add themselves on the Awake or Start of the ball). Now you can synchronize the name of the ball that a player holds (a string in oppose to an object). When a client gets the name change, you can handle the name change as looking it up in the static dictionary, and setting it as the ball being held.
Option b) Synchronize the player AND the balls. Anything logic-related will only happen for the current player. So if the local player holds a ball, the player position will be synced and the ball position will be synced but nothing else will happen for the other players. This of course assumes this works for whatever you are trying to accomplish.
Option c) Serialize the ball. Send the data which describes the ball (size, color, etc) and create a new instance on the client side. This will NOT be tied to any existing balls on the client side. You may need check and destroy whichever ball is already being held. This is probably the least effective way to accomplish this, performance-wise.

Controlling NPCs throughout the plot line of a game

I'm trying to figure out a good way to script the NPCs in my RPG. A sample NPC interaction could go something like this:
NPC starts dialog #1 with player.
When the dialog is finished, the NPC moves to a waypoint on the map.
Once the NPC arrives at the waypoint and the player talks to him again, he starts dialog #2.
At the end of the dialog, the NPC asks a question.
If the player gives response A, the dialog ends. In this case, talking to the NPC again starts dialog #2 again.
If the player gives response B, the NPC gives an item to the player, and disappears. From now on, that same NPC will be present in a different Unity scene.
I've found plenty of examples of making a dialog tree, but I can't find a good way to handle complex situations like that. One of the most challenging problems is to determine which scene -- and where in the scene -- that NPC is. Depending on how far along the player is in the game, that NPC could be in any one of many different scenes, and will have different dialog and behavior.
Since Unity makes it easy to attach a script to my NPC's game object, I could of course do this all through a C# script. However, that script will get pretty big and messy for important NPCs.
The path that I've gone down so far is to create an XML file. Something like this:
<AgentAi>
<ActionGroup>
<Dialog>
<Statement>Hi!</Statement>
<Statement>Follow me.</Statement>
</Dialog>
<MoveTo>Waypoint_1</MoveTo>
<SetNpcState>NpcGreetedPlayer</SetNpcState>
</ActionGroup>
<ActionGroup>
<Conditions>
<State>NpcGreetedPlayer</State>
</Conditions>
<Dialog>
<Statement>Here, take this.</Statement>
</Dialog>
<AddItem>Dagger</AddItem>
<MoveTo>Waypoint_2</MoveTo>
</ActionGroup>
</AgentAi>
That sample would cause the NPC to greet the player and move to another spot. Then when the player talks to him again, the NPC will give the player a dagger and move to another waypoint.
The problem with the XML is that I'm worried about it growing very large for important NPC that can be in a lot of different places depending on where the player is in the game. I'd have to keep dynamically determining which NPCs are in a scene each time I load a new scene. I'm not totally against doing it with XML like this, but I don't want to waste a bunch of time heading down this road if there's a better way of doing it.
Since this type of behavior is common in a lot of games, is there a good way of doing it in Unity without having to homebrew my own complex system?
Normal software systems would use a database, once the level of complexity gets too high.
I'd setup the storyline with a numeric reference, like the pages of a book.
If they go to a higher number without interacting then the interaction is still available.
Then you can setup each interaction as a separate thing, with a start and finish number (not available before and not available after).
maybe you could do this by making the xml files separate, but I'd think you still need to tie them into the storyline.

Anti-Cheat / Glitch in game

Currently I am working with a team on a Unity-based game. The game is still in development and alpha version.
Recently, we saw that the game was vulnerable to Cheat Engine, speedhack etc. Updates after updates, cheats are now stable. We also introduced the ACT or anti Cheat Toolkit of Unity. As the game is Unity-based, it is easy to implement ideas in the game.
Though "hacks" are stablized, "glitches" are not.
This is an open world Survival game and it consists of picking/dropping items. The glitch is that when two players pick the item together, (currently you have to press E while the crosshead is over the item to pick it up) the item gets duplicated. We have been working DAYS to fix it, but no fortune.
We introduced that a player cannot pick up an item when there is another player nearby. It looks odd and we want the game smooth. We also tried auto pick up item. That's our plan, but are there any more ideas what we can do?
If your concerns are players cheating by modifying memory values, as well as maintaining a synchronized game state to avoid problems like item duplication, you should look into setting up an authoritative server that will contain and update the "official" values and state of the game.
Basically, rather than storing values and performing actions directly on the player's computer, the game will send a request to the server of what it wants to do, and the server will perform the actions, update the official game state, and send the new state back to the player so their game is updated.
This will prevent memory editing because even if a player modifies a value on their screen (such as currency or health) the server contains the true value.
It will also prevent exploits like speedhacks, because rather than having the local game directly move the player when a key is pressed, the keypress will just send a movement request to the server, which will update the player's position, and send back the new position.
Finally, this will prevent item duplication, because when both players attempt to pick up the item, they will both send an item pickup request to the server. Whichever player's request arrives first will receive the item, then the server will update the game state so that the item is no longer on the ground, and the second player's request will be ignored, because the item they're trying to pick up no longer exists.
Simply put, the best way to prevent cheating is: Don't store important values or perform important actions on the player's computer.