Action - Ellpeck/TinyLifeExampleMod Wiki

Original URL: https://github.com/Ellpeck/TinyLifeExampleMod/wiki/Action

Tiny Life

TinyLife.Actions

Action Class

An action is something that a Person does.
An action is always derived from an underlying ActionType that contains various action settings.
Actions can be queued up for a person (ActionQueue) or currently active (CurrentActions).
To create a more complex action, it is best to extend MultiAction.
You can find a multitude of action-related events in Person, like OnActionsCompleted.

public abstract class Action : MLEM.Misc.GenericDataHolder

Inheritance System.Object 🡒 MLEM.Misc.GenericDataHolder 🡒 Action

Derived
DieAction
MultiAction
PathfindAction
SocialAction

Constructors

Action.Action(ActionType, ActionInfo) Constructor

Creates a new action from the given ActionType

public Action(TinyLife.Actions.ActionType type, TinyLife.Actions.ActionInfo info);

Parameters

type ActionType
The type to create this action from

info ActionInfo
The information for this action

Fields

Action.ForceFail Field

This value can be set to true to force IsCompleted() to return Failed.
This is useful if there is an erroring calculation in your Update(GameTime, TimeSpan, GameSpeed) code etc.

protected bool ForceFail;

Field Value

System.Boolean

Action.Info Field

The ActionInfo for this action that contains the clicked (or otherwise targeted) objects and more

public readonly ActionInfo Info;

Field Value

ActionInfo

Action.Random Field

A Random instance that can be used by actions.
This value has the default seed.

public static readonly Random Random;

Field Value

System.Random

Action.Type Field

The ActionType that this action instance derives from

public readonly ActionType Type;

Field Value

ActionType

Properties

Action.ElapsedTime Property

The amount of in-game time that has elapsed since this action has started

public System.TimeSpan ElapsedTime { get; set; }

Property Value

System.TimeSpan

Action.IsUnderlying Property

Stores whether this action is an underlying action of either a MultiAction or a UnderlyingAction

public bool IsUnderlying { get; set; }

Property Value

System.Boolean

Action.Map Property

The Map that this action occurs on

public TinyLife.World.Map Map { get; }

Property Value

Map

Action.Person Property

The Person that this action is being executed by

public TinyLife.Objects.Person Person { get; }

Property Value

Person

Action.StartedAutomatically Property

If this value is true, this action was started using PersonAi or through another action rather than by the player

public bool StartedAutomatically { get; set; }

Property Value

System.Boolean

Methods

Action.ApplyVariety(ActionVariety) Method

Applies the ActionVariety given ot this action.
Based on the action and the variety, a number of things can be done.
Note that only varieties supplied in Varieties will be passed into this method.

public virtual void ApplyVariety(TinyLife.Actions.ActionVariety variety);

Parameters

variety ActionVariety
The variety to apply

Action.CanCancel(Action) Method

Returns whether or not this action can be canceled by the given outside source.
Note that the outside source can be null, and if it is, it means that the player canceled the action manually.
By default, actions can only be canceled if the cancelSource is null.

public virtual bool CanCancel(TinyLife.Actions.Action cancelSource);

Parameters

cancelSource Action
The source of the cancelation, or null if the player canceled it

Returns

System.Boolean
true if the action can be canceled

Action.CanEnqueueConversation(Person, ActionType) Method

Returns true if the given person can (automatically) enqueue a social action with the Person that is executing this action.
Note that enqueueing a social action manually is still possible even if this method returns false.
By default, only TinyLife.Actions.SleepAction returns false on this method.

protected virtual bool CanEnqueueConversation(TinyLife.Objects.Person person, TinyLife.Actions.ActionType type);

Parameters

person Person
The person that wants to converse with us

type ActionType
The type of action that should be enqueued

Returns

System.Boolean
Whether or not enqueueing a social action is possible

Action.CanMultitask(Action, Action) Method

