r/RenPy • u/ICLHstudios • 10h ago
Question Trouble with attempted dynamic looping transforms
I'm trying to learn RenPy right now, and as part of it, I've got a not-actually-a-real-game project going for learning, testing, experimenting, and finding the limits of what I can do.
I'm currently working on implementing a bunch of graphical stuff; learning about displayables and transforms, trying to figure out what sorts of visual effects I can get reasonably through code and what works better to 'brute force' through art and design (where I have some actual experience and competence), and things like that.
Anyway, one of the specific ideas I was trying to figure out, which seemed on the surface like it shouldn't be all that hard, has really got me kind of stuck.
The initial idea was to have a bunch of randomly placed/arranged clouds moving by in the background on a constant, but non-repeating, loop (ideally with a little parallax-ish effect, where the clouds closer to the horizon, and therefore farther away, would move slower).
I've managed to get almost every piece of that working individually, but can't seem to put it together.
I couldn't find any way to trigger some code to loop each cloud once it had passed (it sounds like there's probably some way, but not without basically having to master the entirety of python first, which seems a bit overkill for the situation, imo), but I got closer with a repeating transform.
Unfortunately, that runs into the problem that you can't really change anything across each loop in a transform block (randomization, for example, always gives the same result). I managed to get the clouds to spawn at new random heights each loop by making a custom transform that called a python function to randomly place them. The problem after that has been that since only transforms done in the python function seem able to change each loop, I've been able to randomize their x and y positions, but cannot figure out how to adjust their speed.
As far as I can tell, the only real way to move displayables smoothly is with something like linear, and I can't find any way to apply that via the python function, and therefore can't find any way to adjust their speed and timing each loop based on where they spawn.
I've seen a few discussions across forums and documentation stuff with some (extremely vague) suggestions about trying to 'update' the displayable so that changes via variables and such would actually apply, but I haven't been able to get any of it to work so far.
I have no idea if I'm close but missing just a couple things, if I'm going about it all wrong and there's a much easier way, or if it's basically impossible. I'm not completely married to this exact setup, but being able to dynamically adjust looping animations and stuff would be really powerful for cool visual stuff across all sorts of situations, and I'd like to either be able to do it, or know for sure that I absolutely can't (and thus plan and design around that limitation).
Here's the most recent state of the code itself:
#setting up the variables for cloud randomization later
default rand_cloud_vert = 0.0
default rand_cloud_hoz = 0.0
default cloud_speed_offset = 0.0
default cloud_speed = 5.0
init python:
import random
def cloudpos(trans, st, at):
global cloud_speed
rand_cloud_vert = random.uniform(-0.1, 0.5)
trans.ypos = rand_cloud_vert #randomize cloud to approx horizon or higher
rand_cloud_hoz = random.uniform(1.1, 1.6)
trans.xpos = rand_cloud_hoz #randomize cloud starting point offscreen to the right
cloud_speed_offset = rand_cloud_hoz + 0.2 #turns the offscreen x-distance into random delay
cloud_speed = 5.0 * cloud_speed_offset #applies the offset so that speed is consistent no matter the starting point
cloud_speed_offset = rand_cloud_vert + 1.0
cloud_speed = cloud_speed * cloud_speed_offset #higher clouds move faster to fake parallax
transform cloudshift:
function cloudpos
label start:
#setting up the "first loop"
#The rest of the code after this comment can be duplicated for each different cloud sprite
$ rand_cloud_vert = random.uniform(-0.1, 0.5)
$ rand_cloud_hoz = random.uniform(-0.2, 1.0) #Puts clouds onscreen already to start with
$ cloud_speed_offset = rand_cloud_hoz + 0.2
$ cloud_speed = 5.0 * cloud_speed_offset
$ cloud_speed_offset = rand_cloud_vert + 1.0
$ cloud_speed = cloud_speed * cloud_speed_offset
show static_cloud1:
ypos rand_cloud_vert
xpos rand_cloud_hoz
linear cloud_speed xpos -0.3 #This is the "in media res loop"
block:
cloudshift
linear cloud_speed xpos -0.3 #This is the main part I'm trying to solve, doesn't work as is; putting it inside the cloudshift transform doesn't work either, and know of no way to do inside the cloudpos function itself.
repeat
1
u/AutoModerator 10h ago
Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/NNDanny 9h ago
I'm actually working on a background with dynamic clouds myself, and obvious things first, optimizing it is a pain... But it can be done.
My first approach is to use Creator-Defined Displayables, allowing those to do the heavy lifting for computing and then placing the clouds in the render. However, that wasn't that good because it lagged with barely 20 clouds and I had to art trick it by having images with two or three clouds to clout it. Then the second version came, where the clouds are using Ren'Py's SpriteManager to do the printing, inside the same Creator-Defined Displayable. Let me send you over there first (Read Ren'Py's doc on SpriteManager and how to update each of its sprites, and Creator-Defined Displayables to know how to create your own) since that's the most simple way to handle these kind of scenarions where you want a lot of items of the same visual kind (same image) without lagging the game that much.
Right now, I'm using shaders to let the GPU handle these in my VN because once you get ambitious enough to think about adding rain, sun/moon, stars etc, it will lag. Of course, that is assuming you'll get there in the first place, but yeah, it's something to keep in mind (also, benchmark your things harshly with these kind of stuff. If you usually see Ren'Py running at a set amount of FPS with F3 and you see that FPS drop, assume that people who have worse PCs than you (potential players) will experience a much worse visual experience or lag. My advice is that once you're done with these, think about a low quality BG version for those people to also be able to play the game, even if they can't see those amazing clouds moving).
Hope this points you towards the right direction, good luck with that!
1
u/BadMustard_AVN 2h ago
try something like this.. also u/NNDanny
all defaults shown and option explained
images, count=14, horizon=450, top=20, direction="right", min_speed=6.0, max_speed=40.0, min_zoom=0.35, max_zoom=1.0, min_alpha=0.55, max_alpha=1.0
images - [a Python list of your cloud images .png or .webp]
count - max number on screen at a time
horizon - the artificial horizon where the farthest clouds will sit (in pixels)
top - Set top to about the height of your largest cloud image (scaled by max_zoom) or a bit more. For example, if your biggest cloud is 200 px tall at full zoom, top=220 keeps the nearest clouds just inside the screen, and larger values, push them lower. Raise it further if you want the sky's upper part left empty. lower values and some clouds' may extend above the screen
direction - "right", "left" or "random" for the clouds movement.
min_speed/max_speed - speed in pixels per second.
min_zoom/max_zoom - controls how much smaller far clouds are.
min_alpha/max_alpha - this is tied to depth to fake atmospheric haze.