[SOLVED] Random number for animation that renew each time the animation play?

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
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

[SOLVED] Random number for animation that renew each time the animation play?

#1 Post by Black Cat 2412 »

Good day guys,

I have an animation like below:

Code: Select all

image bg_1 light:
    "bg 1 light.png"
    alpha 0.05
    linear renpy.random.random(1, 10) alpha 1
    repeat
The problem is that the "renpy.random.random" part only work once: after the first time it randomized it's dead set for every other loop. I want it to randomize anew whenever the animation repeats, but how can I do it?. I did found this thread but Dinova's function would not work with a linear ATL.

Thank for reading!
Last edited by Black Cat 2412 on Sun Mar 25, 2018 3:52 am, edited 3 times in total.

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Random number for animation that renew each time the animation play?

#2 Post by Lord Hisu »

I *think* an ATL block can't be modified once generated.
You can do your own function and use it inside the ATL block, though.

Code: Select all

init:
    python:
        limit = renpy.random.randint(1, 5)
        def custom_atl(trans, st, at):
            # A function called inside the ATL block should receive these 3 arguments. More in the docs: https://www.renpy.org/doc/html/atl.html#function-statement
            # Also, we should return the time in which this function will be called again. If None is returned, the control is passed to the next ATL statement.
            global limit
            if st > limit:
                trans.alpha = 1.0
                limit = renpy.random.randint(1, 5)
                return None
            trans.alpha = st/limit
            return .01
            
    image img:
        "test.png"
        alpha 0.05
        function custom_atl # This is a custom linear
        repeat

label start:
    show img
    "..."
    jump start

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: Random number for animation that renew each time the animation play?

#3 Post by Remix »

Renpy's random module pushes values into the store, so they are available in saves/rollback etc.
You should be able to get a random that doesn't save by using Python's random module instead.

Just remember to import the module first

Code: Select all

init python:
    import random 
    
image bg_1 light:
    "bg 1 light.png"
    alpha 0.05
    linear random.randint(1, 10) alpha 1
    repeat
Frameworks & Scriptlets:

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Random number for animation that renew each time the animation play?

#4 Post by Lord Hisu »

Remix wrote: Thu Mar 22, 2018 2:59 pmYou should be able to get a random that doesn't save by using Python's random module instead.
I swear I tested this before working on my example. :x
It didn't work for some reason, then I just thought the block couldn't be modified once generated. Good to know it works.

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Random number for animation that renew each time the animation play?

#5 Post by Black Cat 2412 »

Thank for replying! ^^
Lord Hisu wrote: Thu Mar 22, 2018 2:20 pm

Code: Select all

init:
    python:
        limit = renpy.random.randint(1, 5)
        def custom_atl(trans, st, at):
            # A function called inside the ATL block should receive these 3 arguments. More in the docs: https://www.renpy.org/doc/html/atl.html#function-statement
            # Also, we should return the time in which this function will be called again. If None is returned, the control is passed to the next ATL statement.
            global limit
            if st > limit:
                trans.alpha = 1.0
                limit = renpy.random.randint(1, 5)
                return None
            trans.alpha = st/limit
            return .01
            
This works perfectly! Thank you so much!! ( I have no idea whatsoever about function-they seems so ailen to me : ( But I will look into it, since it seems to be very promising)
Remix wrote: Thu Mar 22, 2018 2:59 pm Renpy's random module pushes values into the store, so they are available in saves/rollback etc.
You should be able to get a random that doesn't save by using Python's random module instead.

Just remember to import the module first
Doing this does not work for me either, it just did not reset the random number no matter how many time I tried (I timed it so I'm absolutely sure)

Anyhow, thank you all ^^

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: [SOLVED] Random number for animation that renew each time the animation play?

#6 Post by Lord Hisu »

A function is a block of code that will be executed every time you call it.
Honestly, this is a rather complex function to start with for people who never programmed before.

What you need to understand is that 'def' is a keyword that declares a function, the name in the right is the name of the function, what's inside the parentheses are arguments the function will work with, and everything inside the block above it are commands that do something with the arguments.

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: [SOLVED] Random number for animation that renew each time the animation play?

#7 Post by Black Cat 2412 »

Update: A problem pops up when I tried to make the animation loop

Code: Select all

init python:
    limit = renpy.random.randint(1, 5)
    def custom_atl(trans, st, at):
        global limit
        if st > limit:
            trans.alpha = 1.0
            limit = renpy.random.randint(1, 5)
            return None
        trans.alpha = st/limit
        return .01
# The image
image bg_1 light:
    "bg 1 light.png"
    alpha 0.5
    function custom_atl
    linear 2.0 alpha 0.5
    repeat 
In theory, it should loops seamlessly, but instead the image flickers (suddenly disappears for a noticeable fraction of a sec then reappears) whenever it repeats .
This problem does not happen if I use the traditional method:

Code: Select all

image bg_1 light:
    "bg 1 light.png"
    alpha 0.5
    linear 0.5 alpha 1.0
    linear 2.0 alpha 0.5
    repeat
So what could it be? the function not interacting well with atl or something?

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Random number for animation that renew each time the animation play?

#8 Post by Lord Hisu »

Black Cat 2412 wrote: Fri Mar 23, 2018 1:25 pm Update: A problem pops up when I tried to make the animation loop
I'm sorry, this is my fault. :lol:
My math was wrong. I thought about a mathematical function that would give me y(0) = 0 and y(limit) = 1, but this is wrong. Since the initial value was 0.05 before, the flicker was there, but we couldn't notice. It was jumping from 0.05 to 0, and then going up to limit. When we jump from 0.5 to 0, the jump can now be seen. That's why it flickers. Now I believe I did the right math... I think this is the family of linear functions in which y(0) = 'start_alpha' and y(limit) = 1: st/limit * (1 - start_alpha) + start_alpha

Code: Select all

init python:
    limit = renpy.random.randint(1, 5)
    start_alpha = 0.5  # You can change the start alpha in just one place now
    def custom_atl(trans, st, at):
        global limit, start_alpha
        if st > limit:
            trans.alpha = 1.0
            limit = renpy.random.randint(1, 5)
            return None
        trans.alpha = st/limit * (1 - start_alpha) + start_alpha
        return .01
        
image test:
    "test.png"
    alpha start_alpha
    function custom_atl
    linear 2.0 alpha start_alpha
    repeat
    
label start:
    show test
    "..."
    jump start
Anyway, the ATL was doing exactly what I told it to do. I just told it to do the wrong thing. :mrgreen:

User avatar
Black Cat 2412
Regular
Posts: 74
Joined: Wed Aug 16, 2017 10:10 am
Projects: Rapunzel: A classic retold
Deviantart: BlackCat2412
Location: Vietnam
Contact:

Re: Random number for animation that renew each time the animation play?

#9 Post by Black Cat 2412 »

Thank you so much, I tested and it works great now! :D

Post Reply

Who is online

Users browsing this forum: No registered users