Passing a class object as an argument

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
jwideman
Newbie
Posts: 14
Joined: Tue Jul 30, 2019 7:17 pm
Contact:

Passing a class object as an argument

#1 Post by jwideman »

This is tricky for me to explain, so here's the relevant code:

Code: Select all

default itemlist = Items()
default eventlist = Events()

init -1 python:
    class Items(renpy.store.object):
        def __init__(self):
            self.itemlist = set()

        def get(self):
            return self.itemlist

        def add(self,x):
            for i in self.itemlist:
                if i.name == x.name:
                    return
            self.itemlist.add(x)

        def remove(self,x):
            self.itemlist.remove(x)
            x.disabled = True
            self.itemlist.add(x)

        def readd(self,x):
            self.itemlist.add(x)

    class Item(renpy.store.object):
        def __init__(self,location,image,exit=False,x=0,y=0,dest=False,mouse="item",tooltip=False,sfx=False):
            self.location = location    #the location of the item, must always be included
            self.image = image          #the base image used
            self.name = self.location + "." + self.image
            self.exit = exit            #for the exit buttons, since they are positioned different
            self.x = x                  #xcenter/xalign
            self.y = y                  #ycenter/yalign
            self.dest = dest            #the label to jump to
            self.mouse = mouse          #the mouse pointer, can be defined as "exit" or "item"
            self.tooltip = tooltip      #the tooltip text
            self.sfx = sfx              #the sound that plays when clicked
            self.disabled = False

    class Events(renpy.store.object):
        def __init__(self):
            self.eventlist = set()

        def get(self):
            return self.eventlist

        def add(self,x):
            for i in self.eventlist:
                if i.name == x.name:
                    return
            self.eventlist.add(x)

        def remove(self,x):
            self.eventlist.remove(x)
            x.disabled = True
            self.eventlist.add(x)

        def readd(self,x):
            self.eventlist.add(x)

    class Event(renpy.store.object):
        def __init__(self,story,scene,location=False,day=False,time=False,auto=False,item=False):
            self.story = story
            self.scene = scene
            self.name = self.story + "." + self.scene
            self.location = location
            self.day = day
            self.time = time
            self.auto = auto
            self.item = item
            self.disabled = False

label event_loop:
    python:
        for event in eventlist.get():
            if not event.disabled:
                event_conditions = 0    #the total number of conditions for the event
                event_met = 0    #the number of conditions met
                if event.location:
                    event_conditions += 1
                    if event.location == locstring:
                        event_met += 1
                if event.day:
                    event_conditions += 1
                    if event.day == day:
                        event_met += 1
                if event.time:
                    event_conditions += 1
                    if event.time == time_of_day:
                        event_met += 1
                if event_met and event_met == event_conditions:
                    if event.auto:
                        renpy.call(event.name)
                        eventlist.remove(event)
                    else:
                    	itemlist.add(event.item)
                        eventlist.remove(event)

    return

label setup_item:
	python:
		itemlist.add(Item("mc_living","kitchen",x=1223,y=376,dest="mc_kitchen",mouse="exit"))
   
label setup_event:
	python:
	        eventlist.add(Event("party","beerminigame",location="mc_living",item=Item("mc_living","beers",x=502,y=404,dest="mc_living.beers")))

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: Passing a class object as an argument

#2 Post by Remix »

Care to say which part is not behaving?
Frameworks & Scriptlets:

jwideman
Newbie
Posts: 14
Joined: Tue Jul 30, 2019 7:17 pm
Contact:

Re: Passing a class object as an argument

#3 Post by jwideman »

No idea. The Item() wasn't getting added to the stack like I expected, but now it is. I have second problem though:
Since I can't remove an object from the stack without a reload resetting it, I set a flag:

Code: Select all

def remove(self,x):
            self.eventlist.remove(x)
            x.disabled = True
            self.eventlist.add(x)
This works for the itemlist (see the advance_time label) but not for the eventlist and I can't figure out where it's going wrong.

jwideman
Newbie
Posts: 14
Joined: Tue Jul 30, 2019 7:17 pm
Contact:

Re: Passing a class object as an argument

#4 Post by jwideman »

Whoops. I left out the advance_time code. It's really just to demonstrate that the .remove method works of course.

Code: Select all

label advance_time:
    #scene black with fade

    call event_loop

    python:
        time_of_day += 1
        if time_of_day == 4:
            time_of_day = 0
            day += 1
            for item in itemlist.get():
                if item.name == "mc_bedroom.bed":
                    itemlist.remove(item)
                    break

        if time_of_day == 3:
            itemlist.readd(Item("mc_bedroom","bed",x=1028,y=550,dest="advance_time"))

    scene expression get_bg() with fade

    jump do_events


jwideman
Newbie
Posts: 14
Joined: Tue Jul 30, 2019 7:17 pm
Contact:

Re: Passing a class object as an argument

#5 Post by jwideman »

I spoke too soon. It only works about half the time. The code only works as expected about half the time per launching the game. That is, I launch the game, the imagebutton shows up where it's supposed to, I exit the game. I launch the game again, and this time the imagebutton does show up. However, at no time does the second problem go away - the events replay even though they shouldn't.

Post Reply

Who is online

Users browsing this forum: Google [Bot], mold.FF