[Solved] Basic Python

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
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

[Solved] Basic Python

#1 Post by Lezalith » Fri Feb 24, 2017 4:47 pm

I realized the other day I am doing so much in Ren'Py and I can't even do basic Python stuff.
On one hand, it's nice that Ren'Py allows such a coding-noob to create stuff, however, I'm lost when I need stuff like defining functions...

Situation:
Upon entering a screen, if variable PersuasionValue is greater than 100, it is saved onto PersuasionValueStorage before being changed to 100.
Upon exiting the screen, value of PersuasionValue is restored.

Code: Select all

# Defining first function
def albatros():
    global PersuasionValue
    global PersuasionValueStorage
    if PersuasionValue > 100:
        PersuasionValueStorage = PersuasionValue
        PersuasionValue = 100
        
# Defining second function
def pigeon():
    global PersuasionValue
    global PersuasionValueStorage
    PersuasionValue = PersuasionValueStorage

# Then, upon entering the screen, I add it to the button functions.
button:
    background something
    xpos xxxx
    ypos xxxx
    action Show("Screen"),albatros()

# And same when exiting the screen.
button:
    background something
    xpos xxxx
    ypos xxxx
    action Show("Screen"),pigeon()
The error I get is Expected Statement when defining both functions.

Code: Select all

def albatros->():
def pigeon->():
I thought that's how functions are defined?
It even used to work, then I deleted that piece of code, tried something else, and wrote back the same thing (What you see above here).


Follow-up question:
If I understood variables correctly, when I do

Code: Select all

$ PersuasionValue = 20
I am declaring a global variable, thus I need to write

Code: Select all

global PersuasionValue
when using that variable inside the defined function, right?
Last edited by Lezalith on Sat Feb 25, 2017 7:06 am, edited 1 time in total.

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: Basic Python

#2 Post by theCodeCat » Fri Feb 24, 2017 5:13 pm

You need to put your python function definitions into a "init python:" block.

You are mostly correct about the global variables thing, but you only need to add the "global ..." line if you are modifying the value of the variable. If you are just reading from it, the "global ..." line is not necessary.

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Basic Python

#3 Post by Lezalith » Fri Feb 24, 2017 5:40 pm

Thanks a bunch!

One more question if I may, is it possible to have more conditions in a conditional switch?
Let's say if I wanted to have an image based on time and season.

Code: Select all

image camp = ConditionalSwitch(
"time=day, season=summer","day_summer.jpg"
"time=day, season=winter","day_winter.jpg"
"time=night, season=summer","night_summer.jpg"
"time=night, season=winter","night_winter.jpg"
)
I tried this, but it didn't work.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1458
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Basic Python

#4 Post by xavimat » Fri Feb 24, 2017 8:05 pm

Lezalith wrote:is it possible to have more conditions in a conditional switch?
- You can define a complex condition with "and", but the comma (,) has no meaning in conditions.
- A single equal (=) is used to asing a variable. To check you need double equal (==)
- Always end the condition with an always-True condition, to avoid the ConditionSwitch getting angry.
- Also, there where some commas missing at the end of the lines

Code: Select all

image camp = ConditionalSwitch(
    "time==day and season==summer","day_summer.jpg",
    "time==day and season==winter","day_winter.jpg",
    "time==night and season==summer","night_summer.jpg",
    "True","night_winter.jpg"
)
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Basic Python

#5 Post by Ocelot » Sat Feb 25, 2017 1:13 am

You can define a complex condition with "and", but the comma (,) has no meaning in conditions.
If only. It introduces a tuple and generally makes condition always true.
< < insert Rick Cook quote here > >

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: Basic Python

#6 Post by Lezalith » Sat Feb 25, 2017 7:06 am

Thank you all!

Post Reply

Who is online

Users browsing this forum: Google [Bot], _ticlock_