Quantcast
Download
(2 Kb)
Download
Updated: 01/25/22 04:04 PM
Compatibility:
Deadlands (7.2.5)
Updated:01/25/22 04:04 PM
Created:01/25/22 04:04 PM
Monthly downloads:1,444
Total downloads:5,095
Favorites:1
MD5:
LibPreferences  Popular! (More than 5000 hits)
Version: 0.1.0
by: algwyn [More]
Convenient character and account wide saved variables management.

Many configurable add-ons offer players the option to set them either at a character and account-wide level. ZOS provides quite a few utilities to interact with saved variables and create both of those easily, however it is up to the add-on author to manage multiple tables and switch them accordingly. This can be inneficient and lead to errors, or even less performatic code due to conditional checks everywhere.

LibPreferences leverages Lua's metatable mechanism to provide a transparent way to create and use variables that can be switched between character and account-wide. It replaces the standard ZO_SavedVars methods with a single call to create the tables for you.

Usage

Lua Code:
  1. -- syntax: LibPreferences.New(savedVariablesTable, defaults, options)
  2. preferences = LibPreferences.New("ExampleVars", {
  3.   foo = "corge",
  4.   bar = "grault",
  5. }, {
  6.   version = 1, -- ZO_SavedVars methods' version parameter
  7.   -- namespace = nil, -- ZO_SavedVars methods' namespace parameter
  8.   -- profile = GetWorldName(), -- ZO_SavedVars methods' profile parameter
  9. })
  10.  
  11. d(preferences:IsAccountWide()) -- false
  12. d(preferences.foo) -- corge
  13. d(preferences.bar) -- grault
  14.  
  15. preferences.foo = "garply"
  16. d(self.preferences.foo) -- garply
  17.  
  18.  -- switch to account-wide settings
  19. preferences:SetAccountWide(true)
  20. d(preferences.foo) -- corge
  21. d(preferences.bar) -- grault
  22.  
  23. preferences.foo = "waldo"
  24. d(preferences.foo) -- waldo
  25.  
  26.  -- switch back to character-specific settings
  27. preferences:SetAccountWide(false)
  28. d(preferences.foo) -- garply
  29.  
  30. -- you can also access directly the character or account preferences despite the current toggle
  31. d(preferences.character.foo) -- garply
  32. d(preferences.account.foo) -- waldo
  33.  
  34. -- the account-wide toggle state is saved as a preference on the character object only, so it gets persisted
  35. d(preferences.character.accountWide) -- false
  36. d(preferences.account.accountWide) -- nil

Limitations
  • you can't have a preference named account, character, target or accountWide

Utility methods

The table returned by LibPreferences.New has a metatable assigned that provides these utility methods:
  • IsAccountWide(): returns true if the preferences are set to account-wide
  • SetAccountWide(toggle): changes the preferences between account-wide and character-specific
  • Getter(name): returns a function that, when called, retrieves the value of the preference specified
  • Setter(name): returns a function that, when called, sets the value of the preference specified (it expects a value as the first parameter)
  • IsEqualGetter(name, value): returns a function that evaluates if the current value of the preference specified matches the value
  • AccountWideGetter(): returns a function that retrieves the current account-wide toggle value
  • AccountWideSetter(): returns a function that changes the account-wide toggle value (it expects a boolean parameter)

As a bonus you can get all those methods and typings on your coding environment if your IDE has support for EmmyLua-style docstrings, such such as the EmmyLua JetBrains plugin or the sumneko.lua VSCode extension.
Optional Files (0)


Post A Reply Comment Options
Unread 01/26/22, 03:36 AM  
algwyn
AddOn Author - Click to view AddOns

Forum posts: 0
File comments: 1
Uploads: 1
Same same, but different.

Originally Posted by Baertram
Thank you for that library.
Though I'm not fully understanding yet if this is different to LibSavedVars or not.
Thanks for approving it

They both try to solve a similar problem, however they have a distinct fundamental approach.

LibSavedVars focus more on abstracting away the control and management of the underlying saved vars objects by providing side-effects. It introduces custom "classes", protection mechanisms and methods for migration, versioning and renaming settings. LibPreferences is less invasive in the sense that it provides both account and character saved vars as vanilla as possible, and the main preferences is nothing but a proxy. So it does one thing, and does it well: allows users to use a single table for settings just like they would with the original object from ZOS_SavedVars. All functionality provided by its methods can be reproduced directly on the resulting table due to no side-effects.

It also diverges from LibSavedVars premisse of fallback values, as account and character values are completely independent, and only share the same value during creation.

Finally, LibPreferences provides utility methods to allow integration with other add-ons, such as the getter and setter returning functors. These are agnostic and, unlike LibSavedVars ones that targets a specific third-party add-on (namely LibAddonsMenu), are independent and reusable.
Last edited by algwyn : 01/26/22 at 03:36 AM.
Report comment to moderator  
Reply With Quote
Unread 01/25/22, 05:21 PM  
Baertram
Super Moderator
 
Baertram's Avatar
ESOUI Super Moderator
AddOn Author - Click to view AddOns

Forum posts: 4053
File comments: 4996
Uploads: 71
Thank you for that library.
Though I'm not fully understanding yet if this is different to LibSavedVars or not.
Report comment to moderator  
Reply With Quote
Post A Reply



Category Jump: