Run player controller script on server

DallasOnFire
edited July 2015 in Photon Bolt
Hello :D How can I run a player controller script on the server?

Best Answer

Answers

  • stanchion said:

    Not sure what you're asking, but if you want something to only run on the server you do if (BoltNetwork.isServer)

    It was for making the script below to run on the server. So if a user speedhacks, frames are not affected. How should I convert it? Thanks :smiley:


    using UnityEngine;

    public class AuthoritativeSystem: Bolt.EntityBehaviour {
    const float MOUSE_SENSITIVITY = 2f;

    bool _forward;
    bool _backward;
    bool _left;
    bool _right;
    bool _jump;

    float _yaw;
    float _pitch;

    PlayerMotor _motor;

    void Awake() {
    _motor = GetComponent();
    }

    public override void Attached() {
    state.Transform.SetTransforms(transform);
    }

    void PollKeys(bool mouse) {
    _forward = Input.GetKey(KeyCode.W);
    _backward = Input.GetKey(KeyCode.S);
    _left = Input.GetKey(KeyCode.A);
    _right = Input.GetKey(KeyCode.D);
    _jump = Input.GetKeyDown(KeyCode.Space);

    if (mouse) {
    _yaw += (Input.GetAxisRaw("Mouse X") * MOUSE_SENSITIVITY);
    _yaw %= 360f;

    _pitch += (-Input.GetAxisRaw("Mouse Y") * MOUSE_SENSITIVITY);
    _pitch = Mathf.Clamp(_pitch, -85f, +85f);
    }
    }

    void Update() {
    PollKeys(true);
    }

    public override void SimulateController() {
    PollKeys(false);

    IPlayerCommandInput input = PlayerCommand.Create();

    input.Forward = _forward;
    input.Backward = _backward;
    input.Left = _left;
    input.Right = _right;
    input.Jump = _jump;
    input.Yaw = _yaw;
    input.Pitch = _pitch;

    entity.QueueInput(input);
    }

    public override void ExecuteCommand(Bolt.Command command, bool resetState) {
    PlayerCommand cmd = (PlayerCommand)command;

    if (resetState) {
    // we got a correction from the server, reset (this only runs on the client)
    _motor.SetState(cmd.Result.Position, cmd.Result.Velocity, cmd.Result.IsGrounded, cmd.Result.JumpFrames);
    }
    else {
    // apply movement (this runs on both server and client)
    PlayerMotor.State motorState = _motor.Move(cmd.Input.Forward, cmd.Input.Backward, cmd.Input.Left, cmd.Input.Right, cmd.Input.Jump, cmd.Input.Yaw);

    // copy the motor state to the commands result (this gets sent back to the client)
    cmd.Result.Position = motorState.position;
    cmd.Result.Velocity = motorState.velocity;
    cmd.Result.IsGrounded = motorState.isGrounded;
    cmd.Result.JumpFrames = motorState.jumpFrames;
    }
    }
    }
  • Take a look at the tutorial, it already does this
  • stanchion said:

    Take a look at the tutorial, it already does this

    I though about adding [BoltGlobalBehaviour(BoltNetworkModes.Server)]. But would GetKeys work? or GetComponent?