#Item state design pattern for a backpack hero/battles style inventory?

3 messages · Page 1 of 1 (latest)

sand willow
#

Hey! I'm trying to figure out an approach to storing and changing the state and values of my items placed on a grid inventory.

The items would have their own functionality and values, be affected by the type of tile they are on, the items next to them etc. I'd want the item to be capable of having as many states affecting it as possible, and for the states to interact synergistically ala. Binding of Isaac.

Just as an example, and item affected by two states, one that causes it to activate it's effect when moved a tile, and another that causes it to move in one direction in the inventory when used. So it creates an emergent synergy that makes it activate several times when used as long as it has slots to move to.

Is there some kind of programming pattern fit for storing these overlapping states, and have them effect the base item functionality? I know storing the states in the item script itself and having a nasty nested if statement probably isn't the way. And I feel a state machine wouldn't let me easily have the different states interact?

Thanks for the help for this kinda nebulous question!

sand willow
#

Currently I am as far as a match function that is iterated for every state in an array of item states. I can affect the item variables like this, but how about more complex behavior?

sand willow
#

So, how I am doing it right now:

I have a set_mod function at the inventory level, that iterates through all the item scenes present. set_mod gets the slots next to an item, checks if there is an item there, and sets modifications to the neigbouring items. It gets what modifications to add by going through a huge slab of match statements to match the item ID with what it should do.

then I call a get_mod function at the inventory again, that iterates through all the items, this time just adding the modifications to their pre-existing values.

then I read all the new values for the items.

seems to function alright, but I am a bit worried about having the huge match statement wall.

how does this seem to someone more experienced?