CreateActor: Difference between revisions

From Intrigues Wiki
No edit summary
No edit summary
Line 49: Line 49:
=== Example of Usage ===
=== Example of Usage ===
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
Actor player = IM.Player; // Gets the Player actor.
using Nullframes.Intrigues;
using UnityEngine;
using System.Linq;


Actor firstSpouse = player.Spouses().FirstOrDefault(); // Retrieves the player's first spouse.
public class RuntimeActor_Demo : MonoBehaviour {
    public Sprite babyPortrait_f;
    public Sprite babyPortrait_m;


// Generating random gender and name for the child.
    public void MakeChild_ForPlayer() {
Actor.IGender randomGender = Actor.RandomGender;
        // Reference the player actor
Culture culture = player.Culture;
        Actor player = IM.Player;
string randomName = culture.GenerateName(randomGender);


// Selecting the portrait based on the child's gender.
        // Get the player's first spouse, if any
Sprite portrait;
        Actor firstSpouse = player.Spouses().FirstOrDefault();
if (randomGender == Actor.IGender.Female) {
 
    portrait = babyPortrait_f; // Female baby portrait. (Sprite object)
        // Choose a random gender
}
        Actor.IGender randomGender = Actor.RandomGender;
else {
 
    portrait = babyPortrait_m; // Male baby portrait. (Sprite object)
        // 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);


// Creating the child actor and corresponding GameObject.
        // Add the child to the player's family
RuntimeActor.CreateActor(randomName, Actor.IState.Active, 0, culture, randomGender, portrait, out RuntimeActor childActor, out GameObject gameObj);
        if (firstSpouse != null) {
            player.AddChild(firstSpouse, childActor);
        }
        else {
            player.AddChild(childActor);
        }


// Adding the child to the player's family.
        // Add a character component to the game object
if (firstSpouse != null) {
        gameObj.AddComponent<Character>();
    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.
}
}
</syntaxhighlight>


// Adding the Character component to the GameObject.
==== Description: ====
gameObj.AddComponent<Character>();


</syntaxhighlight>
* 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.


==== Description ====
==== Use Case: ====
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.
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 16: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 of RuntimeActor 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.