Is there a way to replace all of the information of a character and object

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
elhlyn
Regular
Posts: 61
Joined: Wed Aug 01, 2012 1:13 am
Projects: Manorialism
Contact:

Is there a way to replace all of the information of a character and object

#1 Post by elhlyn »

I wanted to organize character files separately and have them be interchangeable, in a sense that you can have "npc1" be referring to a character and object named Tom and his related stats and info or as Jill with her related stats and info. I found out that you can replace the "character" names with that of a placeholder (in this example npc1) as well as as each and every stat as seen below.

Code: Select all

define character.npc1 = Character ("[npc1]")
define npc1 = char_stats ("",0,0)

$character.npc1 = character.lu
$npc1.strength = lu.strength

npc1 "my strength is [npc1.wisdom]"
in the character file

Code: Select all

define character.lu = Character ("Luna", image = "Luna")
define lu = char_stats ("Luna",3,4)

It works fine but I wonder if there is a way to replace all of that information in an aggregate way without having to manually define each and every "stat". unless that's the only way, but I worry as I intend to create "traits" (not exactly sure how to approach that but would love some advice on that) for each character.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Is there a way to replace all of the information of a character and object

#2 Post by _ticlock_ »

Hi, elhlyn,

I am not sure if I got right what you are asking, but you don't need to create character files separately and then replace npc with one of them. I believe you want to use pre-defined characters to select from one of them and maybe to slightly modify their data. To address that, you can create, for example, a dictionary that has pre-defined data for some characters and use this data when you create an instance of your class. A simplified example:

Code: Select all

init python:
    class Person():
        pre_defined = [
            {   'name' : 'Tom',
                'image': 'eileen',
                'strength': 3,
                'dexterity': 2
            },
            {   'name' : 'Jill',
                'image': 'eileen',
                'strength': 1,
                'dexterity': 4
            }
        ]
        def __init__ (self):
            # Random character based on pre_defined
            n = len(Person.pre_defined)-1
            i = renpy.random.randint(0,n)
            self.name = Person.pre_defined[i]['name']
            self.image = Person.pre_defined[i]['image']
            self.strength = Person.pre_defined[i]['strength']
            self.dexterity = Person.pre_defined[i]['dexterity']
            self.c = Character(self.name, image = self.image)

default npc = None

label start:
    $ npc = Person()
    npc.c "My strength is [npc.strength] and dexterity is [npc.dexterity]"
You can put the list/dictionary pre_defined in a separate file if you wish.

elhlyn
Regular
Posts: 61
Joined: Wed Aug 01, 2012 1:13 am
Projects: Manorialism
Contact:

Re: Is there a way to replace all of the information of a character and object

#3 Post by elhlyn »

Thank you for the reply! this is a useful method and I will look in to testing it out more to have a better understanding. I apologize for the way I am phrasing the question and explanation as I am both very inexperienced in programming, and it's a bit difficult to describe what I wish to do.

the end goal is to make a character management game where you have, let's say starting out at 20 characters but you can go in to the game files, insert images and .rpy file (or .txt, I am not experience enough to do that) and include those character in the game as people you can train and work with.
the main issue is trying to set up coding that will have characters do this or that but not state these specific characters,

i.e. $npc.strength += 1
as apposed to
$john.strength += 1

as it is the intent that both it could include additional characters that have not been hardcoded in the game as well as the fact that this method will be repeated with every character in the game.

I've also come to realize that even if I didn't have to worry about how to append additional characters, I have to somehow figure out how to increase the stats of a variety of characters without having to specifically write each character's name down.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Is there a way to replace all of the information of a character and object

#4 Post by _ticlock_ »

Ok. Here is an example that might be better than the explanation (with randomly added characters):

Code: Select all

#List that stores all characters that player can currently interact with. Initially just one random character
default party = [Person()]

#Currently selected character
default npc = party[0]

