CreateActor: Difference between revisions
Tayfunwiki (talk | contribs) No edit summary |
Tayfunwiki (talk | contribs) No edit summary |
||
Line 49: | Line 49: | ||
=== Example of Usage === | === Example of Usage === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
using Nullframes.Intrigues; | |||
using UnityEngine; | |||
using System.Linq; | |||
public class RuntimeActor_Demo : MonoBehaviour { | |||
public Sprite babyPortrait_f; | |||
public Sprite babyPortrait_m; | |||
// | public void MakeChild_ForPlayer() { | ||
Actor | // Reference the player actor | ||
Actor player = IM.Player; | |||
// | // Get the player's first spouse, if any | ||
Actor firstSpouse = player.Spouses().FirstOrDefault(); | |||
// Choose a random gender | |||
Actor.IGender randomGender = Actor.RandomGender; | |||
// Retrieve the player's culture | |||
Culture culture = player.Culture; | |||
// Generate a random name based on culture and gender | |||
string randomName = culture.GenerateName(randomGender); | |||
// Choose the baby portrait based on gender | |||
var portrait = randomGender == Actor.IGender.Female ? babyPortrait_f : babyPortrait_m; | |||
// Create a new actor with the generated details and add it to the game | |||
RuntimeActor.CreateActor(randomName, Actor.IState.Active, 0, culture, randomGender, portrait, | |||
out RuntimeActor childActor, out GameObject gameObj); | |||
// | // Add the child to the player's family | ||
if (firstSpouse != null) { | |||
player.AddChild(firstSpouse, childActor); | |||
} | |||
else { | |||
player.AddChild(childActor); | |||
} | |||
// | // Add a character component to the game object | ||
gameObj.AddComponent<Character>(); | |||
} | |||
} | |||
} | } | ||
</syntaxhighlight> | |||
==== Description: ==== | |||
</ | * Player Reference: The script starts by referencing the current player character using the <code>IM.Player</code> property. | ||
* Spouse and Gender: It then retrieves the player's first spouse and randomly selects a gender for the new child. | |||
* Culture and Name: The child's name is generated based on the player's culture and the randomly chosen gender. | |||
* Portrait Selection: A portrait sprite is selected based on the gender of the child. | |||
* Actor Creation: The <code>CreateActor</code> method of <code>RuntimeActor</code> is called to create a new actor instance with these details. | |||
* Family Integration: The new actor (child) is added to the player’s family. | |||
* Game Object Setup: Finally, a GameObject representing the child is created and configured in the game world. | |||
==== | ==== Use Case: ==== | ||
This example can be used in games featuring family dynamics, where players can have children who inherit certain traits or roles. The script shows how to integrate new actors into the game world, reflecting changes in the player's family status. | |||
==== Remarks ==== | ==== Remarks ==== |
Revision as of 15:08, 25 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
using Nullframes.Intrigues;
using UnityEngine;
using System.Linq;
public class RuntimeActor_Demo : MonoBehaviour {
public Sprite babyPortrait_f;
public Sprite babyPortrait_m;
public void MakeChild_ForPlayer() {
// Reference the player actor
Actor player = IM.Player;
// Get the player's first spouse, if any
Actor firstSpouse = player.Spouses().FirstOrDefault();
// Choose a random gender
Actor.IGender randomGender = Actor.RandomGender;
// Retrieve the player's culture
Culture culture = player.Culture;
// Generate a random name based on culture and gender
string randomName = culture.GenerateName(randomGender);
// Choose the baby portrait based on gender
var portrait = randomGender == Actor.IGender.Female ? babyPortrait_f : babyPortrait_m;
// Create a new actor with the generated details and add it to the game
RuntimeActor.CreateActor(randomName, Actor.IState.Active, 0, culture, randomGender, portrait,
out RuntimeActor childActor, out GameObject gameObj);
// Add the child to the player's family
if (firstSpouse != null) {
player.AddChild(firstSpouse, childActor);
}
else {
player.AddChild(childActor);
}
// Add a character component to the game object
gameObj.AddComponent<Character>();
}
}
Description:
- Player Reference: The script starts by referencing the current player character using the
IM.Player
property. - Spouse and Gender: It then retrieves the player's first spouse and randomly selects a gender for the new child.
- Culture and Name: The child's name is generated based on the player's culture and the randomly chosen gender.
- Portrait Selection: A portrait sprite is selected based on the gender of the child.
- Actor Creation: The
CreateActor
method ofRuntimeActor
is called to create a new actor instance with these details. - Family Integration: The new actor (child) is added to the player’s family.
- Game Object Setup: Finally, a GameObject representing the child is created and configured in the game world.
Use Case:
This example can be used in games featuring family dynamics, where players can have children who inherit certain traits or roles. The script shows how to integrate new actors into the game world, reflecting changes in the player's family status.
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.