Hi guys,
I always wanted to do my games with ease of expansion, like I don’t have to rewrite a lot of code from scratch all the time!
How can one design a system that has a core as its main functional part and serves plug-ins as expansions to the system?
Here is a real-life example scenario for clarification purposes:
I’m developing a 2D Platformer Framework that will need different behavior over time and for different projects (one has double jump, the other features tackling! etc.)
How do I develop a system that has a core that can process plug-able components like: "Ok, I’m the ‘JUMP’ component, the button to activate me is the ‘JUMP’ button, I’m this and I’m that and I can do this, I need that and blah blah…! Or I’m the double jump component, I’m the camera, etc.
Thanks for any help everyone
Guys, please…! Anyone?
Here’s a similar topic with no bright outcome!
Component-based design for maximum code re-usability – suggestions please
Any help is highly appreciated
Well for your example, you would just take your original script, rename it and add jump.
Basically if you make your scripts more general for key components and your game logic in different scripts they should be easy to reuse.
What you are describing starts to sound like Playmaker or Vizio - a building-block approach to designing behaviors. Of course, those do it with visual interfaces.
You are using the term “component” which is a term used by Unity, and could cause confusion, e.g. are you suggesting you create a single script that is ‘jump’ and drop that on the gameObject, which may eventually wind up having 20 script components on it. That does not seem like a good approach.
I am still learning also, so my answer comes from an intermediate level, but I would suggest the way to think about what you are asking:
– A single script can evolve over time - you do not have to write it from scratch. I have scripts that started out simple, and then I reuse code from them or build onto them, and now have ones that may have dozens of functions and coroutines in them that I can simply copy out and paste for re-use in other scripts. Your “jump” example could be a code snippet that is added into the other code - just write your code in an organized and modular way so that snippets can be added together easily.
– Static functions (I’ve been working with unityscript / javascript) can be declared - so for example if you use a function such as “FindNearestGameObjectWithTagThatIsInFrontOfThePlayer()”, you can have that in a separate script in your project and any of your other scripts can access it.
– when I develop a behavior that I think is successful and may be useful for other projects, I take some time to clean it up and make its functionality more versatile ( adding variables, error catching, etc.) and then I copy it into an archive I keep of handy code snippets so I don’t have to hunt through old projects.
– working with those methods I have developed script modules for AIs that are very versatile, wayfinding systems, etc., that I am able to re-use without a lot of fuss. I think you might want to organize your work on that kind of level rather than jump buttons and run buttons.
@TheRaider,
I don’t want to write code that just works, I want it to be reusable and independent. And I’m thinking about a systematic approach.
@priceap,
Yeah, sorry about the confusion! I’m meant the component design in Object Oriented designs not the ones unity uses.
What you’re suggesting is very right and should be kept in mind in every project but as I mentioned, I’m thinking about a systematic way to do things here. And yes you’re right about PlayMaker and Vizio, they’re pretty close to what I’m talking about.
I’m not just concerned about jump button or run button, if I were, I’d be nuts to make simple things this complicated!
I think a script responsible for other scripts would be very good approach to make an extensible piece of software/game.
In theory, we write a manager class and assign it to the player game object for example, then write other scripts that are responsible for different actions like jumping, running, dying, etc. They have to be identified to the manager class (like a common structure) so that the manager could give them the authority to make decisions for a part of the action. Imagine the manager script to be idle, it knows that when the jump button is pressed the flow of the program shall fall into the hands of the jump script, the jump script will check all its requirements and act accordingly and when its job is finish (e.g the jumping is done) the control of the player prefab will once again be given back to the manager class, if you press the arrow keys, the manager will go for the movement script attached to the player, it check if we’re on the ground or not and act accordingly and so on and so forth…
But the problem here is that how can I do that? A messaging system? Yeah, but that could be pretty slow I guess, specially on mobile platforms! Then how can I make things independent and have them work together? How can I make the movement script independent from the jump script and let the player move in the air? Because moving in the air needs cooperation between the jump script and the movement script and that will ruin the in-dependency, right?
ok, we are in agreement on that, but what you then proceed to describe is a manager script that would handle incoming needs by scripts controlling jumping versus running, and that is what I meant by “jump button or run button”. For it to make better sense, can you propose a different scenario where the modularization would seem more significant that jump vs run?
In any case, what you are describing really should be compared to systems like Playmaker - primarily because once you figure out how the scheme should work, then just as you say, you need to figure out how to manage how it all talks to each other, and then once you figure that out, how do you design the interface to it so that it is not more of a chore to use than just putting the code blocks in one script.
I’ve been replying because it seems like there is a shared interest in making something that works like a game-building kit within Unity (which we should ofcourse also consider a game-building kit).
But I think very good examples of this are seen with things such as Unity’s third-person controller that has tons of features that can be turned on/off and adjusted to get very different behaviors. I have done this also with AI templates - starting with whether they chase you or run away with gizmos to show proximity sensing, or if they will instantiate a powerup when dying, etc., and it is all in a single script with the variables in the inspector to turn on or off. Making a reusable template like that can be very rewarding and useful as well.
Yeah, sure, but first, let me ask you if you read the thread I posted in my second post or not?
And yes, but how can I make something like PlayMaker? It doesn’t seem to be an easy to understand system, right?
About the jump vs. run, you could add whatever you think is rational to be implemented in a platformer type of game, I don’t know, like get hit, being invincible, dying and as I mentioned before, the problem is how to make everything connect together on the fly? NOT hardcoding them in unusable pieces of bad written code! I mean, let’s assume that the player can run, jump and get hit (for now), now I want to make a script that when attached to the player, it will tell the HUD to take damage and show it… you know what I mean?
Yes I’m sure we’re talking about mangers here as well, but the problem is how to make them systematic? Like ok, I have an Obstacle manager that could be assigned to a game object, then I have dynamite script that I’d add to the obstacle to make it specific, or I could make the obstacle a wall to avoid, sharp spines to jump off and so on… but they are all still obstacles, enemies, player classes/managers, like inheritance where you have a parent class that you could inheritance and make it a specific kind of class