A utility method that can be used whether the two Action instances can currently be invoked together.
Internally, this checks if both actions return true on CanMultitask(Action).

public static bool CanMultitask(TinyLife.Actions.Action a1, TinyLife.Actions.Action a2);

Parameters

a1 Action
The first action

a2 Action
The second action

Returns

System.Boolean
Whether the two actions can be multi-tasked

Action.CanMultitask(Action) Method

Return true on this method if this action can be multi-tasked along with the passed Action.
To actually check this property, CanMultitask(Action, Action) should be used as it compares both objects.
A multi-tasking is an action that is currently active along with another action.
By default, multi-tasking is disallowed for any action.

public virtual bool CanMultitask(TinyLife.Actions.Action other);

Parameters

other Action
The action to multi-task with

Returns

System.Boolean
Whether this action can be multi-tasked

Action.CausesExtremelyFastSpeed() Method

Returns true if this action, while it is currently active, should cause the ExtremelyFast speed to be available.
By default, this method returns false.

public virtual bool CausesExtremelyFastSpeed();

Returns

System.Boolean
Whether the extremely fast speed should be available

Action.CompleteIfNeedFull(NeedType, CompletionType) Method

A helper method that returns Completed if the given Need's value is at Max.

protected TinyLife.Actions.Action.CompletionType CompleteIfNeedFull(TinyLife.NeedType type, TinyLife.Actions.Action.CompletionType els=TinyLife.Actions.Action.CompletionType.Active);

Parameters

type NeedType
The need that should be completed

els CompletionType
The action type that is returned if the need is not completed. Active by default.

Returns

CompletionType
The appropriate completion type

Action.CompleteInTime(TimeSpan, bool, SkillType, float, bool) Method

A helper method that returns Completed if the given time has passed.
This method makes use of GetEfficiencyModifier(SkillType, float) to return true faster or slower based on the person's current mood and skill levels.

protected TinyLife.Actions.Action.CompletionType CompleteInTime(System.TimeSpan time, bool efficiencyMatters=false, TinyLife.Skills.SkillType skill=null, float levelModifier=0.1f, bool autoOnly=false);

Parameters

time System.TimeSpan
The (total!) amount of time after which this action should be completed

efficiencyMatters System.Boolean
Whether the person's efficiency modifier (GetEfficiencyModifier(SkillType, float)) should be taken into account for the total time required

skill SkillType
A skill that optionally influences the completion time

levelModifier System.Single
The amount that each skill level should influence the returned time by. Defaults to 0.1.

autoOnly System.Boolean
Whether the action should only be completed in the given amount of time if it has been StartedAutomatically. If both this value and StartedAutomatically are true, this method always returns Active.

Returns

CompletionType
Completed if the time has passed, Active otherwise

Action.FindAllFreeFurniture(Person, ObjectCategory, FurnitureType, bool, Nullable<Vector2>, Nullable<float>, bool, bool) Method

A helper method to find a set of all Furniture instances that are currently valid for interaction with the passed Person

public static System.Collections.Generic.IEnumerable<TinyLife.Objects.Furniture> FindAllFreeFurniture(TinyLife.Objects.Person person, TinyLife.Objects.ObjectCategory categories, TinyLife.Objects.FurnitureType objectSpotType=null, bool needsFreeActionSpot=true, System.Nullable<Microsoft.Xna.Framework.Vector2> position=null, System.Nullable<float> radius=null, bool allowBroken=false, bool ignoreVisibility=false);

Parameters

person Person
The person that wants to start the aciton

categories ObjectCategory
The categories that the furniture should have

objectSpotType FurnitureType
The type of item that an object spot should be available for, or null if this is not required

needsFreeActionSpot System.Boolean
Whether or not the furniture returned needs a non-occupied ActionSpot

position System.Nullable<Microsoft.Xna.Framework.Vector2>
The position that we should find people around, or null to use the passed person's position

radius System.Nullable<System.Single>
The radius that should be searched for people in, or 32 by default

allowBroken System.Boolean
Whether furniture that is Broken can be returned

