Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+5)

Yeah, that is one of the downsides of the State Machine pattern. Because each state fully encapsulates everything that object is doing in any one step, that means anything that is shared is duplicated.

I often write functions like "move_and_collide" which would handle gravity and collision and all that. These "helper functions" are defined globally and available in any state on any object. Sometimes I'll allow those functions to accept arguments such as "in this state the gravity is .2, where as in this state the gravity is .3" or have them return results such as "I've hit the ground or a wall" so that the state can react accordingly.