BasicState - Simple State Management

Oh my god. That is the easiest state management plugin I’ve seen.

1 Like

It really is! I sort of borrowed the “wrapping” method they use for BasicState. You’re basically replacing export default view(<React.Component>) with return State:Roact(<Roact.Component>) (didn’t want to name the method anything else as this module can be used both with and without Roact).

1 Like

Been looking at the documentation and wow this is so simple! I like how the methods follow the stuff from Roblox too! I will probably make my own version instead in the future, but for now this will serve my needs amaizingly :slight_smile:

1 Like

Updated: 13th November 2020

:sparkles: Delete Everything! (+Patches) :sparkles:

  • Introduced State.None, which allows for keys to be removed from the state table (similarly to Cryo). This is due to Lua unable to differentiate between nil and undefined, and therefore omits nil values from tables.
  • Added a new State:Delete method for removing keys from the state table. This is the same as doing State:Set("Key", State.None).
  • Patched State:Roact to prevent BasicState from updating a component’s state while unmounting. This should stop any warnings related to this issue you may have experienced.

For more information, visit the Documentation site.

The update is now live in the Library and available to download via GitHub.

1 Like

Whats the pro’s of using this over _G table?

I would personally recommend not using _G at all–if you had to share a table between scripts, contain it in a ModuleScript.

Here’s what BasicState offers that _G doesn’t as-is:

  • Type safety
  • Signals when content is updated
  • Deep merging (you can update tables within tables, etc)
  • Roact integration–Plug your data straight into Roact apps
  • Convenience methods for toggling booleans true/false and incrementing/decrementing numeric values

I have personally used it to:

  • Keep track of global state within Roact apps
  • Retrieve and store plugin settings (with the benefit of being able to add new setting options in an update without screwing up the user’s existing config)
  • Track users in my game when they enter/leave areas
  • Animate door models to open/close as players approach.

Also being a module, you can guarantee that your initial data is there to begin with; you don’t need to wait for it to be set by another script before you can use it, like you would have to with _G.

1 Like

Amazing! I’ll probably replace Rodux for this module in my game, since it is pretty simple. Less than two minutes reading this topic was enough to start using it. Thank you a lot for this, made my life way easier :+1:

1 Like

This is a :gem:!

I have one question though, which I think is pretty general to the topic itself (similar concepts are rather new to me, despite the amount of related sources I’ve read) - Is this something that would aid me in creating Finite state machines? Could I use this outside of UI scope to handle things like AI?

1 Like

You can definitely use it outside of UI, that’s for sure. I personally use it to handle player data (stats, inventory, etc.) and analytics on the server, such as what area the player is currently in.

I also use it for plugin settings, which is great, as you can call :GetState() and save the resulting table. The nice thing is you can add additional settings in an update, and it’ll preserve the user’s current settings, while using default values for the new settings.

As for an FSM, I honestly haven’t tried, so you’d just have to give that one a go yourself and see how it turns out.

1 Like

How do you handle replicating State data from the server to the client? I would like an alternative to Values inside Instances to handle replication of data.

I actually made a solution for this today!

GitHub
Asset Library

This is a module I’m going to be using internally very shortly. It’s currently undocumented (will document in the very near future), but a quick look through the source code should give you a few good ideas as to how it works. It’s very small, anyway!

1 Like

0.2.2

  • Added support for Kayak (rotriever.toml)
  • Implements __tostring metamethod (@Kevinwkz)
  • Unit tests for use with TestEZ

Sounds like a useful and lightweight alternative to Rodux.

A few concerns I have though:

  • This module lacks the :Destroy() method, which may cause memory leaks/performance issues due to unused bindables? (I have yet to dive deep into the internals of this module)
  • Re-usability? I tend to cache most modules I require into a single array/dictionary. So I would like to know if multiple uses of BasicState.new() will effect performance or not.
  • BindableEvents? I suggest using an useful module named Signal that imitates RBXScriptSignals for convenience.

Other than the concerns I’ve noted above, this seems like an excellent module to use.

0.2.3

What’s Changed

  • Add State:Reset() method, and State:SetState() now also accepts a callback by @Gaffal in #8
  • Added support for wally, as requested by @mkargus in #9

New Contributors

  • @Gaffal made their first contribution in #8

Full Changelog: 0.2.2…0.2.3

1 Like

This looks far more simpler than Rodux, and will be very useful in my friend’s bowling game.

1 Like

0.2.6

Due to me being dumb, we’ve skipped a few versions. There’s no changes to the module itself in this release; however, BasicState should now work correctly with wally :tada:.

Below is a summary of what’s changed between v0.2.3 and v0.2.6.

What’s Changed

New Contributors

Full Changelog: 0.2.3…0.2.6

1 Like

Loving it keep up the great work :1000000000:

1 Like

Could I do something like this?

local states = BasicState.new({ Hi = "Roblox", John = "Fisher" })

return states

yes, absolutely

you would then call states:Get("Hi") and states:Set("Hi", "World") (or :GetState() and :SetState()) from wherever you’re requiring the exported state object

just note that it doesn’t replicate across the client-server boundary

1 Like

Thanks for clearing it up, I really struggled because I couldn’t find much on BasicState!