Jump to variable determined label? (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
User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Jump to variable determined label? (solved)

#1 Post by noeinan »

I was wondering if I could jump to a label, where the name of the label draws on a variable? Something like this:

Code: Select all

jump (current_week + "_evt")
Where current_week is a variable. So, if current_week = "Art" then it will jump to label Art_evt
Last edited by noeinan on Tue Apr 07, 2020 8:22 pm, edited 1 time in total.
Image

Image
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Jump to variable determined label?

#2 Post by xela »

daikiraikimi wrote:I was wondering if I could jump to a label, where the name of the label draws on a variable? Something like this:

Code: Select all

jump (current_week + "_evt")
Where current_week is a variable. So, if current_week = "Art" then it will jump to label Art_evt
There is a number of ways, I usually use:

$ renpy.jump("".join([current_week, "_evt"]))

basically the same thing as:

$ renpy.jump(current_week + "_evt")

and maybe:

jump expression current_week + "_evt"

But I have never used the last one.
Like what we're doing? Support us at:
Image

User avatar
Ghuraok
Newbie
Posts: 11
Joined: Wed Jul 19, 2017 7:50 pm
Contact:

Re: Jump to variable determined label?

#3 Post by Ghuraok »

Hi! I'm just throwing it here, I don't know if it's still relevant but this script helped me a lot to work on my inventory system, the only issue I had is that I didn't know how to use it inside screens.
Recently I realised I could just type directly the "".join([thevariable]) inside the Jump arguments. So simply:

Code: Select all

Jump("".join([thevariable])
I don't know if it will help much people, but I know I had been searching for the solution for a while now, so hopefully people like me could find it in this thread. :roll:

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: Jump to variable determined label?

#4 Post by strayerror »

First, all of the options presented by xela will work for you. I'm just going to expand a little on the expression keyword, which is the way to do what you're asking without leaving renpy and jumping into python. The expression keyword causes the remainder of the line to be evaluated and the result used, rather than taking it literally. Here's an example:

Code: Select all

label start:
    $ foo = 'bar'
    jump foo
    return

label foo:
    "foo" "this is where we end up when foo is used literally ..."
    jump expression foo
    return

label bar:
    "bar" "... but using expression with foo, means it's evaluated to its
    value (bar) and now we're here!"
    return
Hope that helps!

User avatar
Ghuraok
Newbie
Posts: 11
Joined: Wed Jul 19, 2017 7:50 pm
Contact:

Re: Jump to variable determined label?

#5 Post by Ghuraok »

strayerror wrote: Sun Feb 10, 2019 1:53 pm First, all of the options presented by xela will work for you. I'm just going to expand a little on the expression keyword, which is the way to do what you're asking without leaving renpy and jumping into python. The expression keyword causes the remainder of the line to be evaluated and the result used, rather than taking it literally. Here's an example:

Code: Select all

label start:
    $ foo = 'bar'
    jump foo
    return

label foo:
    "foo" "this is where we end up when foo is used literally ..."
    jump expression foo
    return

label bar:
    "bar" "... but using expression with foo, means it's evaluated to its
    value (bar) and now we're here!"
    return
Hope that helps!
Oh ! That's actually brilliant, it's more elegant than the "".join() in my opinion ! I don't seem to find a way to make it work in screens though.

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

Re: Jump to variable determined label?

#6 Post by philat »

...or you could just do Jump(thevariable). The only reason the above post references .join() is because they're combining the variable with a static text suffix ("_evt").

User avatar
Ghuraok
Newbie
Posts: 11
Joined: Wed Jul 19, 2017 7:50 pm
Contact:

Re: Jump to variable determined label?

#7 Post by Ghuraok »

philat wrote: Wed Mar 13, 2019 1:15 am ...or you could just do Jump(thevariable). The only reason the above post references .join() is because they're combining the variable with a static text suffix ("_evt").
Image
I swear, why have I never considered writing (item.effect) instead of ("[item.effect]")
Well at the very least, it's great that I don't have to write that everywhere now.
I'm an idiot for not thinking of getting rid of the brackets and quotemarks altogether. :oops: That's what happens when you learn from scattered examples and not theory.
Thank you for your wisdom philat (which I assume was basic knowledge to you) and sorry for the nonsense.

HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Re: Jump to variable determined label?

#8 Post by HammeredEnt »

Hi team,

I'm trying to do a very similar thing and it's just not coming together for me.

My code is as follows:

Code: Select all

label mjbusy:
$ random = renpy.random.randint(0, 13)
$ renpy.jump("".join(["mjbusy", random]))
and I've got a bunch of labels below under mjbusy0, mjbusy01, mjbusy02... mjbusy13. So I'm trying to simplify if from having 14 different 'if' statements but this code just comes back with the following error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/characters/mj.rpy", line 56, in script
    $ renpy.jump("mjbusy" + random)
  File "game/characters/mj.rpy", line 56, in <module>
    $ renpy.jump("mjbusy" + random)
TypeError: sequence item 1: expected string or Unicode, int found

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

Full traceback:
  File "game/characters/mj.rpy", line 56, in script
    $ renpy.jump("mjbusy" + random)
  File "C:\Users\Scott\Downloads\Cats\Omnaverse\renpy-7.0.0-sdk\renpy\ast.py", line 912, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\Scott\Downloads\Cats\Omnaverse\renpy-7.0.0-sdk\renpy\python.py", line 2004, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/characters/mj.rpy", line 56, in <module>
    $ renpy.jump("mjbusy" + random)
TypeError: sequence item 1: expected string or Unicode, int found

Windows-8-6.2.9200
Ren'Py 7.3.2.320
Wed Jul 31 21:57:04 2019
It works fine if I have the excessive amount of 'if' statements in there, but I just don't seem to be getting the variable suffix in the jump link to work properly.

Any idea of what I'm doing wrong here?

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Jump to variable determined label?

#9 Post by Alex »

Try to convert the value of 'random' to a string, like

Code: Select all

$ random = str(renpy.random.randint(0, 13))
to properly join it with prefix.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Jump to variable determined label?

#10 Post by xavimat »

You can add zfill to Alex's answer to add zeros to the left (in your post your labels have zeros):

Code: Select all

$ rand = str(renpy.random.randint(0, 13)).zfill(2)
Also, IMHO using"join" it's too much to join two simple strings. I'd use +

Code: Select all

jump expression "bla" + rand
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Jump to variable determined label?

#11 Post by drKlauz »

Might as well use format:

Code: Select all

$rnd_val=renpy.random.randint(0,13)
...
jump expression 'event_{:0>2}'.format(rnd_val)
https://docs.python.org/2/library/strin ... ing-syntax
https://pyformat.info
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

Post Reply

Who is online

Users browsing this forum: No registered users