CreateActor: Difference between revisions
Tayfunwiki (talk | contribs) (→Usage) |
Tayfunwiki (talk | contribs) No edit summary |
||
Line 47: | Line 47: | ||
Suitable for scenarios where only the <code>RuntimeActor</code> instance is needed, and the creation process is streamlined without the requirement of an associated <code>GameObject</code>. | Suitable for scenarios where only the <code>RuntimeActor</code> instance is needed, and the creation process is streamlined without the requirement of an associated <code>GameObject</code>. | ||
=== | === Example of Usage === | ||
<syntaxhighlight lang="c#"> | |||
Actor player = IM.Player; // Gets the Player actor. | |||
if (!player.HasSpouse) return; // Exits if the player has no spouse. | |||
Actor firstSpouse = player.Spouses().FirstOrDefault(); // Retrieves the player's first spouse. | |||
// Generating random gender and name for the child. | |||
Actor.IGender randomGender = Actor.RandomGender; | |||
Culture culture = player.Culture; | |||
string randomName = culture.GenerateName(randomGender); | |||
// Selecting the portrait based on the child's gender. | |||
Sprite portrait; | |||
if (randomGender == Actor.IGender.Female) { | |||
portrait = babyPortrait_f; // Female baby portrait. (Sprite object) | |||
} | |||
else { | |||
portrait = babyPortrait_m; // Male baby portrait. (Sprite object) | |||
} | |||
// Creating the child actor and corresponding GameObject. | |||
RuntimeActor.CreateActor(randomName, Actor.IState.Active, 0, culture, randomGender, portrait, out RuntimeActor childActor, out GameObject gameObj); | |||
// Adding the child to the player's family. | |||
if (firstSpouse != null) { | |||
player.AddChild(firstSpouse, childActor); // If there's a spouse, adds the child to both parents. | |||
} else { | |||
player.AddChild(childActor); // If no spouse, adds the child only to the player. | |||
} | |||
// Adding the Character component to the GameObject. | |||
gameObj.AddComponent<Character>(); | |||
</syntaxhighlight> | |||
==== Description ==== | |||
In this example, a new child character is created for the player and their spouse (if present). The child's gender is randomly determined, and a culturally appropriate name is generated. A corresponding <code>GameObject</code> is also created, and the new child is added to the player's family. The <code>GameObject</code> is then enhanced with a <code>Character</code> component. | |||
==== Remarks ==== | |||
* Both methods provide flexibility in character creation, supporting diverse and dynamic game worlds. | * Both methods provide flexibility in character creation, supporting diverse and dynamic game worlds. | ||
* The inclusion of cultural background and gender in character creation adds depth to the character's identity and can influence gameplay and narrative. | * The inclusion of cultural background and gender in character creation adds depth to the character's identity and can influence gameplay and narrative. | ||
* These methods are particularly useful in RPGs, simulation games, and any narrative-driven game that requires the introduction of new characters with specific traits and representations. | * These methods are particularly useful in RPGs, simulation games, and any narrative-driven game that requires the introduction of new characters with specific traits and representations. |
Revision as of 07:35, 24 December 2023
CreateActor
Methods in RuntimeActor Class
Overview
The CreateActor
methods in the RuntimeActor
class are used to create new actors within the game. These methods are essential for dynamically introducing new characters, such as when a child is born or a new character enters the storyline.
Method 1: CreateActor with Out Parameters
Syntax
public static void CreateActor(string name, IState state, int age, Culture culture, IGender gender,
Sprite portrait, out RuntimeActor runtimeActor, out GameObject gameObj)
Parameters
- name (string): The name of the new actor.
- state (IState): The initial state of the new actor.
- age (int): The age of the new actor.
- culture (Culture): The cultural background of the new actor.
- gender (IGender): The gender of the new actor.
- portrait (Sprite): The sprite for the new actor's portrait.
- runtimeActor (out RuntimeActor): Out parameter returning the created RuntimeActor instance.
- gameObj (out GameObject): Out parameter returning the GameObject associated with the new actor.
Description
This method creates a new RuntimeActor
and a corresponding GameObject
. It initializes the actor with the specified name, state, age, culture, gender, and portrait. The method returns the created RuntimeActor
and GameObject
through out parameters.
Usage
Used to add new characters to the game with specific attributes. Essential for games where the population of characters needs to expand dynamically.
Method 2: CreateActor with Return Type
Syntax
public static RuntimeActor CreateActor(string name, IState state, int age, Culture culture, IGender gender,
Sprite portrait)
Parameters
- name, state, age, culture, gender, portrait: Same as Method 1.
Description
Similar to the first method, this variation creates a new RuntimeActor
with the provided details. However, it directly returns the RuntimeActor
instance without using out parameters.
Usage
Suitable for scenarios where only the RuntimeActor
instance is needed, and the creation process is streamlined without the requirement of an associated GameObject
.
Example of Usage
Actor player = IM.Player; // Gets the Player actor.
if (!player.HasSpouse) return; // Exits if the player has no spouse.
Actor firstSpouse = player.Spouses().FirstOrDefault(); // Retrieves the player's first spouse.
// Generating random gender and name for the child.
Actor.IGender randomGender = Actor.RandomGender;
Culture culture = player.Culture;
string randomName = culture.GenerateName(randomGender);
// Selecting the portrait based on the child's gender.
Sprite portrait;
if (randomGender == Actor.IGender.Female) {
portrait = babyPortrait_f; // Female baby portrait. (Sprite object)
}
else {
portrait = babyPortrait_m; // Male baby portrait. (Sprite object)
}
// Creating the child actor and corresponding GameObject.
RuntimeActor.CreateActor(randomName, Actor.IState.Active, 0, culture, randomGender, portrait, out RuntimeActor childActor, out GameObject gameObj);
// Adding the child to the player's family.
if (firstSpouse != null) {
player.AddChild(firstSpouse, childActor); // If there's a spouse, adds the child to both parents.
} else {
player.AddChild(childActor); // If no spouse, adds the child only to the player.
}
// Adding the Character component to the GameObject.
gameObj.AddComponent<Character>();
Description
In this example, a new child character is created for the player and their spouse (if present). The child's gender is randomly determined, and a culturally appropriate name is generated. A corresponding GameObject
is also created, and the new child is added to the player's family. The GameObject
is then enhanced with a Character
component.
Remarks
- Both methods provide flexibility in character creation, supporting diverse and dynamic game worlds.
- The inclusion of cultural background and gender in character creation adds depth to the character's identity and can influence gameplay and narrative.
- These methods are particularly useful in RPGs, simulation games, and any narrative-driven game that requires the introduction of new characters with specific traits and representations.