Notifications
ShiroiFX: Allowing designers to create, and programmers to implement
Showcase
13
0
I've been working on a game for a while now, and there was one issue that I stumbled upon that was quite annoying to find a nice, decoupled solution. However, once I did, I instantly felt in love with it, and turned it into an open source framework, which all of you are welcome to use! (<3)
It it hosted on GitHub, where you can download a nicely packed DLL which contains everything you need, or compile it yourself. However, now I'll get into how it works.

Abstraction

Normalize you'd solve this with some simple abstraction, originally I had something like: public abstract class Effect : ScriptableObject { public abstract void Execute(Vector3 position); } And that solve that problem, for a while,.
The big problem, is that different effects requires different data, a particle effect requires a position to be spawned, but a screen shake effect does not, another effect may require more information than only a position. So how do you provide so many different data to different effects?
The solution are Effect Contexts.
An effect context represents the data that you provide to the effect at runtime for it to execute, and this data comes in the form of Features. An effect can make use of these features to make whatever they want to happen happen. And they can use
Now, an effect look like this: public abstract class Effect : ScriptableObject { public abstract void Play(EffectContext context); } Not much different, but much more powerful. Usage is as simple as doing:
public void Kill() { currentHealth = 0; onKilled.Invoke(); KilledEffect.PlayIfPresent(new EffectContext(host: this, new PositionFeature(transform.position)); if (DestroyOnDeath) { Destroy(gameObject); } }
Of course, there are drawback, the biggest one is that is if an effect makes uses of a feature that is not present in the context, an exception is thrown, which to be honest, I find quite reasonable.
Making use of these features is as simple as calling context.GetRequiredFeature<T>() // or context.GetOptinalFeature<T>() Where GetRequiredFeature throws an exception if there is no feature of type T, and GetOptionalFeature returns null.
ShiroiFX is still in active development, and if you'd like to help, anything from feedback to PR's or even merely using it are always welcome! Thanks for reading!

Bruno Silva Freire
Full Stack Programmer at LAJE Studio - Student
1
Comments