Destructable Objects. In-bullet function or OOP setups?

If you’ve played any games over the last …
:stopwatch:
two decades?
You must have atleast once come into contact with “Destructable Objects”. These are objects littered throughout the game’s world;
Exploding Barrels,
Light sources (Lamps, Spotlights),
Computer screens,
Panels,
Basically it can be a ton of things that make you feel like you’re really able to interact with the world around you, despite only really existing in two states, working and broken.

I’m aiming to be able to have a series of objects through my game world that can be destroyed, but can unique assets can be destroyed in different ways; Barrels explode, lights stop emitting light and create electrical ‘sparks’. Typically when approaching something like this I have opted to make the break part of the bullet’s collision function, check if it is something like glass, or a barrel and then fire a series of functions following that.

This time around I’m thinking that I could perhaps hook all the objects up as an “entity” giving all destructable object models a Bindable called TakeDamage and fire that if a bullet detects it. By creating a .Event:connect() via OOP I will be able to set up appropriate damage functions, see if the object has health and reduce it, see if it has expired and break it.

One thing I’m wondering is is this overkill?

Edit:
TakeDamage is an object that the bullets are looking for already, the intention is just to minimize the amout of work on the Bullet’s code is doing.

1 Like

Eventually, this may cause a bit of server lag, but only if there’s hundreds of objects that have been destroyed that don’t despawn.
This seems like a great idea, but despawning as well as non-resource intensive effects are must-haves.
OOP would seem like a decent way to achieve this.

The idea is that these are largely already objects within the game, but their models are both visually and code-wise :Destroy()-ed and replaced with entities that are no longer rigged to do anything.
EDIT: Also, largely meshed objects. So a light will simply remove the light object and swap to a “Broken” version of that mesh. So an electical box on the wall just changes to a broken electrical box with sparks (particle emitters) coming out.

Ah, okay, I get you now.
Yeah, then maybe a combination of an in-bullet function as well as OOP would work better.
Maybe you could fire an event from the client to the object if the bullet hits it, then determine what happens to the object from a serverscript inside said object.

In general objects that are hit should be responsible for how they react to being hit.

This keeps your bullet code clean and lets you work on the destructible objects individually. But you don’t want to fragment your code or have it scattered in objects around your map.

Personally I have a master script for all objects of one type on the map (lanterns for example) which tracks each of their statuses (on, off, flickering, whatever) and on startup decorates each one with some Bindable functions and/or click detectors and/or connects events for various purposes.

And as always remember to never trust the client, if these changes can affect gameplay this master script should be server side only.

12 Likes

I’m using the client as my controller (Largely single-player/non gameplay altering effects) .
I was thinking of having a script act as the Initializer for for all my entities and seperate them all into different types. Lights would have one or two destruction types, Barrels another ect. Just a way of having one reference for every different destruction type and what kind of objects need it.

You shouldn’t really be handling bullet effects on the client. Do this all on the server and consider locally replicating it.

if you want shooting mechanics to feel good, you have to compromise between the client and the server being in charge. i prefer to preform hit detection on the client and then use sanity checks on the server to make sure that the client isnt trying to do anything unreasonable

1 Like

Same - I do some very simple vector math on the server to ensure the data from the client is valid and the ray conceivably could have hit the client

I’d just like to point out v quickly that I’m making a single-player game, so the client is in charge of litterally everything. That being said;

I’ve used this before and believe I’ll continue to do so if I want to hook up a multiplayer element

1 Like