Core Module - Store - Used to store and watch for updates for values in the global table
-- Require the module and add a store with no keys
-- Store with no keys does not need a serializer
local Store = require 'expcore.store' --- @dep expcore.store
local scenario_diffculty = Store.register()
-- When the store is changed this function will trigger
Store.watch(scenario_diffculty, function(value)
game.print('The scenario diffculty has been set to '..value)
end)
Store.set(scenario_diffculty, 'hard') -- Set the value stored to 'hard'
Store.get(scenario_diffculty) -- Returns 'hard'
Store.update(scenario_diffculty, function(value) -- Will set value to 'normal' if no value is present
return not value and 'normal'
end)
-- Require the module and add a store with keys
-- Store with keys does not require a serializer but it can be helpful
local Store = require 'expcore.store' --- @dep expcore.store
local player_scores = Store.register(function(player) -- Use player name as the key
return player.name
end)
-- When any key in the store is changed this function will trigger
Store.watch(player_scores, function(value, key, old_value)
game.print(key..' now has a score of '..value)
end)
Store.set(player_scores, game.player, 10) -- Set your score to 10
Store.get(scenario_diffculty, game.player) -- Returns 10
Store.update(scenario_diffculty, game.player, function(value) -- Add 1 to your score
return value + 1
end)
utils.event |
serializers | An array of the serializers that stores are using, key is store uids |
watchers | An array of watchers that stores will trigger, key is store uids |
file_paths | An index used for debuging to find the file where different stores where registered |
uid | The current highest uid that is being used, will not increase during runtime |
validate(store[, key][, error_stack=1]) | An error checking and serializing function for checking store uids and keys, note key is not required |
register([serializer]) | Required to create new stores and register an serializer to a store, serializer not required |
watch(store, watcher) | Register a watch function to a store that is called when the value in the store is changed, triggers for any key |
get(store[, key]) | Used to retrive the current data that is stored, key is optional depending on if you are using them |
clear(store[, key]) | Used to clear the data in a store, will trigger any watchers, key is optional depending on if you are using them |
set(store[, key], value) | Used to set the data in a store, will trigger any watchers, key is optional depending on if you are using them |
update(store[, key], updater) | Used to update the data in a store, use this with tables, will trigger any watchers, key is optional depending on if you are using them |
map(store, updater) | Used to update all values that are in a store, similar to Store.update but acts on all keys at once, will trigger watchers for every key present |
trigger(store[, key]) | Used to trigger watcher functions, this may be used to trigger them if you did not use Store.update or Store.set |
raw_trigger(store[, key][, value][, old_value]) | Used to trigger watcher functions, the value and key are passed directly to the watchers regardless if the value is correct |
An array of the serializers that stores are using, key is store uids
An array of watchers that stores will trigger, key is store uids
An index used for debuging to find the file where different stores where registered
The current highest uid that is being used, will not increase during runtime
An error checking and serializing function for checking store uids and keys, note key is not required
Parameters:-- Registering a new store and checking that it is valid
-- New store will use player names as the keys
local player_scores = Store.register(function(player)
return player.name
end)
-- player_scores is a valid store and key will be your player name
local key = Store.validate(player_scores, game.player)
Required to create new stores and register an serializer to a store, serializer not required
Parameters:-- Creating a store with no serializer
local scenario_diffculty = Store.register()
-- Creating a store which can take LuaPlayer
local player_scores = Store.register(function(player)
return player.name
end)
Register a watch function to a store that is called when the value in the store is changed, triggers for any key
Parameters:-- Printing the changed value to all players, no keys
-- Register the new store, we are not using keys so we dont need a serializer
local scenario_diffculty = Store.register()
-- Register the watcher so that when we change the value the message is printed
Store.watch(scenario_diffculty, function(value)
game.print('The scenario diffculty has been set to '..value)
end)
-- Set a new value for the diffculty and see that it has printed to the game
Store.set(scenario_diffculty, 'hard')
-- Printing the changed value to all players, with keys
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_scores = Store.register(function(player)
return player.name
end)
-- Register the watcher so that when we change the value the message is printed
Store.watch(player_scores, function(value, key, old_value)
game.print(key..' now has a score of '..value)
end)
-- Set a new value for your score and see that it has printed to the game
Store.set(player_scores, game.player, 10)
Used to retrive the current data that is stored, key is optional depending on if you are using them
Parameters:-- Getting the value of a store with no keys
-- Register the new store, we are not using keys so we dont need a serializer
local scenario_diffculty = Store.register()
-- Get the current diffculty for the scenario
local diffculty = Store.get(scenario_diffculty)
-- Getting the data from a store with keys
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_scores = Store.register(function(player)
return player.name
end)
-- Get your current score
local my_score = Store.get(player_scores, game.player)
-- Get all scores
lcoal scores = Store.get(player_scores)
Used to clear the data in a store, will trigger any watchers, key is optional depending on if you are using them
Parameters:-- Clear a store which does not use keys
-- Register the new store, we are not using keys so we dont need a serializer
local scenario_diffculty = Store.register()
-- Clear the scenario diffculty
Store.clear(scenario_diffculty)
-- Clear data that is in a store with keys
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_scores = Store.register(function(player)
return player.name
end)
-- Clear your score
Store.clear(player_scores, game.player)
-- Clear all scores
Store.clear(player_scores)
Used to set the data in a store, will trigger any watchers, key is optional depending on if you are using them
Parameters:-- Setting a store which does not use keys
-- Register the new store, we are not using keys so we dont need a serializer
local scenario_diffculty = Store.register()
-- Set the new scenario diffculty
Store.set(scenario_diffculty, 'hard')
-- Set data in a store with keys
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_scores = Store.register(function(player)
return player.name
end)
-- Set your current score
Store.set(player_scores, game.player, 10)
-- Set all scores, note this might not have much use
Store.set(player_scores, {
[game.player.name] = 10,
['SomeOtherPlayer'] = 0
})
Used to update the data in a store, use this with tables, will trigger any watchers, key is optional depending on if you are using them
Parameters:-- Incrementing a global score
-- Because we are only going to have one score so we will not need keys or a serializer
local game_score = Store.register()
-- Setting a default value
Store.set(game_score, 0)
-- We now will update the game score by one, we return the value so that it is set as the new value in the store
Store.update(game_score, function(value)
return value + 1
end)
-- Updating keys in a table of data
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_data = Store.register(function(player)
return player.name
end)
-- Setting a default value for your player, used to show the table structure
Store.set(player_data, game.player, {
group = 'Admin',
role = 'Owner',
show_group_config = false
})
-- Updating the show_group_config key in your player data, note that it would be harder to call set every time
-- We do not need to return anything in this case as we are not replacing all the data
Store.update(player_data, game.player, function(data)
data.show_group_config = not data.show_group_config
end)
Used to update all values that are in a store, similar to Store.update but acts on all keys at once, will trigger watchers for every key present
Parameters:-- Updating keys in a table of data
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
local player_data = Store.register(function(player)
return player.name
end)
-- Setting a default value for your player, used to show the table structure
Store.set(player_data, game.player, {
group = 'Admin',
role = 'Owner',
show_group_config = false
})
-- Updating the show_group_config key for all players, note that it would be harder to call set every time
-- We do not need to return anything in this case as we are not replacing all the data
-- We also have access to the current key being updated if needed
Store.map(player_data, function(data, key)
data.show_group_config = not data.show_group_config
end)
Used to trigger watcher functions, this may be used to trigger them if you did not use Store.update or Store.set
Parameters:-- Faking the update to a store
-- The type of store we use does not really matter for this as long as you pass it what you watchers are expecting
local scenario_diffculty = Store.register()
-- Trigger the watchers with a fake change of diffculty
Store.trigger(scenario_diffculty)
Used to trigger watcher functions, the value and key are passed directly to the watchers regardless if the value is correct
Parameters:-- Triggering a manule call of the watchers
-- The type of store we use does not really matter for this as long as you pass it what you watchers are expecting
local scenario_diffculty = Store.register()
-- Trigger the watchers with a fake change of diffculty
-- This is mostly used internally but it can be useful in other cases
Store.raw_trigger(scenario_diffculty, nil, 'normal', 'normal')