Class VirtualValue
(Abstract) A container for a single value.
A VirtualValue (or VV for short) has a single contained value. This value is initially nil, although implementations
should set a sensible default (0
, false
, ""
, etc). It has useful tools which allow you to observe and manipulate the value:
- set(value), which sets the currently contained value
- get, which returns the value
- value([newValue]), which can both set and get the value
- listen(func), which calls a given function with the contained value and every time it changes
- bind(object, property), which sets a key of a table with the contained value and every time it changes
Additionally, the onChange(newValue) event fires immediately after the value is set though any means.
Replication
Server-side replication of a VirtualValue is easily implemented through a Server, constructable through the server method. On the client, use the client method and provide the remotes container. Finally, open the server to begin replication. You can set up fine-grain replication rules within the Permissions of the Server.
Implementations:
- VirtualNumberValue, for number
- VirtualStringValue, for string
- VirtualBoolValue, for boolean
See also:
- DynamicVirtualValue, an abstract subclass which can add VirtualValue as children to stack their values
Events
VirtualValue.onChange | Fires when the contained value changes |
Functions
VirtualValue.new(expectedType) | Constructs a new VirtualValue which contains a certain expectedType |
Metamethods
VirtualValue:__tostring() | Returns the current value passed to tostring |
Methods
VirtualValue:cleanup() | Releases all resources used by this VirtualValue |
VirtualValue:set(value) | Sets the contained value; if different than current, fires onChange |
VirtualValue:get() | Gets the contained value |
VirtualValue:value([newValue]) | Set/get the contained value |
VirtualValue:listen(func) | Call a function with the contained value, then again every time the contained value changes |
VirtualValue:bind(object, property) | Set object[property] to the currently contained value, then again every time the contained value changes |
VirtualValue:getServerClass() | Returns the class used by VirtualValue:server |
VirtualValue:server(...) | Constructs a Server to replicate the contained value to various clients |
VirtualValue:client(remotes) | Constructs a Client to replicate the contained value to/from the server |
VirtualValue:serialize() | Convert the contained value into a payload that can be sent through Roblox's serialization used by BindableFunction, RemoteFunction, etc. |
VirtualValue:deserialize(payload) | Set the contained value to the provided payload from serialize |
Events
Functions
Methods- VirtualValue.new(expectedType)
-
Constructs a new VirtualValue which contains a certain
expectedType
Parameters:
- expectedType string expected type (or table of expected types) to be contained
Metamethods
- VirtualValue:__tostring()
-
Returns the current value passed to tostring
Returns:
-
string
The current value as a string
Methods
- VirtualValue:cleanup()
- Releases all resources used by this VirtualValue
- VirtualValue:set(value)
-
Sets the contained value; if different than current, fires onChange
Parameters:
- value The value to store
- VirtualValue:get()
-
Gets the contained value
Returns:
-
The contained value
- VirtualValue:value([newValue])
-
Set/get the contained value
Parameters:
- newValue The new value to set, or nil to keep the current value (optional)
Returns:
-
The contained value
- VirtualValue:listen(func)
-
Call a function with the contained value, then again every time the contained value changes
Parameters:
- func function The listener function
Returns:
-
Connection
When disconnected,
func
from being called when the value changes - VirtualValue:bind(object, property)
-
Set
object[property]
to the currently contained value, then again every time the contained value changesParameters:
- object
A table, userdata, etc which can accept
[property] = ...
- property
string
The index on
object
to set
Returns:
-
Connection
When disconnected, stops
func
from being called when the value changes - object
A table, userdata, etc which can accept
- VirtualValue:getServerClass()
-
Returns the class used by VirtualValue:server
Returns:
-
The Server class
- VirtualValue:server(...)
-
Constructs a Server to replicate the contained value to various clients
Parameters:
- ... Passed to Server.new
Returns:
-
Server
The newly constructed server
- VirtualValue:client(remotes)
-
Constructs a Client to replicate the contained value to/from the server
Parameters:
- remotes The remotes container as returned by Server:getRemotesContainer
Returns:
- VirtualValue:serialize()
-
Convert the contained value into a payload that can be sent through Roblox's serialization used by BindableFunction, RemoteFunction, etc.
This default implementation sends the data unchanged; custom types will need custom serilization.
Returns:
-
The data payload
- VirtualValue:deserialize(payload)
-
Set the contained value to the provided payload from serialize
Parameters:
- payload The data payload