Objects pretending to be sprites

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
herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

Objects pretending to be sprites

#1 Post by herenvardo » Sun Nov 27, 2011 7:44 pm

I hope I can describe properly what I'm trying. If not, don't hesitate to ask for details and clarifications.

Some time ago, I managed to make my Creature objects "pretend" to be characters when used in say statements (this post mostly summarizes it: http://lemmasoft.renai.us/forums/viewto ... 097#p57552). This way I use the same objects (and hence the same variables) to track in-game info and for say statements.

Now I want to go one step further and reuse those creature objects for my Show statements. Each Creature object has enough information to retrieve the actual displayable to show, depending on different fields, so I came up with something like this:

Code: Select all

init python:
    class Creature(renpy.Displayable):
        # I'm omitting a lot of stuff here
        def render(self, width, height, st, at):
            return sprites[self.spriteKey][self.mood].render(width, height, st, at)
        def event(self, ev, x, y, st):
            return sprites[self.spriteKey][self.mood].event(ev, x, y, st)
        def visit(self):
            return [ sprites[self.spriteKey][self.mood] ]
"sprites" is a global variable, a dictionary of dictionaries (indexed by strings at both levels) which contain the actual displayables. It is filled during the init phase, and never modified afterwards.
Creature objects are meant to be saved; they are all created after the init phase; and their fields change quite often.
The actual "selection" logics are a bit deeper than taking the creature's mood, but the idea is essentially the same.

The goal is to be able to have code like this:

Code: Select all

show priest at right
priest "Welcome to the Temple."
My question: is this the "right" way to do this, or will it backfire bringing troubles later on?
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

User avatar
PyTom
Ren'Py Creator
Posts: 15454
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Objects pretending to be sprites

#2 Post by PyTom » Sun Nov 27, 2011 11:49 pm

Hm... This could work, although you should call renpy.render rather than the render method directly, since the render api may change at some time in the future.

When do you create the Creature object? If it's in the init phase, realize that if you save and load you might get a different creature back.

I'm not sure what the advantage of doing it this way is, rather than just having a show method on Creature that calls renpy.show(). Similar encapsulation, but without the tight binding.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

Re: Objects pretending to be sprites

#3 Post by herenvardo » Mon Nov 28, 2011 5:56 am

PyTom wrote:Hm... This could work, although you should call renpy.render rather than the render method directly, since the render api may change at some time in the future.
The overall idea was to delegate any "displayable-related" calls to the displayable itself. If the API for Displayable.render changes, then the API for Creature.render should also change: as long as it inherits from renpy.Displayable, it should be interface-compatible with it.
PyTom wrote:When do you create the Creature object? If it's in the init phase, realize that if you save and load you might get a different creature back.
I think I answered that even before you asked:
herenvardo wrote:Creature objects are meant to be saved; they are all created after the init phase; and their fields change quite often.
There are a few exceptions to this (some "template" objects only used to create new ones by copying), but I'll handle those "at my own risk" :P
PyTom wrote:I'm not sure what the advantage of doing it this way is, rather than just having a show method on Creature that calls renpy.show(). Similar encapsulation, but without the tight binding.
Neither am I. The only purpose is to make the Creature objects usable on show statements, and to ensure my sprite-selection logics are invoked to pick the right image. The code snippet I posted was my first (reasonable) idea for achieving this. The main reason I was asking here was to find out if there is a better way :)

So, having a show method would be enough for my objects to work on show statements? What signature should that method have? On a hunch, I'd mimic the signature on http://www.renpy.org/wiki/renpy/doc/ref ... renpy.show replacing the name argument with self, but it's always safer and more reliable to ask.
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

User avatar
PyTom
Ren'Py Creator
Posts: 15454
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Objects pretending to be sprites

#4 Post by PyTom » Mon Nov 28, 2011 11:19 am

herenvardo wrote:So, having a show method would be enough for my objects to work on show statements? What signature should that method have? On a hunch, I'd mimic the signature on http://www.renpy.org/wiki/renpy/doc/ref ... renpy.show replacing the name argument with self, but it's always safer and more reliable to ask.
[/quote]

No, it wouldn't be - since objects and images live in a different namespace. The only way to show an object created after the end of the init phase is to use "show expression object".

But why not just have:

$ myObject.show()

which calls renpy.show with the right arguments. Don't show myObject, just show the appropriate image.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

Re: Objects pretending to be sprites

#5 Post by herenvardo » Mon Nov 28, 2011 12:27 pm

I was trying to keep my script as clean as possible, but I guess this is as far as it gets.

Well, this is still better than having a 24-fold or worse ConditionSwitch for each sprite (which I would need if I had to rely on plain show statements), so I guess I'll have to live with those few $s scattered around the script.

On a positive note, since I started looking at this, not even 24h ago, I have gained quite a bit of insight on how "show" and "image" work :D

Thanks once again for your valuable answers.
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

Post Reply

Who is online

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