So, I am new to this forum, but a quite advanced developer. Still I am very new to Unity itself.
I am building a game where you’ll be able to brew your own potions. You can find recipes inside the game with instructions on how to make the potion and what ingredients to use. To brew a potion you can for example stir the cauldron, heathen it, ground the ingredient, cut the ingredient, etc.
So my question is, how would i make a script that will check if a step of the instruction is completed and in the right order, without having to create a new script for each recipe. I can figure it out when i’d have to create a new script for each potion but there should be a way to minimize that. Especially when there are over 200 potions to be created.
I am using SQLite as data storage for items, recipes, etc.
Sounds to me like a job for a C# delegate and event! See here for help: C# - Events
You can create a seperate public class InstructionCheckList that only has the task of remembering which instuctions have or have-not been completed (a simple array of booleans might work, or maybe you need a more intricate data structure to model the relationships between all the tasks and items etc, such as a acyclic directed graph).
For the role of delegates and events, you can create an event like
onInstructionABCDCompletion
using the code:
public event OnInstructionABCDCompletion onInstructionABCDCompletion;
, which in turn is an instance of:
public delegate void OnInstructionABCDCompletion();
Then you create methods like “CheckIfPrerequisteInsustructionsCompletedForABCD()” and “UpdateInstructionCheckList()” that are both “added” to your event onInstructionABCDCompletion.
Hope this helps at least point you in the right direction for now. If you don’t want to use C# delegates and events, you can use Unity’s own implementation: Unity - Scripting API: UnityEvent
UnityEvents are slower than C# events individually, but scale very well for more than two “listeners”. So it’s really up to you.
If it’s just doing things in a raw order it wouldn’t be that hard. Create an indexed list with each potion, the index is the order and the value is the thing being done. Create a function that reads from the list (I use JSON but if you have a SQL database you’ll need to convert it into a class through script) and every time the user inputs a new ingredient / step do a check to see if it matches up with the next one in the list.
If it is more complicated than that you will need to write separate functions for each potion, and at that point it would start to get more complex with a lot of code.