How do I avoid unicode?

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
sinjin25
Newbie
Posts: 5
Joined: Sun Dec 19, 2010 4:55 pm
Contact:

How do I avoid unicode?

#1 Post by sinjin25 » Sun Dec 19, 2010 5:54 pm

So I've been experimenting with making a text-based rpg since, uh, yesterday morning, and have run into this error:
TypeError: %d format: a number is required, not unicode
The game crashes at line 123 which would be:

Code: Select all

    "You have %(dmg)d attack."
Background:
-I've based my code upon example 2 of this framework http://www.renpy.org/wiki/renpy/doc/coo ... ney_System
-I don't know much about Python or classes, so I've been figuring it out through patterns (in the above framework) and seeing what crashes when I do certain things
-I actually got a full battle system working, but it started crashing when I decided to make more advanced stats (like armor, accuracy, dodge rate, etc) using equations

Question:
So, my question is what exactly is crashing the game? I'm assuming what the error refers to as "unicode" would be when instead of saying, "dmg= 5" I say, "dmg = str*.5" or something (so like, a formula rather than a value). Exactly how do I get the game to display my stats in the dialogue without crashing?

Error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

TypeError: %d format: a number is required, not unicode

While running game code:
 - script at line 123 of C:\Program Files\renpy-6.11.0\Current/game/script.rpy
Code (I've removed a lot of unrelated lines):

Code: Select all

# You can place the script of your game in this file.

init:
    # Declare images below this line, using the image statement.
    # eg. image eileen happy = "eileen_happy.png"

    # Declare characters used by this game.
    $ e = Character('Eileen', color="#c8ffc8")

init python:
    #THIS is the player CLASS in which my game CRASHES
    class Player:
        #def __init__(self, hp=25, mp=10, atk=5):
        def __init__(self, end=10, str=10, int=10, dex=10, wdmg=0):
            #self.end = end means that self.end is turned into a global value or w/e that can be reffered to outside of player. which kind of matters in a lot of cases.
            self.end = end
            self.str = str
            self.int = int
            self.dex = dex
            self.wdmg = wdmg
            
        #def update(self, hp, arm, wdmg, dmg, mp, sdmg, acc, dodge, crit): <<--- backup code so I don't have to retype so much (NOT from the time my code actually worked)
        def update(self, end, str, int, dex, wdmg):
            self.hp = end*10
            #I tried the below line after creating the formula in some kind of effort to have a variable that wasn't "unicode." It didn't work.
            hp = self.hp
            self.arm = end*.25
            #This is another example of the above comment
            arm = self.arm
            self.wdmg = wdmg
            self.dmg = str*.5 + wdmg
            dmg = 0
            self.mp = int*4
            mp = self.mp
            self.sdmg = int*.7
            sdmg = self.sdmg
            self.acc = dex*.6
            acc = self.acc
            self.dodge = dex*.5 + 2
            dodge = self.dodge
            self.crit = dex*.25
            crit = self.crit
            #since dmg has an equation invovled in its value it is considered unicode. %()d requires a normal value and cannot use unicode.
            
   [size=85] ##Lines HERE TO##   
    class Enemy:
        def __init__(self, hp=1, mp=1, atk=1):
            self.hp = hp
            self.mp = mp
            self.atk = atk
            
        def update(self, hp, mp, atk):
            self.hp = hp
            self.mp = mp
            self.atk = atk
    ##HERE PROBABLY have nothing to do with the crash or error (to my understanding)##[/size]

# The game starts here.
label start:

    python:
        #Classes are reffered to using "ClassName()" without the ""
        player = Player()
        enemy = Enemy()
    
    $ enemy.update(20,5,3)
    
    #$eatk = enemy.atk
    #$ehp = enemy.hp
    #$emp = enemy.mp
    #$defended =enemy.atk*.8
    
    
    
    $ player.update(player.end, player.str, player.int, player.dex, player.wdmg)
    
    #$atk = player.dmg
    #$hp = player.hp
    #$mp = player.mp
    #$heal = player.hp/10

    "Now I have %(current_money)d coins."
    "You have %(dmg)d attack."
#Crash point ^^^
    "You have %(hp)d hit points."
    "You have %(mp)d mana points."
    "The enemy has %(eatk)d attack."
    "The enemy has %(ehp)d hit points."
    "The enemy has %(emp)d mana points."
    jump Epic_Battle
    
Hopefully I've done. This is my first time posting in a forum since 2005.

(2nd question, a bit unrelated):
Is there a way to shorten a line of code (kind of like functions in flash)?
For example:
would it be possible to shorten this:

Code: Select all

    menu:
        "attack":
            $ ehp -= atk
            "You strike the enemy for %(atk)d points of damage!"
            $ hp -= eatk
            "The enemy attacks!"
            "You lost %(eatk)d hit points!"
            jump hp_check
Into something like:
attack()
^(If I remember correctly, you could do something to that effect in flash).

Thanks for reading.

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: How do I avoid unicode?

#2 Post by Showsni » Sun Dec 19, 2010 6:16 pm

For the first one, you could try setting a different variable to the str value beforehand; I think that would work... Something like:

$ currentdmg = dmg

just before you put:

"You have %(currentdmg)d attack."

For the second... Yeah, you can define things in python to be whatever. I'm not an expert, though. Something like

Code: Select all

python:
    def attack():
            $ ehp -= atk
            "You strike the enemy for %(atk)d points of damage!"
            $ hp -= eatk
            "The enemy attacks!"
            "You lost %(eatk)d hit points!"
            jump hp_check
maybe? I don't really know... Try it and see if it works, I guess...

sinjin25
Newbie
Posts: 5
Joined: Sun Dec 19, 2010 4:55 pm
Contact:

Re: How do I avoid unicode?

#3 Post by sinjin25 » Sun Dec 19, 2010 8:38 pm

Yeah, defining the variables in "init" works, but defining it in "init python" or whatever it's called doesn't. The problem is that doing that takes like 15 lines, considering the amount of stats I have is about that number, which kind of destroys the point of using classes and such in the first place. Which means the second question is even more important now. If it's possible to create a function that sets the values to their appropriate amounts, then classes are once again useful (in my opinion).

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: How do I avoid unicode?

#4 Post by PyTom » Sun Dec 19, 2010 8:44 pm

str is the name of a python builtin, so if you use it in your own code, you can have problems.

To debug the first problem, try seeing what "%(dmg)r" outputs.

For now, formatting only works from the store. You can access the store from inside a function, using code like:

store.dmg = expression
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

sinjin25
Newbie
Posts: 5
Joined: Sun Dec 19, 2010 4:55 pm
Contact:

Re: How do I avoid unicode?

#5 Post by sinjin25 » Mon Dec 20, 2010 2:19 am

Wow, really? No wonder I was running into problems with it. Either way, I've gotten a few methods now, so all that's left is to try them out tomorrow and see what works.
Thanks for the speedy replies; consider this post technically solved, although I'm still open to ideas in case anyone has any other alternatives.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]