retrieving x,y of a ui.image

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
jack_norton
Lemma-Class Veteran
Posts: 4067
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

retrieving x,y of a ui.image

#1 Post by jack_norton » Fri Apr 30, 2010 4:39 am

Is it possible? In my Planet Stronghold battle I display the enemies at center of screen with an ui.hbox to align them horizontally, the problem is that the enemies can have very different sizes (width/height).
I wanted now do add some more effects like displaying the damage done with a floating number over the enemy head and so on. Problem is: how I can put the number correctly if I don't know big is the ui.image (width/height) ? there's any way to obtain that info rather than make all enemies of same width? :oops:
follow me on Image Image Image
computer games

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: retrieving x,y of a ui.image

#2 Post by Jake » Fri Apr 30, 2010 5:02 am

jack_norton wrote:Is it possible?
I had the same problem with my recent battle system work. The reason, I believe, is because a Displayable's actual-rendered height and width can change arbitrarily - imagine if you were using an Animation which has a really small first frame and a really large second frame, for example!

The only way I've found to do it is to call renpy.render() (lower-case 'r') to create a Render from the Displayable in question, and then call get_size() on that Render instance to obtain a tuple of how much space it takes up. You're using the Image/Displayable you pass into ui.image rather than the ui.image instance itself, though. So, something like this:

Code: Select all

python:
    myDisplayable = im.Image("testImage.png")
    myRender = renpy.render(myDisplayable, 800, 600, 0, 0)
    sizes = myRender.get_size()
    x = sizes[0]
    y = sizes[1]
The parameters to renpy.render are width, height, and two times:
- As I understand it, normally, the width and height are the space Ren'Py has allocated to that Displayable (e.g. the inside of a window, if they're being displayed inside a window), but the Displayable can elect to render itself bigger or smaller than that with no consequence, and these numbers will be completely unrelated to the numbers you get out of get_size, which will be the actual size that Displayable renders to.
- The other two parameters are 'st' and 'at'; the number of seconds this Displayable has been shown for so far, and the number of seconds a Displayable with the same tag has been shown for in total.




However, I suspect that the correct way to do it is to create a User-Defined Displayable for whatever else it is you're drawing that depends on the other Displayable's height and width, initialise it passing in the other Displayable, and have it do the size maths in its render() method. The advantage to this is that if the Displayable's size changes over the course of time, your thing-that-depends-on-its-size can change too, because it can pass genuine values into the 'st' and 'at' parameters of renpy.render.

However, I believe that it should be fine to call methods like renpy.render() outside of Displayable rendering code, because:
- They don't actually do any drawing, just return Render instances that Ren'Py core can then use for drawing later on if they get that far
- The Render instances you get from a Displayable are cached for given values of the parameters, so you won't necessarily be making Ren'Py do all the working out every time you call it; it may have been done already for you, and you may be just preparing a cached version for when Ren'Py needs it later.


(I'm using qualified language, and I can't be entirely sure about some of this, because the documentation for user-defined displayables and the related utility methods is somewhat incomplete, and has had mistakes in it in the past... so it may be better to wait for PyTom to come along and tell you whether I've got everything right or not. But on the other hand, I'm doing all the things I describe above in my battle engine code with no problems.)
Server error: user 'Jake' not found

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4067
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: retrieving x,y of a ui.image

#3 Post by jack_norton » Fri Apr 30, 2010 5:07 am

Thanks for the explanation, will make some tests! To complicate it even further, I also scale the enemies when they're less to show them more in details :D
follow me on Image Image Image
computer games

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: retrieving x,y of a ui.image

#4 Post by Jake » Fri Apr 30, 2010 5:38 am

jack_norton wrote:To complicate it even further, I also scale the enemies when they're less to show them more in details :D
I believe this shouldn't make any difference - just pass the scaled Displayable into renpy.render(), it should still give you the right sizes. ;-)
Server error: user 'Jake' not found

Post Reply

Who is online

Users browsing this forum: Bing [Bot]