NetworkWeapon

Inherits: Node

Base class for creating responsive weapons, by spawning projectiles locally, but keeping control on the server.

Methods

Return Type Name
bool can_fire()
Node fire()
int get_fired_tick()
bool _can_peer_use(int peer_id)
void _after_fire(Node projectile)
Node _spawn()
Dictionary _get_data(Node projectile)
void _apply_data(Node projectile, Dictionary data)
bool _is_reconcilable(Node projectile, Dictionary request_data, Dictionary local_data)
void _reconcile(Node projectile, Dictionary local_data, Dictionary remote_data)

Method Descriptions

bool can_fire ( )

Check whether this weapon can be fired.


Node fire ( )

Try to fire the weapon and return the projectile.

Returns null if the weapon can't be fired.


int get_fired_tick ( )

Get the tick when the weapon was fired.

Whenever a weapon gets fired, it takes time for that event to be transmitted to the server. To account for this latency, the exact tick is sent along with other data, so weapon implementations can compensate for the latency.

One way to use this is to manually simulate the projectile after it's created:

func _after_fire(projectile: Node3D):
    last_fire = get_fired_tick()
    sound.play()

    for t in range(get_fired_tick(), NetworkTime.tick):
        if projectile.is_queued_for_deletion():
            break
        projectile._tick(NetworkTime.ticktime, t)

bool _can_peer_use ( int peer_id )

Override this method to check if a given peer can use this weapon.

Usually this should check if the weapon's owner is trying to fire it, but for some special cases this can be some different logic, e.g. weapons that can be used by any player on a given team.


void _after_fire ( Node projectile )

Override this method to run any logic needed after successfully firing the weapon.

This can be used to e.g. reset the firing cooldown or deduct ammo.


Node _spawn ( )

Override this method to spawn and initialize a projectile.

Make sure to return the projectile spawned!


Dictionary _get_data ( Node projectile )

Override this method to extract projectile data that should be synchronized over the network.

This will be captured both locally and on the server, and will be used for reconciliation.


void _apply_data ( Node projectile, Dictionary data )

Override this method to apply projectile data that should be synchronized over the network.

This is used in cases where some other client fires a weapon and the server instructs us to spawn a projectile for it.


bool _is_reconcilable ( Node projectile, Dictionary request_data, Dictionary local_data )

Override this method to check if two projectile states can be reconciled.

This can be used to prevent cheating, for example by not allowing the client to say it's firing from the other side of the map compared to its actual position.

When this method returns false, the server will decline the projectile request.


void _reconcile ( Node projectile, Dictionary local_data, Dictionary remote_data )

Override this method to reconcile the initial local and remote projectile state.

Let's say the projectile travels in a straight line from its origin, but we receive a different origin from the server. In this reconciliation step, the projectile's position can be adjusted to account for the different origin.

Unless the use case is niche, the best practice is to consider the server's state as authorative.