What's the Best Way to Define Character Stats

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
Bjoern
Newbie
Posts: 5
Joined: Mon Dec 15, 2014 2:12 am
Contact:

What's the Best Way to Define Character Stats

#1 Post by Bjoern »

Greetings,

I've been playing around with Ren'py trying to create custom menus and screens. While I've discovered quite a bit by reading forums and experimenting, I think I've hit a wall. So, I think I need to go back to square one and try to learn things correctly so I can achieve my goals.

To start off, I would like to create characters with different numerical stats. Later on, I would like to have a stats screen where people can view them. Instead of making an individual stat screen for each character, I could make one screen that would call up the stats after declaring which character I want to choose.

Here are my ideas so far.

Code: Select all

init:
     $ charlist = ["Alex", "Bob", "Charlie"] # Characters can be added or removed later on in the game.
     $ alexstat = [100, 10, 15, 12] # Stats [HP, Strength, Intelligence, Stamina]
     $ bobstat = [100, 12, 13, 12]
     $ charliestat = [100, 17, 10]
Is this a good way to define characters and their stats? Is there a more efficient way to do this?

Thanks for your help in advance.

Ryue
Miko-Class Veteran
Posts: 745
Joined: Fri Nov 02, 2012 8:41 am
Projects: Red eyes in the darkness
Contact:

Re: What's the Best Way to Define Character Stats

#2 Post by Ryue »

There are a couple of different ways to do this the mainquestion here is though how deep you want to go into programming.

Your method is fully valid

Another option would be to use a dictionary instead of a list of the atats so that you say "strength" has value 100 instead of having a list of stats where the first one has value 100.

A third option would be to use classes.


The order here is also how deep you need to go into programming.
So as i said the first part is i think to define how deep you want to go into programming (if you dont want to go into it too deep the classes approach wouldnt be good)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: What's the Best Way to Define Character Stats

#3 Post by xela »

Flip them to classes:

Code: Select all

class Char(object):
    def __init__(self, name, stats):
        self.name = name
        self.hp = stats[0]
        self.strength = stats[1]
        self.intelligence = stats[2]
        self.stamina = stats[3]

alex = Char(charlist[0], alexstat)
bob = Char(charlist[1], bobstat)
charlie = Char(charlist[2], charliestat)
I generally dislike using basic containers for characters but a dict or your way will work as well...
Like what we're doing? Support us at:
Image

Bjoern
Newbie
Posts: 5
Joined: Mon Dec 15, 2014 2:12 am
Contact:

Re: What's the Best Way to Define Character Stats

#4 Post by Bjoern »

Thanks a bunch to both of you for your help. I was thinking about 'class', but I haven't really learned about that yet. I've mostly learned about 'variables' and 'lists' in my studies of Python programming. I've done the first third of classes for Python, Ruby, and JavaScript on Code Academy. I am definitely willing to learn if need be. One of my major goals is not to create an individual screen for each character for each action/event that happens.

Your questions also leads into my next step that I am trying to figure out. Below is part of the menu system that I have been working on. Current "charlist" is the list of characters that are in your group. As the game develops, Characters will be added and removed. With the first menu, the the list of characters are displayed as buttons. When their names is clicked, the Character Info screen will appear with their stat information.

Code: Select all

screen char_menu:
    tag menu
    frame pos (10, 60) xminimum 580:
        vbox:
            for x in range(0, len(charlist)):
                $ chardisplay = charlist[x]
                textbutton _("[chardisplay]") action [SetVariable("currentchar", chardisplay), ShowMenu("char_info")]
                
screen char_info:
    tag menu
    frame pos (10, 60) xminimum 580:
        vbox:
            label "[currentchar]"
            label "Strength: [alexstat[1]]"
            textbutton _("Back") action Return()
As you can see so far, I can display the screen with the Character's name, but I don't know how to pull their information yet. As of now, I just wrote in the filler "alexstat[1]", but that would not work for my goals. I would like to have it display Alex's Strength stat after it pulled the information from my character stat section.

So, this loops back to what method I would want to store the stat data. As you can see my secondary goal, what method would you recommend and how would I be able to pull? If you believe that class is the best way, I would love to learn.

Thanks again for your help!

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: What's the Best Way to Define Character Stats

#5 Post by xela »

Code: Select all

init python:
    class Char(object):
        def __init__(self, name, stats):
            self.name = name
            self.hp = stats[0]
            self.strength = stats[1]
            self.intelligence = stats[2]
            self.stamina = stats[3]
            
            chars[name] = self

screen char_menu:
    tag menu
    frame pos (10, 60) xminimum 580:
        vbox:
            for i in chars:
                textbutton _("[i]") action ShowMenu("char_info", char=chars[i])
               
screen char_info(char=None):
    tag menu
    frame pos (10, 60) xminimum 580:
        vbox:
            label "[char.name]"
            label "Strength: [char.strength]"
            textbutton _("Back") action Return()
            
label start:
    python:
        chars = dict()
        alex = Char("Alex", [100, 10, 15, 12])
        bob = Char("Bob", [100, 12, 13, 12])
        charlie = Char("Charlie", [100, 17, 10, 12])
        
    call screen char_menu
Like what we're doing? Support us at:
Image

Bjoern
Newbie
Posts: 5
Joined: Mon Dec 15, 2014 2:12 am
Contact:

Re: What's the Best Way to Define Character Stats

#6 Post by Bjoern »

xela, thanks a bunch for your help! This definitely helps me achieve what I want. I was able to put that into my script and it worked wonderfully. Additionally, I was able to edit it a bit to represent whether the character was active or not by adding another stat.

I am going to go to bed now and contemplate how to add a button to the char_info screen that will increase their strength stat by 1 tomorrow at work.

If you have any suggestions on how to do this, I would be very grateful.

Thanks again for all of your help!

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: What's the Best Way to Define Character Stats

#7 Post by xela »

Code: Select all

textbutton _("Add Strength") action SetField(char, "strength", char.strength + 1)
Like what we're doing? Support us at:
Image

Bjoern
Newbie
Posts: 5
Joined: Mon Dec 15, 2014 2:12 am
Contact:

Re: What's the Best Way to Define Character Stats

#8 Post by Bjoern »

Thanks a bunch! That worked perfectly!

Post Reply

Who is online

Users browsing this forum: BadMustard, Ocelot