- Home /
Is there a way to connect scripts in a way where they can be assembled like building blocks?
Is there a way to take multiple scripts (probably a lot) and connect them to form sort of a tree? The principle is that there'll be a randomized choice between a few options, and there will be a long line of those, and the next one to run will depend on the outcomes of the ones previous. Another thing I'd like to do is be able to easily add "limbs" to this tree, and rearrange them if I need to. Is there any way to do this? Thanks in advance!
Yes there is. Based only on this vague verbal explanation i cannot say much more than that though.
I'm kind of a huge noob, so I'm sorry if my explanation doesn't make sense. What I'm trying to do is to make a system of randomization, wherein everything about the character gets randomized, from their appearance to their interests to their environment. The first part would be simple and vague, for example deciding whether the character was rich or poor, or somewhere in between. And then, depending on the result of that, there would be another one. (Let's say they turned out to be rich, and the next one would deter$$anonymous$$e what type of house they lived in) There would be a different set out outcomes for each outcome of the first question, and so on and so forth until there would be a large tree of outcomes. And then, later in development, I'd like to be able to add branches to this tree relatively easily, and move them around in their order without (much) difficulty. Sorry if this is still not enough information.
I understand the idea, but as you call yourself "a huge noob", this may be a little out of your reach. Object Oriented Program$$anonymous$$g, Interfaces, Inheritance, Polymorphism etc.. are all subjects you are going to run into. It takes time and study to understand how to apply and make this. Its not impossible dont get me wrong, but the best way to achieve this is to read about it and try it out.
https://unity3d.com/learn/tutorials/topics/scripting
This is a good place to get started i think. And to answer your question: YES!
This is kind of out of reach for an absolute beginner for the moment. I second what metalted mentioned.
Get the foundations of C# OOP (Object Oriented Program$$anonymous$$g) under your belt. Specifically understand these topics:
Inheritance, Polymorphism, Constructors and perhaps even Generics.
It sounds like a lot of hot garbage when you read that but really there's no reason you can't learn all of that within a few months to a year if you take it step-by-step. It all makes a lot more sense in sequence and builds one concept to the next.
While you might be a ways out to understanding and implementing this code, eventually you might want to check out my approach to combining C# OOP with Unity's Component-based system.
I created a tutorial for this. In this example I lay out enemy class variations that branch out in their differences using inheritance (movement patterns, properties like speed and size, etc). I then connect these different enemy scripts to a "master" class that would be something like:
Enemy<T> where T : Enemy
Which also creates a gameObject and attaches the enemy script where that "T" in the angled brackets is referenced at instantiation. Your instantiation code will look something like:
Enemy<Ghost> ghost = new Enemy<Ghost>();
The Ghost class would then expand on the base code (shared stuff) you find in the Enemy class. The inheritance tree for all that would be:
Enemy : $$anonymous$$onoBehaviour
Ghost : Enemy
You can then access the ghost's relevant properties, such as its speed, (or whatever else you want) with something like:
ghost.ScriptComponent.Speed = 5;
This overall approach I'm describing lets you also do everything (and keep track of everything) in code rather than in the Unity Editor. Programmers tend to like to stay in code :)
Here's that tutorial. Just keep in $$anonymous$$d, it will look like PURE nonsense right now. Do not take that to mean that it's difficult to learn. Good luck!
https://www.devu.com/free-videos/freebie-unity-constructing-oop-enemy-classes/
While it may be something of a contentious proposition, maybe you need to look outside Unity for a drag & drop scripting language. Depending on your needs, you could even go as simple as Scratch. I think Unreal Engine 4 has something along those lines too, if I'm allowed to mention that particular engine...