diff --git a/Moose Development/Moose/Core/Base.lua b/Moose Development/Moose/Core/Base.lua index 8e6a06e2b..6bb7aa043 100644 --- a/Moose Development/Moose/Core/Base.lua +++ b/Moose Development/Moose/Core/Base.lua @@ -115,6 +115,19 @@ -- * @{#BASE.GetClassName}(): Gets the name of the object, which is the name of the class the object was instantiated from. -- * @{#BASE.GetClassNameAndID}(): Gets the name and ID of the object. -- +-- ## 1.5) All objects derived from BASE can have "States" +-- +-- A mechanism is in place in MOOSE, that allows to let the objects administer **states**. +-- States are essentially properties of objects, which are identified by a **Key** and a **Value**. +-- The method @{#BASE.SetState}() can be used to set a Value with a reference Key to the object. +-- To **read or retrieve** a state Value based on a Key, use the @{#BASE.GetState} method. +-- These two methods provide a very handy way to keep state at long lasting processes. +-- Values can be stored within the objects, and later retrieved or changed when needed. +-- There is one other important thing to note, the @{#BASE.SetState}() and @{#BASE.GetState} methods +-- receive as the **first parameter the object for which the state needs to be set**. +-- Thus, if the state is to be set for the same object as the object for which the method is used, then provide the same +-- object name to the method. +-- -- ## 1.10) BASE Inheritance (tree) support -- -- The following methods are available to support inheritance: @@ -706,25 +719,41 @@ function BASE:onEvent(event) end end -function BASE:SetState( Object, StateName, State ) +--- Set a state or property of the Object given a Key and a Value. +-- Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone. +-- @param #BASE self +-- @param Object The object that will hold the Value set by the Key. +-- @param Key The key that is used as a reference of the value. Note that the key can be a #string, but it can also be any other type! +-- @param Value The value to is stored in the object. +-- @return The Value set. +-- @return #nil The Key was not found and thus the Value could not be retrieved. +function BASE:SetState( Object, Key, Value ) local ClassNameAndID = Object:GetClassNameAndID() self.States[ClassNameAndID] = self.States[ClassNameAndID] or {} - self.States[ClassNameAndID][StateName] = State - self:T2( { ClassNameAndID, StateName, State } ) + self.States[ClassNameAndID][Key] = Value + self:T2( { ClassNameAndID, Key, Value } ) - return self.States[ClassNameAndID][StateName] + return self.States[ClassNameAndID][Key] end - -function BASE:GetState( Object, StateName ) + + +--- Get a Value given a Key from the Object. +-- Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone. +-- @param #BASE self +-- @param Object The object that holds the Value set by the Key. +-- @param Key The key that is used to retrieve the value. Note that the key can be a #string, but it can also be any other type! +-- @param Value The value to is stored in the Object. +-- @return The Value retrieved. +function BASE:GetState( Object, Key ) local ClassNameAndID = Object:GetClassNameAndID() if self.States[ClassNameAndID] then - local State = self.States[ClassNameAndID][StateName] or false - self:T2( { ClassNameAndID, StateName, State } ) - return State + local Value = self.States[ClassNameAndID][Key] or false + self:T2( { ClassNameAndID, Key, Value } ) + return Value end return nil diff --git a/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.lua b/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.lua index 39dd57319..2216f6efa 100644 --- a/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.lua +++ b/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.lua @@ -12,33 +12,39 @@ -- -- +-- MOOSE wraps each Group alive in the mission into a GROUP class object. The GROUP class object is a wrapper object, wrapping +-- the Group object from DCS and adding methods to it. +-- Get the GROUP wrapper objects that were created by MOOSE at mission startup, by using the GROUP:FindByName() method. +-- The Group name is the parameter to be searched for. +-- Note that late activated groups are also "alive" and have a corresponding GROUP object in the running mission. local PolyZoneGroup1 = GROUP:FindByName("PolyZone1") local PolyZoneGroup2 = GROUP:FindByName("PolyZone2") +-- Create 2 Polygon objects, using the ZONE_POLYGON:New constructor. +-- The first parameter gives a name to the zone, the second is the GROUP object that defines the zone form. local PolyZone1 = ZONE_POLYGON:New( "PolyZone1", PolyZoneGroup1 ) local PolyZone2 = ZONE_POLYGON:New( "PolyZone2", PolyZoneGroup2 ) +-- Create a SET of Moose CLIENT wrapper objects. At mission startup, a SET of Moose client wrapper objects is created. +-- Note that CLIENT objects don't necessarily need to be alive!!! +-- So this set contains EVERY RED coalition client defined within the mission. local RedClients = SET_CLIENT:New():FilterCoalitions("red"):FilterStart() -SchedulerInit, SchedulerInitID = SCHEDULER:New( nil, - function() - RedClients:ForEachClient( - function( MooseClient ) - - -- Here we register the state of the client in which step he is in. - -- We set the state of the client "ZoneStep" to 0, indicating that he is not out of the first zone. - local function ResetClientForZone( MooseClient ) - BASE:E("Reset") - MooseClient:SetState( MooseClient, "ZoneStep", "0" ) - end - - BASE:E( { "Alive Init", Client = MooseClient } ) - MooseClient:Alive( ResetClientForZone ) - end - ) - end, {}, 1 +RedClients:ForEachClient( + function( MooseClient ) + + -- Here we register the state of the client in which step he is in. + -- We set the state of the client "ZoneStep" to 0, indicating that he is not out of the first zone. + local function ResetClientForZone( MooseClient ) + BASE:E("Reset") + MooseClient:SetState( MooseClient, "ZoneStep", "0" ) + end + + BASE:E( { "Alive Init", Client = MooseClient } ) + MooseClient:Alive( ResetClientForZone ) + end ) Scheduler, SchedulerID = SCHEDULER:New( nil, diff --git a/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.miz b/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.miz index 1a83dba9a..a9a725b63 100644 Binary files a/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.miz and b/Moose Test Missions/ZON - Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones/ZON-510 - Send message if Clients fly the first time in the Polygon Zones.miz differ diff --git a/docs/Documentation/Base.html b/docs/Documentation/Base.html index 6888b0cc6..9d7ccc16f 100644 --- a/docs/Documentation/Base.html +++ b/docs/Documentation/Base.html @@ -202,6 +202,19 @@ The Core.Event#EVENTDATA structure co
A mechanism is in place in MOOSE, that allows to let the objects administer states. +States are essentially properties of objects, which are identified by a Key and a Value. +The method BASE.SetState() can be used to set a Value with a reference Key to the object. +To read or retrieve a state Value based on a Key, use the BASE.GetState method. +These two methods provide a very handy way to keep state at long lasting processes. +Values can be stored within the objects, and later retrieved or changed when needed. +There is one other important thing to note, the BASE.SetState() and BASE.GetState methods +receive as the first parameter the object for which the state needs to be set. +Thus, if the state is to be set for the same object as the object for which the method is used, then provide the same +object name to the method.
+The following methods are available to support inheritance:
@@ -520,9 +533,9 @@ YYYY-MM-DD: CLASS:NewFunction( Params ) addedGet a Value given a Key from the Object.
Set a state or property of the Object given a Key and a Value.
Get a Value given a Key from the Object.
+ +Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone.
Object :
Object :
+The object that holds the Value set by the Key.
StateName :
Key :
+The key that is used to retrieve the value. Note that the key can be a #string, but it can also be any other type!
Value :
+The value to is stored in the Object.
The Value retrieved.
+Set a state or property of the Object given a Key and a Value.
+ +Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone.
Object :
Object :
+The object that will hold the Value set by the Key.
StateName :
Key :
+The key that is used as a reference of the value. Note that the key can be a #string, but it can also be any other type!
State :
Value :
+The value to is stored in the object.
The Value set.
+ +#nil: +The Key was not found and thus the Value could not be retrieved.
+ +