ignoreVisibility System.Boolean
Whether the visibility (IsLotVisible(Lot)) of the object's lot should be ignored, or false by default

Returns

System.Collections.Generic.IEnumerable<Furniture>
A set of valid furniture

Action.FindAllFreeGround(Person, Nullable<Vector2>, Nullable<float>, bool) Method

A helper method to find a set of all positions on the ground that are currently valid for interaction

public static System.Collections.Generic.IEnumerable<Microsoft.Xna.Framework.Point> FindAllFreeGround(TinyLife.Objects.Person person, System.Nullable<Microsoft.Xna.Framework.Vector2> position=null, System.Nullable<float> radius=null, bool ignoreVisibility=false);

Parameters

person Person
The person that wants to start the aciton

position System.Nullable<Microsoft.Xna.Framework.Vector2>
The position that the ground should be around, or null to use the person's position

radius System.Nullable<System.Single>
The radius that should be searched for ground positions in, or 32 by default

ignoreVisibility System.Boolean
Whether the visibility (IsLotVisible(Lot)) of the object's lot should be ignored, or false by default

Returns

System.Collections.Generic.IEnumerable<Microsoft.Xna.Framework.Point>
A set of valid locations

Action.FindAllFreePeople(ActionType, Person, Nullable<Vector2>, Nullable<float>, bool) Method

A helper method to find a set of all Person instances that are currently valid for interaction with the passed Person

public static System.Collections.Generic.IEnumerable<TinyLife.Objects.Person> FindAllFreePeople(TinyLife.Actions.ActionType type, TinyLife.Objects.Person person, System.Nullable<Microsoft.Xna.Framework.Vector2> position=null, System.Nullable<float> radius=null, bool ignoreVisibility=false);

Parameters

type ActionType
The type of action we want to start

person Person
The person that wants to start the aciton

position System.Nullable<Microsoft.Xna.Framework.Vector2>
The position that we should find people around, or null to use the passed person's position

radius System.Nullable<System.Single>
The radius that should be searched for people in, or 32 by default

ignoreVisibility System.Boolean
Whether the visibility (IsLotVisible(Lot)) of the object's lot should be ignored, or false by default

Returns

System.Collections.Generic.IEnumerable<Person>
A set of valid interaction partners

Action.FindAllFreeWalls(Person, Nullable<Vector2>, Nullable<float>, bool) Method

A helper method to find a set of all Wall instances that are currently valid for interaction with the passed Person

public static System.Collections.Generic.IEnumerable<TinyLife.World.Wall> FindAllFreeWalls(TinyLife.Objects.Person person, System.Nullable<Microsoft.Xna.Framework.Vector2> position=null, System.Nullable<float> radius=null, bool ignoreVisibility=false);

Parameters

person Person
The person that wants to start the aciton

position System.Nullable<Microsoft.Xna.Framework.Vector2>
The position that the walls should be around, or null to use the person's position

radius System.Nullable<System.Single>
The radius that should be searched for walls in, or 32 by default

ignoreVisibility System.Boolean
Whether the visibility (IsLotVisible(Lot)) of the object's lot should be ignored, or false by default

Returns

System.Collections.Generic.IEnumerable<Wall>
A set of valid walls

Action.FindFreeFurniture(Person, ObjectCategory, FurnitureType, Nullable<Vector2>, bool) Method

A helper method to find the best Furniture instance to interact with based on the given data.
Note that this method always returns a ActionInfo related to the first result from FindAllFreeFurniture(Person, ObjectCategory, FurnitureType, bool, Nullable<Vector2>, Nullable<float>, bool, bool).

public static TinyLife.Actions.ActionInfo FindFreeFurniture(TinyLife.Objects.Person person, TinyLife.Objects.ObjectCategory categories, TinyLife.Objects.FurnitureType objectSpotType=null, System.Nullable<Microsoft.Xna.Framework.Vector2> position=null, bool allowBroken=false);

