[SOLVED] Any alternative for lambda?

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
Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

[SOLVED] Any alternative for lambda?

#1 Post by Victorius »

So I can't use lambda in Ren'Py, as it causes errors on save, is there any alternative? I've heard about renpy.curry(), but I didn't find it in docs to check how to use it, so are there any alternatives to 'sorting' dicts in Ren'Py?

Code: Select all

# Labels opening crafting screen
label stor_forge:
    python:
        sorted_dict = sorted(recipes_forge.items(), key=lambda x: x[0].name)

    call screen storage_craft(recipes = sorted_dict, table_lvl = exp_forge_lvl, name = "Forge", button_style = "btn_craft_stone")
or

Code: Select all

screen storage_materials(gs_va, gs_pl):
    zorder 95
    tag storage_menu

    $sorted_mat = sorted(inventory.materials.items(), key=lambda x: x[0])
    $sorted_mat_vault = sorted(vault.materials.items(), key=lambda x: x[0])
Last edited by Victorius on Wed Apr 14, 2021 5:01 pm, edited 1 time in total.

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

Re: Any alternative for lambda?

#2 Post by Ocelot »

Lambdas are just syntax sugar for normal functions. I always suggest to never use lambdas until you learn how they work.

In your first ccasee you can do something like:

Code: Select all

init python:
def get_material_name(item):
    return item[0].name

#...
    sorted_dict = sorted(recipes_forge.items(), key=get_material_name)
< < insert Rick Cook quote here > >

Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

Re: Any alternative for lambda?

#3 Post by Victorius »

Ocelot wrote: Wed Apr 14, 2021 1:28 pm Lambdas are just syntax sugar for normal functions. I always suggest to never use lambdas until you learn how they work.

In your first ccasee you can do something like:

Code: Select all

init python:
def get_material_name(item):
    return item[0].name

#...
    sorted_dict = sorted(recipes_forge.items(), key=get_material_name)
Thanks for answer! But it still gives an error. I tried using partial, but it changed nothing.

Code: Select all

sorted_dict = sorted(recipes_forge.items(), key=partial(get_card_name))

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00keymap.rpy", line 463, in script
    python hide:
  File "renpy/common/00keymap.rpy", line 463, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 482, in _execute_python_hide
    renpy.save("_reload-1", "reload save game")
PicklingError: Can't pickle <type 'code'>: attribute lookup __builtin__.code failed

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00keymap.rpy", line 463, in script
    python hide:
  File "C:\Users\Gamer\Desktop\Astral Lust\renpy-7.3.5-sdk/renpy/ast.py", line 922, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\Gamer\Desktop\Astral Lust\renpy-7.3.5-sdk/renpy/python.py", line 2218, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "renpy/common/00keymap.rpy", line 463, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 482, in _execute_python_hide
    renpy.save("_reload-1", "reload save game")
  File "C:\Users\Gamer\Desktop\Astral Lust\renpy-7.3.5-sdk/renpy/loadsave.py", line 413, in save
    reraise(t, e, tb)
  File "C:\Users\Gamer\Desktop\Astral Lust\renpy-7.3.5-sdk/renpy/loadsave.py", line 399, in save
    dump((roots, renpy.game.log), logf)
  File "C:\Users\Gamer\Desktop\Astral Lust\renpy-7.3.5-sdk/renpy/loadsave.py", line 49, in dump
    cPickle.dump(o, f, cPickle.HIGHEST_PROTOCOL)
PicklingError: Can't pickle <type 'code'>: attribute lookup __builtin__.code failed

Windows-10-10.0.19041
Ren'Py 7.4.4.1439
Astral Lust 0.1.4b
Wed Apr 14 19:42:06 2021

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

Re: Any alternative for lambda?

#4 Post by Ocelot »

Well, it appears that somewhere a function gets assigned to a variable which is then is saved. This is the main reason an error like that should appear. Sorted does not store key function, so it should not be the case.
< < insert Rick Cook quote here > >

Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

Re: Any alternative for lambda?

#5 Post by Victorius »

Ocelot wrote: Wed Apr 14, 2021 2:41 pm Well, it appears that somewhere a function gets assigned to a variable which is then is saved. This is the main reason an error like that should appear. Sorted does not store key function, so it should not be the case.
I've solved the problem, code objects can't be pickled and I did it when deciding which buttons can appear, so I've removed compile() from code, and directly evaluated a string.

Post Reply

Who is online

Users browsing this forum: Andredron