I’m currently building something where the player can shoot projectiles with random effects. eg, different functions called on collision, on spawn, while the projectile is active, etc.
Right now, I have the projectile’s data (size/shape, mass, damage, etc) stored as value types in an objectdata class. This is used to build the object, add material, rigidbody, etc. Now, I also want to store data saying “on collision, explode” and/or “while active, pull nearby objects” and such.
My first idea is to simply write a whole lot of functions for each possibility, and store a list of arguments (or null) in the objectdata instance for each projectile. Then, in the script attached to each projectile, I can call a function if objectdata has arguments for it. eg, When “OnCollisionEnter” is called, I can check if objectdata has arguments for a hand-written “Explode” function, and if so call “Explode” with those arguments. Then I just cycle through if statements for everything that could happen in “OnCollisionEnter”.
So if I wanted to generate a cloud that drains HP, I could spawn a gameobject trigger on the projectile. In “OnTriggerStay” I would have a function that decrements something by a float I pass to it every second. Objectdata would have to store a long list of possible things to decrement in an enum. Then in “OnTriggerStay” I would have to have a long switch statement to decide which stat to drain (stamina, health, mass, speed…).
I can’t help but feel there could be a better way. I’m doing everything in C sharp, and I’m very new to both C sharp and unity (and game dev). Maybe someone with experience in this kind of randomization/generation could help me out? I’d like to make sure this will be a decent solution before I get into it, since it’ll take a while to account for all the possibilities I want, especially since I’ll be hitting knowledge walls along the way.
Random weapons, projectiles, enemies, and effects are pretty core to my game, so I would love any help!