Parameters

person Person
The person that wants to start the aciton

categories ObjectCategory
The categories that the furniture should have

objectSpotType FurnitureType
The type of item that an object spot should be available for, or null if this is not required

position System.Nullable<Microsoft.Xna.Framework.Vector2>
The position that we should find people around, or null to use the passed person's position

allowBroken System.Boolean
Whether furniture that is Broken can be returned

Returns

ActionInfo
An action info for the best furniture, or null if there is none

Action.GetDisplayName() Method

Returns a localized string that explains this action in short.
By default, this method returns GetDisplayName(ActionInfo, bool).

public virtual string GetDisplayName();

Returns

System.String
This action's display name

Action.GetFreeChair(Person, Furniture) Method

A helper method that returns an ActionInfo for a ActionSpot on a Furniture with the Chair category that is closest to the given object which is on a desk or table.
The furniture returned is the one that the person should Sit(Furniture, GameSpeed, ActionSpot) on when interacting with the passed deskObject.
This method will return the deskObject's parent itself if it is a picnic-style table that has benches directly attached to it.

public static (TinyLife.Objects.Furniture Chair,TinyLife.Objects.ActionSpot Spot,MLEM.Misc.Direction2 Direction) GetFreeChair(TinyLife.Objects.Person person, TinyLife.Objects.Furniture deskObject);

Parameters

person Person
The person to get the free chair for

deskObject Furniture
The object that is sat on a desk

Returns

<Furniture,ActionSpot,MLEM.Misc.Direction2>
The corresponding chair, ActionSpot and the action spot's rotation, or default if there is none

Action.GetIconObject() Method

Returns the map object that is displayed in the action queue in the top left of the screen.
Note that this value is ignored if this action's type has a Texture.
By default, the GetActionObject<T>() is returned.

public virtual TinyLife.Objects.MapObject GetIconObject();

Returns

MapObject
The icon object

Action.GetInProgressTime() Method

Returns the amount of time that this action has "properly" been in progress for.
This is used by MultiAction and SocialAction and returns true only once the first actions are completed or the conversation has started, respectively.
By default, ElapsedTime is returned.

public virtual System.TimeSpan GetInProgressTime();

Returns

System.TimeSpan
The amount of time that this action has properly been in progress for

Action.GetNextAction(CompletionType) Method

Returns an action that should be queued up immediately after this action completes.
The queued up action is immediately started in the slot that this action occupied.
Can be null, and is null by default.

public virtual TinyLife.Actions.Action GetNextAction(TinyLife.Actions.Action.CompletionType completion);

Parameters

completion CompletionType
The type that this action completed with

Returns

Action
The follow-up action

Action.GetPlaceDirection(FurnitureType) Method

Returns a MLEM.Misc.Direction2 that represents the facing that the given FurnitureType should be placed with.
The direction is determined by the Person's current rotation.

protected MLEM.Misc.Direction2 GetPlaceDirection(TinyLife.Objects.FurnitureType item=null);

Parameters

item FurnitureType
The item to place

Returns

MLEM.Misc.Direction2
The facing the item should be placed with

Action.GetTableSpot(Furniture, ActionSpot) Method

A helper method that returns the ObjectSpot on the table or desk that the given chair is connected to.
This is the object spot that should be used for interaction if a person sits on the given chair.

public static (TinyLife.Objects.Furniture Table,TinyLife.Objects.ObjectSpot Spot) GetTableSpot(TinyLife.Objects.Furniture chair, TinyLife.Objects.ActionSpot actionSpot=null);

Parameters

chair Furniture
The chair to get the table spot for

actionSpot ActionSpot
The action spot on the chair to get the table spot for

Returns

<Furniture,ObjectSpot>
The table spot, or null if there is none

Action.Initialize() Method

This method is called when the action is first started by a Person.
Note that it is not called when the action gets added to the ActionQueue, but when it is moved to CurrentActions.

public abstract void Initialize();

Action.IsCompleted() Method

