Passing Renpy variables to Python code.

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
Zeroized
Regular
Posts: 33
Joined: Sat Mar 23, 2013 12:33 am
Contact:

Passing Renpy variables to Python code.

#1 Post by Zeroized »

I've been searching around the forum for simple battle engines, and I've found one that I like and seems simple enough to use. The attacks are defined in this way: Attack = Skill("Attack", (mp cost), (chance to hit), (damage)), and the fighting characters are defined in this way: player = Fighter ("Charname", HP, MP, [Attack, Talents, Wait]).

"Charname" was defined earlier by the user, the same with STR, AGI, HP, MP and so on, using Renpy. Since I'm working on a RPG, the stats will be changing from time to time. The variable "Charname" works just as intended.
The problem begins when I try to change the values in between parentheses to variables, for example, when I change Attack = Skill("Attack", (mp cost), [AGI], [STR]), from fixed values to variables, I get syntax errors. I've tried using brackets, parenthesis and quotes, nothing works. Since I'm going to be using a lot of variables, I want to learn how to properly pass Renpy variables to Python.

Thanks in advance. Here is the code, if someone wants to look at it. It's not mine, I've found it using the search function on the forum.

Code: Select all

init -1 python:
    class Fighter():
        
        def __init__(self, name, max_hp, max_mp, skills):
            self.name = name
            self.max_hp = max_hp
            self.hp = max_hp
            self.max_mp = max_mp
            self.mp = max_mp
            self.skills = skills
    
        def auto_select(self):
            while True:
                selected = renpy.random.choice(self.skills)
                if selected.cost<self.mp:
                    return selected
    
        def defence(self):
            if user.skill.hit<renpy.random.randint (0,100):
                renpy.say (adv, ("%s dodged the attack" %self.name))
                return
            else:
                self.hp -= user.skill.power
                renpy.say (adv, ("%s got %d damage" %(self.name, user.skill.power)))
                return
            
    class Skill():
        def __init__(self, name, cost, hit, power):
            self.name = name
            self.cost = cost
            self.hit = hit
            self.power = power
            
        def use(self): 
            user.mp -= self.cost
            renpy.say (adv, ("%s used %s" %(user.name, self.name)))
            target.defence()
            return
            
    def battle(win, lose):
        global player, enemy, user, target
        renpy.show_screen("battle_ui", zorder="2")
        while True:
            renpy.say (adv, "Skill select", interact=False)
            player.skill = renpy.call_screen("skill_select")
            enemy.skill = enemy.auto_select()
            user = player
            target = enemy
            user.skill.use()
            if target.hp<1:
                renpy.jump(win)
            user = enemy
            target = player
            user.skill.use()
            if target.hp<1:
                renpy.jump(lose)
                                            
screen skill_select:    
    vbox xpos .05 ypos .3:
        for i in player.skills:
            if i.cost <= player.mp:
                textbutton "[i.name]" action Return (value=i)
            else:
                textbutton "[i.name]"

screen battle_ui:    
    frame xmaximum 200 xanchor 1.0 xpos 0.99 yalign 0.10:
        has vbox
        hbox:
            text "[enemy.name]"
        hbox xfill True:
            text "HP"
            text "[enemy.hp]/[enemy.max_hp]" xalign 1.0
        hbox xfill True:
            text "MP"
            text "[enemy.mp]/[enemy.max_mp]" xalign 1.0
            
    frame xmaximum 200 xpos 0.01 yalign 0.10:
        has vbox
        hbox:
            text "[player.name]"
        hbox xfill True:
            text "HP"
            text "[player.hp]/[player.max_hp]" xalign 1.0
        hbox xfill True:
            text "MP"
            text "[player.mp]/[player.max_mp]" xalign 1.0

label start:
    $ Attack = Skill("Attack", 0, 50, 10)
    $ Talents = Skill("Talents", 15, 100, 20)
    $ Wait = Skill("Wait", 0, 100, 0)
    $ player = Fighter ("Charname", 50, 50, [Attack, Talents, Wait])
    $ enemy = Fighter ("Goblin", 50, 50, [Attack, Talents])
    $ battle("win", "lose")
    
label win:
    "you won"
    return
    
label lose:
    "you lost"
    return

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Passing Renpy variables to Python code.

#2 Post by saguaro »

Are you giving the variable a value before you plug it in?

Code: Select all

label start:
    $ attack_hit = 50
    $ Attack = Skill("Attack", 0, attack_hit, 10)

User avatar
Zeroized
Regular
Posts: 33
Joined: Sat Mar 23, 2013 12:33 am
Contact:

Re: Passing Renpy variables to Python code.

#3 Post by Zeroized »

Yes, the variables are defined elsewhere. But your post gave me a pretty good clue about how to fix the issue.
Apparently, to call variables I don't need to put anything besides the name of the variable.
Thanks.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], bonnie_641, Google [Bot]