[SOLVED] How can I pass an argument to a Label from an anchor tag?

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
Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

[SOLVED] How can I pass an argument to a Label from an anchor tag?

#1 Post by Thailandian »

I'm trying to make an interactive dictionary for the Australian idioms (Strine) in my game.

I've tried using an anchor tag to open the dictionary and that works fine:

Code: Select all

tr right "Bloody {a=call:open_strine}top{/a} gig mate."
However, I really want to pass a parameter to the Label statement to open the dictionary at the relevant entry; e.g.:

Code: Select all

tr right "Bloody {a=call:open_strine(1)}top{/a} gig mate."
That gives the following traceback:
While running game code:
File "game/script.rpy", line 72, in script call
tr right "Bloody {a=call:open_strine(1)}top{/a} gig mate."
File "game/script.rpy", line 72, in script call
tr right "Bloody {a=call:open_strine(1)}top{/a} gig mate."
ScriptError: could not find label 'open_strine(1)'.
I've also tried:

Code: Select all

tr right "Bloody {a=call: expression 'open_strine' pass (entry=1)}top{/a} gig mate."
... with the same resulting traceback. I notice that in the documentation, although use of parameters is discussed in regular jumps and calls, in the section on anchor tags, there is no mention of them.

Am I doing something wrong or is it just not possible?
Last edited by Thailandian on Fri Jun 04, 2021 3:20 pm, edited 2 times in total.

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

Re: How can I pass an argument to a Lable from an anchor tag?

#2 Post by Ocelot »

This is not possible using standard RenPy handlers. You will need to define your own protocol as documented there: https://www.renpy.org/doc/html/config.h ... k_handlers

An example implementation, which uses '|' to separate label and each argument:

Code: Select all

init python:
    def call_with_arguments(value):
        args = value.split('|')
        label = args.pop(0)
        renpy.call(label, *args)

    config.hyperlink_handlers.append('call_args', call_with_arguments)

# Usage:
tr right "Bloody {a=call_args:open_strine|1}top{/a} gig mate."
< < insert Rick Cook quote here > >

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#3 Post by Remix »

The simplest way is to just hijack the anchor call: handler and add some logic...

Code: Select all

init python:
    
    def my_call_handler(value):
        vals = value.split()
        renpy.call(vals[0], *vals[1:])

    config.hyperlink_handlers["call"] = my_call_handler

label anchor_jump(arg1=0, arg2=):
    "Jumped to with [arg1] and [arg2]"
    return

label start:
    ## Note the spaces in the anchor ... call:anchor_jump 12 15
    ## First the label name, then any arguments with a space separating each
    "A text with a {a=call:anchor_jump 12 15}Link{/a}."
    pause
Beaten by Ocelot... Similar approach though
Frameworks & Scriptlets:

Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#4 Post by Thailandian »

Thank you both - just what I needed.

I'm new to this forum. Do I need to mark this as "SOLVED"?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#5 Post by Remix »

Most people do, yup.
Frameworks & Scriptlets:

Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#6 Post by Thailandian »

Oops - seems I spoke a bit too soon. Ocelot's code resulted in the following traceback:
I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 3, in script
init python:
File "game/script.rpy", line 3, in script
init python:
File "game/script.rpy", line 11, in <module>
config.hyperlink_handlers.append('call_args', call_with_arguments)
AttributeError: 'RevertableDict' object has no attribute 'append'

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

Full traceback:
File "/home/ian/Storage2/Software/Executable/renpy-7.4.2-sdk/renpy/bootstrap.py", line 326, in bootstrap
renpy.main.main()
File "/home/ian/Storage2/Software/Executable/renpy-7.4.2-sdk/renpy/main.py", line 515, in main
renpy.game.context().run(node)
File "game/script.rpy", line 3, in script
init python:
File "game/script.rpy", line 3, in script
init python:
File "/home/ian/Storage2/Software/Executable/renpy-7.4.2-sdk/renpy/ast.py", line 922, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "/home/ian/Storage2/Software/Executable/renpy-7.4.2-sdk/renpy/python.py", line 2218, in py_exec_bytecode
exec(bytecode, globals, locals)
File "game/script.rpy", line 11, in <module>
config.hyperlink_handlers.append('call_args', call_with_arguments)
AttributeError: 'RevertableDict' object has no attribute 'append'
Fortunately, Remix's code worked but I hate not understanding things and I only half understand the traceback.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#7 Post by Remix »

config.hyperlink_handlers['call_args'] = call_with_arguments
or
config.hyperlink_handlers.update( {'call_args', call_with_arguments} )

*should* work there... .append is really a list method whereas config.hyperlink_handlers is a dictionary...
Once you learn those sequences (dict/list/tuple/set) etc you will probably mix the methods up too ;)
Frameworks & Scriptlets:

Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

Re: How can I pass an argument to a Lable from an anchor tag?

#8 Post by Thailandian »

Happy chance then - not only solved my immediate problem but got pointed in the direction of useful generic stuff.

In case anyone else ends up here and wants to find out about Python data types, here is a great link: https://www.whereisstuff.com/2021/02/li ... -data.html

Thanks again to you both.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]