This method is called every update frame by a Person if this action is currently active to check if it should be stopped.
If this returns a result other than Completed, OnCompleted(CompletionType) will be called and the action is stopped.
By default, only ForceFail modifies the completion type, otherwise Active is returned.

public virtual TinyLife.Actions.Action.CompletionType IsCompleted();

Returns

CompletionType
The current completion type of this action

Action.IsFullyInProgress() Method

Returns whether this action is currently "properly" in progress.
This is used by MultiAction and SocialAction and returns true only once the first actions are completed or the conversation has started, respectively.
By default, this method returns true if GetInProgressTime() is greater than System.TimeSpan.Zero.

public virtual bool IsFullyInProgress();

Returns

System.Boolean
Whether this action is fully in progress

Action.OnCompleted(CompletionType) Method

This method is called when this action IsCompleted(), or if it is canceled from an outside source.
Note that, if this method is called as a result of IsCompleted(), the CompletionType passed will be the same.

public virtual void OnCompleted(TinyLife.Actions.Action.CompletionType type);

Parameters

type CompletionType
The type that this action completed with

Action.OpenPlayerPrompt(string, Predicate<Panel>, Action<Panel>, bool, Element[]) Method

Opens a prompt for the player with the given elements, also pausing the game.
This behavior is used for things like the TinyLife.Actions.ActionType.QuitJob action, where a confirmation panel pops up.

protected MLEM.Ui.Elements.Group OpenPlayerPrompt(string title, System.Predicate<MLEM.Ui.Elements.Panel> canFinish, System.Action<MLEM.Ui.Elements.Panel> onFinished, bool canExit, params MLEM.Ui.Elements.Element[] children);

Parameters

title System.String
The title that should be displayed in the box, has to be localized if required

canFinish System.Predicate<MLEM.Ui.Elements.Panel>
A function that determines whether the Okay button can be pressed. If the function is null, no button is displayed.

onFinished System.Action<MLEM.Ui.Elements.Panel>
A function that is called once the Okay button is pressed. Can be null.

canExit System.Boolean
Whether the user can exit the window without making a decision by clicking the background

children MLEM.Ui.Elements.Element[]
The elements that should be displayed in this prompt, along with the title and possibly the Okay button if canFinish is used

Returns

MLEM.Ui.Elements.Group

Exceptions

System.InvalidOperationException
Thrown if this action was StartedAutomatically

Action.OpenTextPrompt(string, Action<string>, Predicate<string>, Rule) Method

Opens a prompt with a text box that allows the player to input a string.
While the prompt is open, the game will be paused.

protected void OpenTextPrompt(string title, System.Action<string> onFinished, System.Predicate<string> isNameValid=null, MLEM.Ui.Elements.TextField.Rule rule=null);

Parameters

title System.String
The text to display in the prompt

onFinished System.Action<System.String>
An action that is executed when the okay button is pressed, which contains the string that was input into the text box

isNameValid System.Predicate<System.String>
A function that determines whether the name is valid. If this is null, all text inputs (except an empty string) will be valid.

rule MLEM.Ui.Elements.TextField.Rule
A rule to use for the MLEM.Ui.Elements.TextField

Action.OpenYesNoPrompt(string, Action, Action) Method

Opens a prompt for the player that contains a yes and a no button along with some text.
This prompt can be used to ensure that a player selected the right option in an important decision.
While the prompt is open, the game will be paused.

protected void OpenYesNoPrompt(string title, System.Action onYes, System.Action onNo=null);

Parameters

title System.String
The text to display in the prompt

onYes System.Action
The action that should be executed when the yes button is pressed

onNo System.Action
The action that should be executed when the no button is pressed

Action.PutDownOrGoTo(ObjectCategory, FurnitureType[]) Method

A helper method used by TinyLife.Actions.PrepareFoodAction and TinyLife.Actions.CookFoodAction that causes the Person to walk to a valid surface with the action item on it, plcae the action item down on a valid surface or move the action object from its current location to a valid surface.
The validity of the surface is determined by parentToPutOn, and the item to find is any of items.

