02 - Interactions Everywhere

Hey Everyone,

It’s been two weeks since we began work on our capstone project “Work In Progress” and it’s already turning into something of a journey. You might not realize it initially but the thing about building a game centers on the players interactions with basically everything is that you then need to build a system to handle interacts with basically…. everything. So for this past week that’s what I’ve been working on.

So I knew I wanted to build a fairly scalable system that could account for future interact types which means I had to account for a couple of things. So I slapped together a rough chart that indicates which items would need to interact with each other.

So the ideal approach here is one that avoids the use of the physics system in favor of a math-based approach and thanks to some guidance from our mentors. I began with an abstract class called “InteractableBase” which all items that CAN be interacted with would inherit from. The idea here is to use this class to store all intractable items in a list inside a new component, the “InteractablesManager”, that we can then use to build an actual system of interactions on top of. This also allows me to handle interactions differently in different objects that implement “InteractableBase” by creating different logic based on how each item is supposed to work.

So what’s the point of all this? Well without getting into the weeds of each and every method that supports this system the ultimate goal here is to use the stored list of “InteractableBase” in the “InteractablesManager“. Since I now have all of these references stored I can now iterate through this list, do a bit of math and find the closest object to any arbitrary point, I can also then check to see if it within an acceptable range and then handle an interaction for the item.

So I can facilitate this check by wrapping it all in a neat function and since it’s being called on a button press instead of update its impact on resource usage is fairly limited. So just like a recipe we throw in a guard clause, a foreach loop, a few if statements, a couple of conditionals, and presto we have ourselves a useful method. So the last piece of the puzzle is how is this all being used to actually interact with an item.

SO… on Update() in the player we can grab that closest item, filter out anything we don’t want and then interact with it. The astute among you may have noticed that our “InteractableBase” abstract class enforces the implementation of the “OnInteract” method. Since we know we are searching for objects that implement this abstract class we can be certain that these objects have this logic implemented and then call it to handle that interaction. While this code is not the perfect implementation and can be improved in a couple of places it’s a good start.

And just like that we have ourselves the bare bones of an interaction system that does not rely on the Unity physics system. Now there are other problems that need to be addressed to make this system truly scalable. For example some of you may have noticed that in Update() in the above code block we are filtering out objects with the “Belts” component and we will get to that… next time.

Thanks for following my rampant train of thought. If you found this even remotely interesting then make sure to plug in your hyper advanced toaster we call a computer into that information superhighway some refer to as the interweb and tune in next time for the next installment of my Capstone Dev blog.

Previous
Previous

03 - Conveyors Are For Cool Kids

Next
Next

01 - A Work In Progress