Object / variable references in python [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
kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Object / variable references in python [SOLVED]

#1 Post by kivik » Mon Aug 08, 2016 2:12 pm

I'm having a bit of trouble understanding python / Ren'py and it's causing problems when loading, saving or restarting the game:

So I've got a load of rooms as objects, and in said rooms are items (also objects) that I can store. Pretty straight forward so far..

Now I want to move the player around rooms, so I figured the easiest thing to do is to assign a current room variable, for example:

Code: Select all

$ current_room = bedroom
Now if I were to add or remove items to the room, I'd use:

Code: Select all

$ current_room.addItem(item)
$ current_room.removeItem(item)
The problem is, the above code seem to create a copy of the room object and put it in the current_room variable. Changes made to current_room aren't then reflected on bedroom.

In trying to resolve this, I've tried moving the initial object creation code in and out of init blocks, as well as init_labels to create the objects during game start - none of which really behaves the way I expected it to when restarting / reloading the game :(


What I really want, is to have a variable that's able to just point at the relevant rooms, and allow me to manipulate said rooms through it. I've tried looking up variable call to references on google and they all seem to point to variable types int and string etc, not objects.

Could someone please explain what I'm doing wrong please?

Thank you in advance!

kivik


edit: found out that I'd created one of the variables in an init block, which messed up the aliasing!
Last edited by kivik on Mon Aug 08, 2016 5:13 pm, edited 1 time in total.

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Object / variable references in python

#2 Post by gas » Mon Aug 08, 2016 4:58 pm

You're using a variable like it was a class.
You need to use two variables, one for the room, one for the items (in that later case, use a LIST).

Or create a class.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Object / variable references in python

#3 Post by kivik » Mon Aug 08, 2016 5:12 pm

I finally solved this by re-reading the saving and loading aspect of Ren'py.

My items and rooms are all classes, but what happened was I was creating some of the objects in init blocks (I missed one of them) and some in labels - so when loading / restarting the game, the variables were messed up and the aliasing were messed up!

Wish I hadn't skipped that chapter when reading the docs!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], span4ev