Printing Strings With %

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
Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Printing Strings With %

#1 Post by Nero »

I was wondering if there is easier way of doing conditional operations like this.


This works perfectly fine but can I like group certain set of strings that can be used if conditions are met?

Code: Select all

    idle LiveComposite(
        (120, 170),  # x y pos
        (0.095, 0.650), Text("%s%s%s%s%s%s" % ("Deal" if c.dmg > 0 else "" ,c.dmg if c.dmg > 0 else "" , "damage." if c.dmg > 0 else "", "Gain"if c.armor > 0 else "",c.armor if c.armor > 0 else "","armor." if c.armor > 0 else "" ) ),
        )
I tried working with something like this but (Gain % armor) part gets ignored...

Code: Select all

    idle LiveComposite(
        (120, 170),  # x y pos
        (0.095, 0.650), Text("%s%s%s" % ("Deal",c.dmg,"damage.") if c.dmg > 0 else "", ("Gain",c.armor,"armor.") if c.armor > 0 else ""),
        )
So if anyone can give me idea how to print those variables and strings in a more efficient way?

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

Re: Printing Strings With %

#2 Post by gas »

You should use each conditional on all arguments,non the whole tuple.

Something like
Text("%s%s%s" % (("Deal" if c.dmg > 0 else "Gain" if c.armor > 0 else ""),(c.dmg if... else c.armor if...)))

As you can see this is not the best thing to do, so probably you should compute such values in advance with a function.
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

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Printing Strings With %

#3 Post by Nero »

That's kind of sad that there is no way around it. Oh well I have to figure out some other way then thanks.

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

Re: Printing Strings With %

#4 Post by gas »

Well, is NOT so hard or dramatical, I mean... just use placeholders.

Code: Select all

default st_msg = "something"
default st_score = ""
default st_what = "?!?!"

idle LiveComposite(
        (120, 170),  # x y pos
        (0.095, 0.650), Text("%s%s%s" % (st_msg,st_score,st_what)),
        )
In the moment you deliver an hit or whatever such game require, also compute such output variables the way you did early in the LiveComposite and you're fine to go.
To me, this sound easier to code and read (and customize too).
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: No registered users