having an object selector?

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:

having an object selector?

#1 Post by elhlyn »

I have several characters that are set up as objects (with their own stat values) and would like to set up a system where if I select them I can have them do something without having to reference that specific character/object for each and every character. any suggestions on how to approach this?

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: having an object selector?

#2 Post by zmook »

Could you be more specific? It might help if you give an example of what you *don't* want to do, and we can tell you how to avoid it.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

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

Re: having an object selector?

#3 Post by elhlyn »

what I'd like to make is basically a "roster" menu of sorts where I can select a character and interact with them such as train them up a specific stat or hang out with them to increase affection. What I don't want is to code the exact same common "activities" for each and every character.

hopefully that clears up any confusion

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

Re: having an object selector?

#4 Post by _ticlock_ »

Hi, elhlyn,

I think you previously had the related question. I guess, the example didn't work for you, or it was a poor explanation from my side (or I just did not get your question.)
(If you look at the example, screen select_character is simple version of "roster" menu. The selected character passed to the variable npc. Then you can do with this selected npc all sorts of "activities" without specifying who exactly this character is.)

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

Re: having an object selector?

#5 Post by elhlyn »

Unfortunately the main issue I had when I was utilizing this code was on the issue of repeating characters when I wished to have them unique rather than potential duplicates. I am wrong on 2 fold for not reaching out again and more importantly not being very clear in explaining what I'd like to do.

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: having an object selector?

#6 Post by zmook »

Please write out a sample of what you want your code to do for one character, and then we can maybe show you how to generalize it. I still really don't have a handle on what you're looking for.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

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

Re: having an object selector?

#7 Post by elhlyn »

just a quick example

Code: Select all

label training
$ selected = char_tom
"what would you like to do?"
menu:
    "traing":
        "you trained their strength"
        $selected.strength +=1
        jump ("train")
    "hang out":
        "you hung out."
        $selected.affection += 1
        jump("training")
#and if I want to choose another character, let's say Jill,
label training
$ selected = char_jill
"what would you like to do?"
menu:
    "train":
        "you trained their strength"
        $selected.strength +=1
        jump ("training")
    "hang out":
        "you hung out."
        $selected.affection += 1
        jump("training")

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: having an object selector?

#8 Post by zmook »

You want your code to look like this:

Code: Select all

init python:
    class NPC():
        def __init__(self, name, strength=0, affection=0):
            self.name = name
            self.strength = strength
            self.affection = affection


default char_tom = NPC("Tom")
default char_jill = NPC("Jill")


label char_selector:
    "Who do you want to work with?"
    menu:
        "Tom":
            "Tom is by the bar."
            $ current_npc = char_tom
        "Jill":
            "Jill is climbing a tree."
            $ current_npc = char_jill
    call training(current_npc)
    "Training is done."
    "[current_npc.name]'s stats are now: strength=[current_npc.strength], affection=[current_npc.affection]."
    $ current_npc = None
    jump char_selector


label training(selected):
    "what would you like to do with [selected.name]?"
    menu:
        "train":
            "you trained their strength"
            $selected.strength +=1
        "hang out":
            "you hung out."
            $selected.affection += 1
            
    "[selected.name] has to go now."
    return
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

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

Re: having an object selector?

#9 Post by elhlyn »

Thanks!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], decocloud, sittingox