Global Variable and Renpy function in Init Python? (Solved)

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
pumpkin spike
Regular
Posts: 76
Joined: Fri Mar 09, 2018 6:13 pm
Projects: A Bureau of Monsters
itch: pumpkin-spike
Location: A Pumpkin Patch
Contact:

Global Variable and Renpy function in Init Python? (Solved)

#1 Post by pumpkin spike »

Struggling to understand what's happening with my code, hopefully someone can shed some light? ^^;
This is what I started with, and closest to my intentions. I'm trying to generate a bunch of variables based on two input values to the function. At first, without the global value code, the code wasn't working at all. After putting in the code, it returns monlvl = 8 and monhp = 80, seemingly to completely ignore the if statements?? Is it because I'm trying to use a renpy function within init python?

Code: Select all

init python:
    monlvl = 8
    def mgen1(x, lvl):
        global monlvl
        if lvl > 3:
            monlvl = renpy.random.randint((lvl-3),lvl)
        elif lvl == 3:
            monlvl = renpy.random.randint(1, 3)
        elif lvl == 2:
            monlvl = renpy.random.randint(1, 2)
        elif lvl == 1:
            monlvl = 1

        if x == 1:
            monhp = monlvl*10
        else:
            pass
        return monlvl, monhp
within label start:

Code: Select all

    "Text"
    $ mclvl = 5
    init python:
        monlvl, monhp = mgen1(1, mclvl)
    "mclvl is [mclvl]"
    "monhp is [monhp]"
    "monlvl is [monlvl]"

    init python:
        monlvl, monhp = mgen1(1, mclvl)
    "monhp is [monhp]"
    "monlvl is [monlvl]"
This is where I'm currently up to in terms of trying to troubleshoot? I noticed that the renpy random function was working sometimes. At one point it spit out a negative number. Then at another point, the return was monhp = 40 and monlvl = 5, which seems impossible?? Also, although I run the function twice to check how random the generator is, I always get the exact same values both times.

Code: Select all

init python:
    monlvl = 8
    def mgen1(x, lvl):
        global monlvl
        monlvl = renpy.random.randint(2,6)

        if x == 1:
            monhp = monlvl*10
        else:
            pass
        return monlvl, monhp
within label start:

Code: Select all

    "Text"
    $ mclvl = 5
    init python:
        monlvl, monhp = mgen1(1, mclvl)
    "mclvl is [mclvl]"
    "monhp is [monhp]"
    "monlvl is [monlvl]"

    init python:
        monlvl1, monhp1 = mgen1(1, mclvl)
    "mclvl is [mclvl]"
    "monhp1 is [monhp]"
    "monlvl1 is [monlvl]"
Thank you in advance ^^
Last edited by pumpkin spike on Fri Oct 12, 2018 3:29 pm, edited 1 time in total.
Image
WIP AllxAll game with turn based fighting (Demo 2.0 out!)

Image
WIP Yaoi game Demo 1.0 out! (haitus)

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Global Variable and Renpy function in Init Python?

#2 Post by philat »

1. Don't use init python in labels.
2. If you're going to use global anyway, there's no point in returning the values.

Code: Select all

init python:
    monlvl = 0
    monhp = 0
    def mgen1(x, lvl):
        global monlvl, monhp
        if lvl > 3:
            monlvl = renpy.random.randint((lvl-3),lvl)
        else:
            monlvl = renpy.random.randint(1, lvl)

        if x == 1:
            monhp = monlvl*10
        else:
            monhp = monlvl*20 # you had pass here, but that means monhp isn't set if x is anything other than 1

default monlvl = 1
default monhp = 10

label start:
    "Text"
    $ mclvl = 5
    $ mgen1(monlvl, 1, mclvl)
    "mclvl is [mclvl]"
    "monhp is [monhp]"
    "monlvl is [monlvl]"

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Global Variable and Renpy function in Init Python?

#3 Post by Per K Grok »

pumpkin spike wrote: Tue Oct 09, 2018 10:37 pm Struggling to understand what's happening with my code, hopefully someone can shed some light? ^^;
This is what I started with, and closest to my intentions. I'm trying to generate a bunch of variables based on two input values to the function. At first, without the global value code, the code wasn't working at all. After putting in the code, it returns monlvl = 8 and monhp = 80, seemingly to completely ignore the if statements?? Is it because I'm trying to use a renpy function within init python?

---
init python runs at initialization time, before the game loads. Use plain "python:" or "$" in the code under the start level and you will be fine.

I don't think you need a global value either.

edit:
Ooops, I see you already had been answered.

pumpkin spike
Regular
Posts: 76
Joined: Fri Mar 09, 2018 6:13 pm
Projects: A Bureau of Monsters
itch: pumpkin-spike
Location: A Pumpkin Patch
Contact:

Re: Global Variable and Renpy function in Init Python?

#4 Post by pumpkin spike »

Thank you both! This worked! (:

Out of curiosity, is there any difference in using a global variable for a function like this vs. returning the variable at the end of the function?
As in, is there a difference in chances of a bug/some wrong value being inserted between lines of code?
Image
WIP AllxAll game with turn based fighting (Demo 2.0 out!)

Image
WIP Yaoi game Demo 1.0 out! (haitus)

Post Reply

Who is online

Users browsing this forum: Dark79