Combining variable into 1?

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
wildride
Newbie
Posts: 16
Joined: Wed Jan 04, 2017 8:25 am
Contact:

Combining variable into 1?

#1 Post by wildride »

Ok what Im attempting to do and likely in the worst way possible, is simply an item purchased from a shop to the players inventory.

The Inventory code is like this:

Code: Select all

init:
    $ itemcount = 1
    $ item2 = ""
    $ item3 = ""
    $ item4 = ""
    $ item5 = ""
    $ item6 = ""
    $ item7 = ""
    $ item8 = ""
    $ item2txt = ""
    $ item2txt = ""
    $ item2txt = ""
    $ item2txt = ""
    $ item2txt = ""
    $ item2txt = ""
    $ item2txt = ""

screen inventory:
    frame:
        xysize (656,524)
        xalign 0.5
        yalign 0.5
        background "#00000090"
        has vbox:
            xalign 0.1
            yalign 0.1
            imagebutton:
                idle "/inv/keys.png" clicked NullAction() hovered Show("displayTextScreen", displayText = "Your Keys.") unhovered Hide("displayTextScreen")
    if itemcount >= 2:
        frame:
            xysize (656,524)
            xalign 0.5
            yalign 0.5
            background
            has vbox:
                xalign 0.5
                yalign 0.1
                imagebutton:
                    idle "/inv/[item2].png" clicked Jump("[item2]") hovered Show("displayTextScreen", displayText = "[item2txt]") unhovered Hide("displayTextScreen")
   
It continues on for the 8 items. The buying code is looking like:

Code: Select all

label buyhw:
    if cash >= 10:
        menu:
            "Pay $10 for the Hand Weights.":
                $ cash -= 10
                $ ownhw = True
                $ itemcount += 1
                $ item + itemcount = "handweight"
                call screen buystuff
            "Cancel.":
                call screen buystuff
    else:
        "You cannot afford anything here."
        call screen stats
Where its having trouble is the line
$ item + itemcount = "handweight"
I think you can see what Im trying to accomplish but if not I'm effectively trying to use the itemcount numeric variable to combine to item2 then item3 then item4 etc setting those variables as the inventory fills out.
Its giving me a "Cant assign operator" error.
Ive tried == which changes the error to "name 'item' is not defined. Tried a few iterations of how to type that and not having any luck combining.

I could get around it with a bunch of if/elif statements but thought that seemed code heavy, another way to eliminate this itemcount altogether and have fixed positions for each possible item on the inventory screen, but I would rather find out how I could get this done (if possible) for possible uses in other situations.

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: Combining variable into 1?

#2 Post by Remix »

globals( "item{0}".format( itemcount ) ) = "handweight"

Should get you round that hurdle.
I would say though, have a google for python lists and dictionaries... they would be a much nicer way to hold your data
Frameworks & Scriptlets:

User avatar
Scribbles
Miko-Class Veteran
Posts: 636
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island, As We Know It
Projects: In Blood
Organization: Jaime Scribbles Games
Deviantart: breakfastdoodles
itch: scribbles
Location: Ohio
Contact:

Re: Combining variable into 1?

#3 Post by Scribbles »

I would second lists and dictionaries, they are actually pretty easy to pick up on, and they have little tutorials on codeacademy.com

