Ren'Py Assign an action to a variable, then perform that action within the same button click.

Milvidi

Member
Game Developer
Feb 26, 2018
200
540
I have something like this:
Code:
default generated_log_list = []
default action_taken = None

label dungeon_creation:
    python:
        test_dungeon_1st_floor_log = [
            ["You searched around and found nothing...", None], #[log text, action]
            ["You've found an energy drink. {color=#3366b8}Energy +10{/color}.", Function(protag.EnergyModify, +10)]
        ]

        test_dungeon_1st_floor = Dungeon(
            name = "Test Dungeon 1st Floor",
            type = "Organic",
            bg = "test_dungeon_bg.webp",
            progress = 0,
            enemy_req = 5,
            log = test_dungeon_1st_floor_log
        )
    return
And a function like this:

Code:
init -1 python:
    def LogGen(log=[]):
        global generated_log_list
        global action_taken
        entry = renpy.random.choice(log)
        action_taken = entry[1]
        if len(generated_log_list) > 10:
            generated_log_list.remove(generated_log_list[0])
        generated_log_list.append(entry[0])
And a screen:

Code:
screen dungeon_scr(dungeon, next_floor=False):
    zorder 0
    modal True
    add dungeon.bg
    add "ui/dungeon ui/Dungeon Info.webp"
    add "ui/dungeon ui/Enemy List.webp"
    use mc_dungeon_scr
    frame:
        xsize 950
        background None
        xalign 0.85
        yalign 0.065
        vbox:
            spacing 10
            text dungeon.name xpos 320
            bar:
                value dungeon.progress
                range 100
                left_bar Frame("ui/dungeon ui/Progress-Bar-2.webp", 10, 0)
                right_bar Frame("ui/dungeon ui/Progress-Bar-1.webp", 10, 0)
            text "Enemy required for next floor: " + str(dungeon.enemy_req)

    viewport:
        xpos 800
        ypos 590
        ysize 300
        yinitial 1.0
        mousewheel True
        draggable True
        vbox:
            spacing 20
            for log in generated_log_list:
                text log

    imagebutton:
        idle "ui/dungeon ui/Search-1.webp"
        hover "ui/dungeon ui/Search-2.webp"
        insensitive "ui/dungeon ui/Search-3.webp"
        focus_mask True
        tooltip "Search around"
        action [Function(protag.EnergyModify, -1),
        SetField(dungeon, "progress", dungeon.progress + 10),
        Function(LogGen, dungeon.log),
        action_taken] <----- The problem is here
        sensitive protag.energy > 0
What I want it to do is: When I press the imagebutton, it perform the LogGen() function, which will assign a value randomly chosen from dungeon.log to 'action_taken', then perform said action. This has to happen within the same click.

The problem is that when I click the button the first time, even if 'Function(protag.EnergyModify, +10)' is chosen, it still perform its default value, which is 'None'. Then on the second click, it will run 'Function(protag.EnergyModify, +10)', regardless of what is chosen that time.

The action is always 1 click late, but the logs shown in the viewport is correct. It shows 'Energy +10', but the actual value of energy won't change until I click that button another time.

Is there anyway to fix this?
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,964
16,210
The problem is that when I click the button the first time, even if 'Function(protag.EnergyModify, +10)' is chosen, it still perform its default value, which is 'None'. Then on the second click, it will run 'Function(protag.EnergyModify, +10)', regardless of what is chosen that time.
This should fix the issue.

Python:
init -1 python:
    def LogGen(log=[]):
        global generated_log_list
        global action_taken
        entry = renpy.random.choice(log)
#        action_taken = entry[1]
        renpy.run( entry[1] )
        if len(generated_log_list) > 10:
            generated_log_list.remove(generated_log_list[0])
        generated_log_list.append(entry[0])
 
  • Like
Reactions: Milvidi

Milvidi

Member
Game Developer
Feb 26, 2018
200
540
This should fix the issue.

Python:
init -1 python:
    def LogGen(log=[]):
        global generated_log_list
        global action_taken
        entry = renpy.random.choice(log)
#        action_taken = entry[1]
        renpy.run( entry[1] )
        if len(generated_log_list) > 10:
            generated_log_list.remove(generated_log_list[0])
        generated_log_list.append(entry[0])
It works. Thank you :D.