Character speaks based on a variable?

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
User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Character speaks based on a variable?

#1 Post by wyverngem »

Question:
Can I make a character have dialogue based on a variable? Such as a True or False or even if they are in a list of names?

For example you have a party made up of different people. You have your main character and they can bring two people with them out of five in the game. I don't want to spend my time coding "if BenInParty" or "if 'Ben' in party_list" show dialogue. Could I put it in there defined character instead?

For example it would look like this.
"You bring Ben and Joe with you. You left Tony, Chris, and Ella behind at camp."

Code would be:

Code: Select all

MC "Is he seriously still on his monologue?"
b "I know right?"
t "Ugh, how much longer will this take?"
j "I don't like what he's saying about us, [mc_name]."
e "Ooh, ooh my nails are dry. Let the guy talk more!"
In-game it would appear (If you had Ben and Joe):
MC "I-I think he's still monologuing."
b "I know right?"
j "I don't like what he's saying about us, Alice."
I'm not sure if I'm putting it in the wrong place or if it's just not possible with an error I always get with Python and screen about a Revertable List.

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Character speaks based on a variable?

#2 Post by wyverngem »

I tried writing a condition, but I don't think I'm doing it right. The error is below. I'm simply trying to add a condition to the Character Object and using python to check if the person is in the party.

Code: Select all

define party_list = []

init python:
    global party_list
    def inparty(name):
        if name in party_list:
            return True
        else:
            return False

    def add_member(name):
        if name not in party_list:
            party_list.append(name)

    def remove_member(name):
        if name in party_list:
            party_list.remove(name)

define j = Character("James")
define k = Character("Karen", condition=(inparty('Karen')))

label start:
    $ party_list = []
    if inparty('James'):
        j "I'm here."
    else:
        $ party_list.append("James")
        j "Hold up, I'm coming!"
    "Thanks for showing up James."
    j "Not a problem."
    k "You can't see me."
    "I'm testing if I can see if there are people in a party."
    j "Well, let's get going."
    return

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 31, in script
    k "You can't see me."
TypeError: eval() arg 1 must be a string or code object

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 31, in script
    k "You can't see me."
  File "D:\Creative\Visual Novels\Renpy\renpy\ast.py", line 708, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "D:\Creative\Visual Novels\Renpy\renpy\exports.py", line 1345, in say
    who(what, *args, **kwargs)
  File "D:\Creative\Visual Novels\Renpy\renpy\character.py", line 1050, in __call__
    if not (self.condition is None or renpy.python.py_eval(self.condition)):
  File "D:\Creative\Visual Novels\Renpy\renpy\python.py", line 2059, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "D:\Creative\Visual Novels\Renpy\renpy\python.py", line 2052, in py_eval_bytecode
    return eval(bytecode, globals, locals)
TypeError: eval() arg 1 must be a string or code object

Windows-8-6.2.9200
Ren'Py 7.3.5.606
Advanced Python 1.0
Thu Jan 16 00:09:05 2020

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Character speaks based on a variable?

#3 Post by philat »

I haven't tested it, but the error message itself is telling you that the condition needs to be a string.

Code: Select all

define k = Character("Karen", condition=("inparty('Karen')"))

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Character speaks based on a variable?

#4 Post by wyverngem »

What is a "code object"? I wrote it as a string and it worked fine. Thank you.

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Character speaks based on a variable?

#5 Post by gas »

A code object is a python entity compiled in CPython to be further processed (like a kind of "intermediary" translation). Something we never touch on renpy use.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot