Best way to decouple network events from my objects?

My combat framework so far looks like this:

  1. Player on client side makes attack
  2. Attack is split up into ‘phases’

However, the player must also tell the server that it started the attack, so there are of course events to handle this. The server then executes the exact same attack, but on the server.

Here is a UML Diagram that roughly outlines the process:

Note the events that communicate cross server-client here are:

  1. When the user starts an attack, and
  2. When the user continues an attack (used in, for example, chargeable attacks )

From a code design standpoint, however, this is super messy - ideally, I’d like to have it so that the attack itself has no knowledge about replication, and all network events are handled externally from the attack.

Is there any neat way to handle this in roblox?

1 Like

In this case, you will want to implement a design pattern known as the “Proxy” / “Surrogate” pattern. The purpose of the pattern is to abstract the implementation to protect the lower-level elements (the RemoteEvents) from the implementation, or potentially create a “mock” version for testing or if the real object is incomplete or too expensive to always instantiate.

About the Proxy pattern:

For this case specifically, what I would do is create a “proxy” class that handles each event type, which could simply be Network:StartAttack() and Network:SetAttackState(...), and potentially have others for specific cases, such as ChargeStarted() and ChargeCancelled().

2 Likes

How would I go about syncing this proxy between the client and the server, though?

The setup for such a proxy seems complicated and hard to manage.

I probably should have included more in terms of implementation in the original post. For this, you would want to create a proxy on the client and a proxy on the server. Here is a quick example of that as a UML:

The client sends requests through the client proxy’s functions, and the server would listen to a set of abstracted events. Something else that could be considered is creating server objects that implement their own ServerAttackObject interface and registering them with something that handles the events.

2 Likes