Hello Devforum! Today I was working on something I’ve admittedly never played around with too much, that being generic types. I implemented generics in a State Manager implementation for my personal framework, these being responsible for providing a template of data for the per-state and global shared data, which would theoretically let you have typechecking in the domain for developing Entity AI’s
Extra Information (About project & framework)
I’ve decided to go more in-depth about the implementation side to answer any possible questions, but just wanted to hide it.
This implementation (a module in my framework) provides a simple DSL-style experience to build Finite-State Machines, and there’s this value called the “environment” which allows different states and external script store & fetch data about the specific machine they represent. However, not having a dedicated way to provide typechecking makes it a little annoying to use, and I just love trying new things and so this is a great way to practice the use of generics! Now I’m just having a difficult time getting these generics to move between my objects that I create, in this post I specifically am going over my use case, an Entities object for the game I’m working on, however I’m trying to use as broad language as possible, however the stem from game → framework is still important.
Game → Framework (Seemingly fine)
Once implemented, I noticed the generics don’t fully propagate. The top layer (game logic) provides the types, but anything the framework creates loses them.
entity.Entity<{test: string}> = entity.new(script.Name, {}) → This generates a new entity with the table as TStEnv (Type State Environment)
From there, Entity generates a FSM and injects “Cortex” as TShEnv (Type Shared Environment)
The types.lua file then defines FSM as StateMachine<FSM_Cortex, TStEnv>, which is exactly what we see.
Access to Framework Data Issues

As you see in the screenshot, when you access environment through self.fsm (only accesses the shared environment), it provides the generic type of TShEnv!! (likely due to this line, not sure if I’m doing something wrong with it though, just isn’t propagating?)
When Entity creates a FSM it takes the TShEnv & TStEnv and binds them to the StateMachine instances that require them.
![]()

:hook()'ing a state lets you listen to different events in the StateMachine, and provides another type of environment for the individual state! This is why I also added
TStEnv, so you can template the data that may be persistent per-state, but this may be omitted for simplicity.
The important part of this though is how env.shared doesn’t provide the type we want, and just the generic TShEnv.
It’s 2:41 AM I gotta go to bed I really hope this all makes sense, I really do hope you guys are interested in this kind of stuff and we can have some good discussions and hopefully we can all make something really cool with this, do let me know if you need more info and thank you for reading, have a great rest of your day!








