Hey.
I’m thinking about my game and his network system. I read many articles about network and the fact we have to be careful with the players who cheat. I’m not used to code network games so I have 2 questions:
-
RPC: I don’t know how to ask my question so I’m just gonna show you an example. On an GameObject (Character), I have 2 components :
-
PlayerController : handle inputs and is enable only for ONE character (OUR character)
public class PlayerController : MonoBehaviour {
private MyCharacterController _character;
Spell spell = new Fireball();
void Awake() {
_character = GetComponent<MyCharacterController>();
}
void Update() {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
// change the character position
} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
// change character position
} else if (Input.GetKeyDown(KeyCode.A)) {
if (_character.mana >= spell.cost) {
// Launch a Fireball to the target
// Spell.spellName is needed to know which spell is being cast
// target.photonView.viewID is needed to know who is targeted
_character.photonView.RPC("spellCast", PhotonTargets.All, spell.spellName, target.photonView.viewID);
}
}
}
}
- CharacterController : contains all data relative to a character (Health, Mana, whatever…) and all functions which will call an animation or an effect.
public class MyCharacterController : MonoBehaviour {
public float health;
public float mana;
public float speedMovement;
[RPC]
public void spellCast(string spellName, int targetViewId) {
Character target = getTargetByViewId(targetViewId);
Spell spell = getSpellByName(spellName);
// Apply effects of the spell
spell.effects();
target.takeDamage(spell.damages);
}
public void takeDamage(int damages) {
// do some maths and check (is the player dying ?)
// the change the character life
setHealth(health - damages);
}
public void setHealth(int newHealth) {
health = newHealth;
}
}
When you connect to my game, the game creates a “Character” for you. For each other players in the game, It’ll create a “Character” and disable “PlayerController” (because there should be only one PlayerController).
Now, let’s say I wanna cast a spell ; through my PlayerController, I’m gonna check if everything is OK (have I enough mana ? is the spell in cooldown ? etc.) and if so, I’ll call “castSpell” of “CharacterController” which is a RPC function. Every player is gonna call this function.
Okay, now here is my problem: when I call this function, castSpell will… cast the spell wanted by ALL player. So everybody is gonna call spell.effects which creates a Fireball G.O. (but it is NOT instantiate with Photon.Instantiate). And I’m not really sure if this is a good idea. But it works. However, the real “problem” is when I’m gonna apply the damages : I have NO idea how to do that… Currently, you can see that everybody is gonna call “target.takeDamages(…)” ; so it shouldn’t be a problem, all of them will have the target life updated (it works)… But I’m pretty sure this is not the good way… I was thinking about changing setHealth to a RPC function… but If I do that, I’ll have to change everything (cause everyone will call setHealth so this is stupid… or maybe should I check if photonView.isMine returns true ?).
I don’t know If I’m clear (maybe my english, I don’t even know how to explain that in my mother tongue ). I don’t want you to code something, I wanna know the way of how to refreshing data and applying animations/effects through the network.
-
Security: as you can see above, I don’t check what the player is sending (HE checks if he has enough mana to cast his spell)… I read somewhere that it should be interesting to have a “fake” player (launched by the server) who creates the game (so he is the MasterClient) and all RPC have to go through him… But I’m not sure if it is efficient… I mean, If I do that, when a player call a RPC function, he has to send his request to the MasterClient, then the MasterClient check if it is OK and finally, the MasterClient sends to all players the request… Instead of doing:
-
Client → Server → Clients
It does:
- Client → Server → MasterClient → Server → Clients
… Maybe i didn’t understand what they were saying. Lol.
Maybe I have a solution, but I want to be sure this is safe: when the player launches the game, the server is gonna check all the files inside the game directory… so if someone tried to change a file or add a file, the server is gonna say “No”. Good or bad solution ?
Thank you and If you need any information, feel free to ask me your questions!! I really want to understand all that stuff
Sbizz.