#Screen to select character. The variable npc changes its value to the selected character
screen select_character():
    vbox:
        for i in range(0,len(party)):
            textbutton party[i].name action SetVariable('npc',party[i]), Jump('main')
            
label start:
    
label main:
    menu:
        "Add character":
            jump add_character
        "Select character":
            call screen select_character()
        "Interact with selected character":
            jump interact_character

label add_character:
    $ party.append(Person())
    $ npc = party[-1]
    npc.c "I am added to your party. My strength is [npc.strength] and dexterity is [npc.dexterity]"
    jump main

label interact_character:
    npc.c "What do you want to talk about?"
    jump main

elhlyn
Regular
Posts: 61
Joined: Wed Aug 01, 2012 1:13 am
Projects: Manorialism
Contact:

Re: Is there a way to replace all of the information of a character and object

#5 Post by elhlyn »

so to add characters, I have to modify the "Person" class? or what would be the format to create characters?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Is there a way to replace all of the information of a character and object

#6 Post by _ticlock_ »

elhlyn wrote: Mon Mar 15, 2021 10:15 pm so to add characters, I have to modify the "Person" class? or what would be the format to create characters?
You create a character every time you write Person(...). For the game, you create them only when you need one. For example, if you want the player to select one of the characters to add to the party, you can create as many as you'd like to provide a selection for the player, but once the player "buy" a character, you add this character to the party list and discard the others. The player can anytime select one of the characters that are in the party as shown in the example code.

If you want to add a new character(s) to the pre_defined character list you can directly add them to the pre_defined.

You definitely need to modify "Person" class to fit your needs. For example, if you want to create a particular character (let's say by index in pre_defined list) you can put an argument in the __init__ method:

Code: Select all

def __init__ (self, i = None):
    if i == None: # If index is not specified - random character based on pre_defined
        n = len(Person.pre_defined)-1
        i = renpy.random.randint(0,n)
    self.name = Person.pre_defined[i]['name']
    ...
Then Person(i = 0) create "Tom", Person(i = 1) create "Jill"

elhlyn
Regular
Posts: 61
Joined: Wed Aug 01, 2012 1:13 am
Projects: Manorialism
Contact:

Re: Is there a way to replace all of the information of a character and object

#7 Post by elhlyn »

got it, got it. I deeply appreciate you taking your time and being very patient with me, but how do show images that correspond to the "selected" character? can't seem to make it work despite the images being defined?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Is there a way to replace all of the information of a character and object

#8 Post by _ticlock_ »

elhlyn wrote: Tue Mar 16, 2021 12:44 am got it, got it. I deeply appreciate you taking your time and being very patient with me, but how do show images that correspond to the "selected" character? can't seem to make it work despite the images being defined?
Let's say you have:

Code: Select all

image side eileen = "side eileen.png"
image eileen = "eileen.png"
image eileen happy = "eileen happy.png"
image eileen sad = "eileen sad.png"
(Well, you don't actually need to define the images if you name the image files like that)

The side image should be automatically shown when npc speaks since we define character with this image tag:

Code: Select all

self.c = Character(self.name, image = self.image)
To show other images you have to use show expression statements since your image name is written in the variable.

For convenience let's add a function to the class to return image name with different emotions:

Code: Select all

init python:
    class Person():
        ...
        def img(self, mood=None):
            if mood == None:
                return self.image
            return ' '.join([self.image, mood])
Then in script:

Code: Select all

label interact_character:
    show expression npc.img() as npc_img
    "eileen.png is shown"
    show expression npc.img('happy') as npc_img
    "eileen happy.png is shown"
    hide npc_img 
    npc.c "Side image is shown"
    jump main
Note: The tag npc_img helps to show only one image of npc and a way to hide the image.

elhlyn
Regular
Posts: 61
Joined: Wed Aug 01, 2012 1:13 am
Projects: Manorialism
Contact:

Re: Is there a way to replace all of the information of a character and object

#9 Post by elhlyn »

Again, thank you very much.

Post Reply

Who is online

Users browsing this forum: No registered users