Page 1 of 1

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

Posted: Tue Oct 09, 2018 10:37 pm
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 ^^

Re: Global Variable and Renpy function in Init Python?

Posted: Tue Oct 09, 2018 11:58 pm
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]"

Re: Global Variable and Renpy function in Init Python?

Posted: Wed Oct 10, 2018 12:54 am
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.

Re: Global Variable and Renpy function in Init Python?

Posted: Fri Oct 12, 2018 3:29 pm
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?