basically lists hold items, they can be variables, strings, and numbers. Dictionaries hold keys which have values. so... [item_1, item_2] would be a list, and {item_1: weight, item_2: weight} would be a dictionary. then there are lots of ways to modify, access, and add/remove information from those. (I may not have the right syntax for dictionary though) -- def check out codeacademy (it's free)
Image - Image -Image

wildride
Newbie
Posts: 16
Joined: Wed Jan 04, 2017 8:25 am
Contact:

Re: Combining variable into 1?

#4 Post by wildride »

Thanks, while the globals didnt sort it out (on its own without additional work which I wasnt sure how to proceed from) I did look up lists and dictionaries and sure enough the lists work perfectly.

Much better than a string of if/elif statements I had planned for other things (ie days of the week)

Ok spoke to soon:

Code: Select all

idle "/inv/[item[1]].png" clicked Jump("[item[1]]") hovered Show("displayTextScreen", displayText = "[itemtxt[1]]") unhovered Hide("displayTextScreen")
this will find the pic correctly, it will find the itemtxt correctly, but wont jump to the label.

Testing wise Ive done the following:
Bought all the items, saved the game, gone into inventory clicked an item and got an error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/inventory.rpy", line 107, in script
    call screen inventory
ScriptError: could not find label '[item[1]]'.

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

Full traceback:
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\bootstrap.py", line 295, in bootstrap
    renpy.main.main()
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\main.py", line 487, in main
    run(restart)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\main.py", line 147, in run
    renpy.execution.run_context(True)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\execution.py", line 761, in run_context
    context.run()
  File "game/inventory.rpy", line 107, in script
    call screen inventory
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\script.py", line 858, in lookup
    raise ScriptError("could not find label '%s'." % str(original))
ScriptError: could not find label '[item[1]]'.

Windows-8-6.2.9200
Ren'Py 6.99.12.4.2187
Metastasis 0.1
Load that game and get a "list index out of range" error when trying to look at inventory.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/inventory.rpy", line 107, in script
    call screen inventory
  File "renpy/common/000statements.rpy", line 471, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
Exception: In DynamicImage u'/inv/[item[1]].png': IndexError('list index out of range',)

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

Full traceback:
  File "game/inventory.rpy", line 107, in script
    call screen inventory
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\ast.py", line 1706, in execute
    self.call("execute")
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\ast.py", line 1724, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\statements.py", line 145, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 471, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\exports.py", line 2526, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\ui.py", line 285, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 2526, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 2793, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\screen.py", line 403, in visit_all
    self.child.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 495, in visit_all
    d.visit_all(callback)
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\core.py", line 492, in visit_all
    for d in self.visit():
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\image.py", line 587, in visit
    self.find_target()
  File "C:\Users\Adam\Downloads\renpy-6.99.12.2-sdk\renpy\display\image.py", line 499, in find_target
    raise Exception("In DynamicImage %r: %r" % (self.name, e))
Exception: In DynamicImage u'/inv/[item[1]].png': IndexError('list index out of range',)

Windows-8-6.2.9200
Ren'Py 6.99.12.4.2187
Metastasis 0.1
What Im not getting is how it works to show the pic and text, then after trying to get it to jump to a label and failing then it wont work at all. I could get that I messed up the syntax or something if it didnt work at all.

wildride
Newbie
Posts: 16
Joined: Wed Jan 04, 2017 8:25 am
Contact:

Re: Combining variable into 1?

#5 Post by wildride »

Well sorted it out by changing the Jump("[item[1]]") etc to simply Jump("item1") etc.

So looks like

Code: Select all

label item0:
    $ invact = "[item[0]]"
    jump invaction
    
label invaction:
    "getting to the ifs, item zero is [item[0]]"
    if invact == "keys":
        "You jangle your keys."
    elif invact == "handweight":
The invaction label is basically all the seperate labels I had for keys/handweight and other items into if statements under the 1 label. It actually looks cleaner than before

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: Combining variable into 1?

#6 Post by Remix »

The "[var]" format in Ren'py string interpolation can (and often does) get a bit of a pain with list indices or dictionary keys, due to the variable part also containing square brackets... Depending where the string is, some workarounds work and some don't (e.g. escaping the [ by doubling it [[)...
As you use function calls in most parts, you can skip the "[ and ]" though... try

idle "/inv/[item[1]].png" clicked Jump(item[1]) hovered Show("displayTextScreen", displayText = itemtxt[1]) unhovered Hide("displayTextScreen")

If the image still glitches, maybe

$ pic = item[1]
... "/inv/[pic].png" ...

or
"/inv/%s.png" % item[1]
or
"/inv/{0}.png".format( item[1] )
Frameworks & Scriptlets:

wildride
Newbie
Posts: 16
Joined: Wed Jan 04, 2017 8:25 am
Contact:

Re: Combining variable into 1?

#7 Post by wildride »

Ill give it a try because while I thought I had it down, its not quite working out for clicking the images with what I had.

After testing a few options yeah it was brackets, putting a little bug test so others may have an idea to try similar if they get stuck on syntax.

Essentially did this to work out what was being produced.

Code: Select all

label item0:
    $ invact = "[item[0]]"
    $ invact2 = [item[0]]
    $ invact3 = "item[0]"
    $ invact4 = item[0]
    jump invaction

label invaction:
    "this is getting to invaction."
    "but is invact being set? [invact]"
    "invact2 here [invact2]"
    "invact3 here [invact3]"
    "invact4 here [invact4]"
Then played the game to a point of being able to test this, the item in question are keys
1 ended up [item[0]]
2 ended up [u'keys]
3 ended up item[0]
4 ended up keys
So without the " and [] around it is the way to go which is what you are suggesting there so Im going to back up what I have working to see if this will work as originally planned which will condense the code a little bit.

As for the pics messing up, that seems to be if I change the code while in game too, so saving likely had things at one code, loaded game and it broke it. Will post back with results.

Edit: It would appear that changing just the jump language worked, if I changed the itemtxt one it would crash.
Doing this also appears to have resolved the crash on reloading the script after a change. So thats another bonus as I couldve seen that coming back to bite me.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], fufuffiero, Google [Bot], Majestic-12 [Bot]