protected System.Collections.Generic.IEnumerable<TinyLife.Actions.Action> PutDownOrGoTo(TinyLife.Objects.ObjectCategory parentToPutOn, params TinyLife.Objects.FurnitureType[] items);

Parameters

parentToPutOn ObjectCategory
An object category that the object's surface should have

items FurnitureType[]
The items that should be found on the surface

Returns

System.Collections.Generic.IEnumerable<Action>
A set of actions that cause the person to set the action object up correctly

Action.Sit(Furniture, GameSpeed, ActionSpot) Method

A helper method that causes the Person to sit on the given object.
This method causes the action spot to be occupied and the person's CurrentPose to be changed to Sitting.
Additionally, the TinyLife.NeedType.Energy need is restored a little bit.
Note that this method has to be called every Update(GameTime, TimeSpan, GameSpeed) frame for the person to stay sat down.

protected bool Sit(TinyLife.Objects.Furniture chair, TinyLife.GameSpeed speed, TinyLife.Objects.ActionSpot spot=null);

Parameters

chair Furniture
The chair to sit on

speed GameSpeed
The current game speed

spot ActionSpot
The action spot to sit on, or null to select one automatically

Returns

System.Boolean
Whether or not the chair can be sat on

Action.SpeakAlone(GameTime, GameSpeed, EmoteCategory, SpeakStyle, int) Method

Displays emotes for this action's Person with the given settings as if they were talking to themselves out loud.
This is used by actions like TinyLife.Actions.ActionType.PracticeJokes and TinyLife.Actions.ActionType.PracticeSpeech.

protected bool SpeakAlone(Microsoft.Xna.Framework.GameTime time, TinyLife.GameSpeed speed, TinyLife.Actions.EmoteCategory emoteCategories, TinyLife.Objects.SpeakStyle speakStyles, int totalAmount=int.MaxValue);

Parameters

time Microsoft.Xna.Framework.GameTime
The game's current time

speed GameSpeed
The game's current speed

emoteCategories EmoteCategory
The categories to pick emotes from, can be a combined flag

speakStyles SpeakStyle
The speak styles to use for speech sounds, can be a combined flag

totalAmount System.Int32
The total amount of emotes to display, defaults to an infinite amount (System.Int32.MaxValue)

Returns

System.Boolean
Whether all emotes were succesfully displayed. This method only returns true if totalAmount is less than the default value

Action.TryForceFail(CanExecuteResult) Method

This is an external version of ForceFail which can be called from outside sources, like a BreakableFurniture object.
A reason is provided that can be used to decide whether the external object can cause this action to fail.
By default, this action sets ForceFail to true and returns true always.

public virtual bool TryForceFail(TinyLife.Actions.ActionType.CanExecuteResult reason);

Parameters

reason CanExecuteResult
The reason for this action to fail

Returns

System.Boolean
Whether this action was successfully forced to fail (if returning true, ForceFail should also be set to true)

Action.Update(GameTime, TimeSpan, GameSpeed) Method

This method is called every update frame if this action is currently active.
By default, only the ElapsedTime is modified in this method and ActionUpdate(Action, GameTime, TimeSpan, GameSpeed, bool) is called.

public virtual void Update(Microsoft.Xna.Framework.GameTime time, System.TimeSpan passedInGame, TinyLife.GameSpeed speed);

Parameters

time Microsoft.Xna.Framework.GameTime
The current game time

passedInGame System.TimeSpan
The amount of time that has passed, in game time

speed GameSpeed
The game's current speed setting

Action.Validate(Person) Method

Validates this action's data.
This is called when a map is loaded from disk.
When returning false on this method, the action is removed from the Person.

public virtual bool Validate(TinyLife.Objects.Person person);

Parameters

person Person
The person that this action belongs to

Returns

System.Boolean
Whether or not the action is still valid (or if it has invalid data)