Switch variable at random intervals.

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
Inksword
Regular
Posts: 83
Joined: Fri Oct 24, 2014 1:20 am
Tumblr: inksword
Contact:

Switch variable at random intervals.

#1 Post by Inksword »

I'm very much a beginner attempting my first programming by myself so forgive if it's a simple issue.

I've been attempting to have a variable that switches from true to false and back again at random real-time intervals. I thought I'd be able to use renpy's time function to do it but haven't had any luck using it as described for non-jumping purposes. I decided to make a custom function that would do it instead and have been trying to get it to work. This is what I have so far (no time even involved yet) but I haven't been successful.

The variable is intended to be referenced during choices like a flag eventually.

Here's my code I've been testing with, Eileen in this example only says false (the default value for watched)

Code: Select all

init 999 python:
    config.developer = True
    config.console = True

define e = Character("Eileen")

default watched = False

init python:
    currentwatch = False
    def swapWatch(currentwatch):
        if currentwatch is True:
            watched = False
        else:
            watched = True


# The game starts here.

label start:
    scene bg room
    show eileen happy  

    # These display lines of dialogue.

    e "[watched]"
    e "[watched]"
    
    $ swapWatch(watched)
    
    e "[watched] (should be swapped)"
    e "[watched]"
    
    $ swapWatch(watched)
    
    e "[watched] (should be swapped back)"
    e "[watched]"
I feel like as a beginner to python I must not quite be understanding how the python section of the code is interacting with the renpy section of the code.

User avatar
Alex
Lemma-Class Veteran
Posts: 3098
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Switch variable at random intervals.

#2 Post by Alex »

Inksword wrote: Mon Jul 24, 2023 2:16 am ...
In your code you:
- declared changable global variable 'watched',
- set 'currentwatch' variable to False at every game start,
- created the 'swapWatch' function that takes 'currentwatch' argument.

In 'swapWatch' function:
- if given argument is True, you set the local (related only to this function) variable 'watched' to False,
- overwise, set this local variable to True.

So, try either:
- use global variable 'watched' in 'swapWatch' function, like

Code: Select all

    def swapWatch(currentwatch):
        global watched
        watched = not currentwatch # to toggle True/False
- or toggle the value of 'watched' directly in label, like

Code: Select all

label start:
    scene bg room
    show eileen happy  

    # These display lines of dialogue.

    e "[watched]"
    e "[watched]"
    
    #$ swapWatch(watched)
    $ watched = not watched

Inksword
Regular
Posts: 83
Joined: Fri Oct 24, 2014 1:20 am
Tumblr: inksword
Contact:

Re: Switch variable at random intervals.

#3 Post by Inksword »

Hey thanks! I wasn't sure if calling the variable something different in the function definition would confuse it, I'm used to javascript where you would've given it a unique placeholder variable. So thank you! I'll try these pieces of code out!

Post Reply

Who is online

Users browsing this forum: No registered users