Quick rundown of my game there are these bots you can program with “logic chips” there are different kinds of chips. Each chip can be placed into slots in the Brain class which is the master class that allows the bot to function.
worldReader - Can read/get variables from the world such nearby ore position, home base, current location, etc
botReader - Same as worldReader but only variables inside the bot, health, inventory, etc…
math - Can do math operations with reader chips provided
memory - Can store numbers to be used in math operations
botWriter - Same as reader except sets/writes variables to the bot.
Now my question is how I should go about programming all these things I’ve thought of a few ideas
Make each logic chip a subclass of a main chip class that way I can access them from a script just by referencing the parent class and create and abstract function for each individual chip logic.
Make each chip it’s own script and just have a master class that acts as a database for all the chips.
Some things to keep in mind these chips are gonna be things I need to draw to the screen so that the player can drag them into slots that each bot has available.
I’m just brainstorming here not asking anyone to write any scripts for me I’m pretty experienced I just wanted to hear what others might think I should do
The 2 ideas you mentioned both sound reasonable. You could also consider using the strategy pattern for the chips. Although it looks like you want interactions between these different chips, so maybe the mediator pattern could be another option.
I’d probably end up with a ‘strategy’ pattern as well like Doug_B suggests. This results from my preference for interfaces, though it ends a wrench into dealing with the Unity serializer if you need that (you just need to write your own custom inspector around it).
Something along the lines of this (just roughly tossed together):
public interface ITickChip
{
//Tell the configuration to update
void Tick();
}
public interface IReadableChip
{
float Read();
}
public interface IWritableChip
{
void Write(float input);
}
public class AdditionChip : ITickChip
{
public IReadableChip InputA;
public IReadableChip InputB;
public IWritableChip Output;
public void Tick()
{
if(Output == null) return;
float a = InputA != null ? InputA.Read() : 0f;
float b = InputB != null ? InputB.Read() : 0f;
Ouput.Write(a + b);
}
}
public class MemoryChip : IReadableChip, IWritableChip
{
public float Value;
public float Read()
{
return Value;
}
public void Write(float value)
{
this.Value = value;
}
}
public class WorldReader : IReadableChip
{
public string WorldVariable;
public float Read()
{
return ReadWorldVariable(WorldVariable);
}
}
public class DebugPrintChip : ITickChip
{
public IReadableChip Input;
public void Tick()
{
if(Input == null) return;
Debug.Log(Input.Read());
}
}
I like the concept of the interface strategy pattern. I’ve honestly only used interfaces about once or twice so I’m not too knowledgeable about how they work.
Reading and Writing are absolutely two main methods are gonna be running in most chips depending on what they are.
I’ve always been more of a scriptable object kind of guy but programming the logic for each chip could be tedious that way unless I used an enum to specify the chip type but I don’t think that would be efficient.
As long as I’m able to easily access each chip and it’s methods and be able to add futures types of chips easily I suppose I could give the strategy method a try since the example it gives does kind of explain what i’m trying to do on a larger scale.
Never knew a site like that existed for OOP practices thanks a lot guys