Autoplay for stat grinding.

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
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Autoplay for stat grinding.

#1 Post by Saltome »

...Aaah! >(X_X)< *sigh* I give up!
Now that I've got that out of my system... I will ask for help.
I've been poking at a stat training feature, and I decided to make an autoplay feature, you click a button, and it automatically raises your stat after a set amount of time.
The thing is-- I love to lock renpy into a while loop that runs through an ingame menu. Here's where most of my problems begin.
All the same, I managed to make it function, the only problem I'm not sure how to approach, is that it doesn't show the changes on the screen until after I choose a menu option again.
Now before I jump onto any more serious techniques, I'd like to try and salvage this one, for the time being anyway.

Here's the actual screen:

Code: Select all

screen Training_Overlay(s, o, r, autoplay):
    text "Training Overlay" at top
    vbox:
        yalign 0.1
        xalign 0.05
        $st="Strength: {:.2f} +{:.2f} {:.2%}".format(s, o, r/1)
        text st
        if autoplay:
            timer 0.01 repeat True action If(timespan > 0, true=SetVariable('timespan', timespan - 0.01), false=[SetVariable('timespan', 1), SetVariable('strength', strength+1)])
            bar value timespan range 1 xalign 0.5 yalign 0.9 xmaximum 300
And here is the label that handles the input:

Code: Select all

label Event_11:
    python:
        q= False
        strength=1.0
        m=1.01
        o=0
        r=0
        autoplay=False
    while q == False:
        show screen Training_Overlay(strength, o, r, autoplay)
        menu:
            "Train":
                $ r= 1
                $ o=strength
                $ m= 1 + 0.01* r
                $ strength*=m
                $ o=strength-o
            "Auto-train":
                $ r= renpy.random.random()
                $ autoplay= not autoplay
                $ timespan=1
            "Stop training":
                $ q= True

    hide screen Training_Overlay
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Autoplay for stat grinding.

#2 Post by xela »

I haven't tried the code but the obvious issue that stands out is that you're mixing variables from different scopes, either use globals or reshow the screen with from the menu choices passing them to screens scope directly.
Like what we're doing? Support us at:
Image

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Autoplay for stat grinding.

#3 Post by Saltome »

A risky business I admit, but I'm pretty sure that's not the problem. Actually I'm thinking it has something to do with the renpy menu command.
Since it stops the loop until I give it input, it prevents the show screen call from sending the new values to the screen object.
I did some testing in advance, I can assure you that internally the screen works as expected, it just doesn't redraw until I interact with the menu.
I don't know how to deal with that. Force it to redraw... from the screen... or something.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
ringonoki-ua
Newbie
Posts: 15
Joined: Thu Mar 19, 2015 11:48 am
Projects: Rozumnyk/Sofia
Deviantart: ringonoki
Skype: ringonoki-ua
Location: Kyiv, Ukraine
Contact:

Re: Autoplay for stat grinding.

#4 Post by ringonoki-ua »

You are using the same name for input parameter in function and as a local variable.Maybe setting that variable global and persistent in its initial definition and removing it as a n input parameter from a function definition in brackets will help.Try that.

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Autoplay for stat grinding.

#5 Post by Saltome »

Oh, yeah. Actually you two are right. I don't know why I didn't think about that sooner. Not passing the strength variable through the label seems to have fixed it.

Now there's one more tiny little thing.
When I click on autoplay the menu itself freezes until I click again.
I noticed this happens when I set repeat to true for the timer. But if I don't, the timer doesn't advance.
I want to prevent it from freezing like that.
I assume it has something to do with the screen taking focus..?
Any ideas?
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Autoplay for stat grinding.

#6 Post by philat »

What do you mean the menu freezes?

On a slight tangent, repeating a timer at 0.01 to infinity creates significant lag on less than top of the line machines, in my experience. Just use AnimatedValue for the bar and have the timer at one second instead.

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Autoplay for stat grinding.

#7 Post by Saltome »

Well, it appears to stop redrawing the menu. For example the screen keeps showing the button as being hovered over, and moving the mouse around doesn't show any change. Once you click somewhere on the screen it goes back to normal, if that happens to be where a button is, it executes it's action.

I like your tangent, but I don't know how to use it in practice.
I'm supposed to put it where the action goes, right?

Code: Select all

timer 1 repeat True action AnimatedValue(timespan, 1.0, 1.0)
But it gives me an error after I click the button:
menu:
TypeError: 'AnimatedValue' object not callable
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Autoplay for stat grinding.

#8 Post by xela »

No... what philat suggested is to use AnimatedValue as the bar value, which is the only proper way that it can be used in the first place.
Like what we're doing? Support us at:
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Autoplay for stat grinding.

#9 Post by xela »

Something along these lines following your pattern/logic:

Code: Select all

screen training():
    text "Training Overlay" at top
    vbox:
        align (0.05, 0.1)
        text "Strength: {:.2f} +{:.2f} {:.2%}".format(strength, o, r/1)
        if autotrain:
            timer 1.0 repeat True action Hide("training"), SetVariable('strength', strength+1), Show("training")
            bar value AnimatedValue(value=0, range=strength, old_value=strength) align (0.5, 0.9) xmaximum 300
    
label start:
    $ strength, m, o, r, autotrain = 1.0, 1.01, 0, 0, 0
    show screen training
    call event_11
    return
    
label event_11:
    menu:
        "Train":
            $ r = 1
            $ o = strength
            $ m = 1 + 0.01*r
            $ strength *= m
            $ o = strength - o
        "Auto-train":
            $ r = renpy.random.random()
            $ autotrain = not autotrain
        "Stop training":
            return
    jump event_11
Like what we're doing? Support us at:
Image

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Autoplay for stat grinding.

#10 Post by Saltome »

Ah, now I see how it works.
Also, I don't know what happened but the menu no longer freezes. So I'll just pretend it never happened.
This really isn't turning out as I want so I think it's about time I take a nice long break and maybe look for a new approach.
Thank you for your time.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Autoplay for stat grinding.

#11 Post by Saltome »

Well, after gaining some advice from Xela, and letting it stew for a while. I decided to mix things up a bit, it even supports rollback now.
It still has a functional problem though.
When you rollback then roll forward, the changes made by the autoplay feature get erased.
If you rollback on the normal training feature it has no problem.
In fact, if you rollback on a mixture of training and autotraining commands, renpy restores the right amount of "training commands", but without any of the autotraining commands.
Actually I should note that it restores the right amount of autotraining commands too, it's the strength variable that doesn't get restored.
I assume this is caused by renpy not creating restore points when the timer executes it's action.
Which makes perfect sense, from what I've learned so far, of renpy's moody persona.
...Damn she really must be a woman. x)
If my logic is correct I should be able to fix it by instructing renpy to create such a point, as part of the timer action.
The question is, does such an instruction even exist?
Here's the actual code:

Code: Select all

init python:
    class Game_Training_Overlay(renpy.store.object):
        def __init__(self, training_actor):
            self.training_actor=training_actor
            self.random_factor=1
            self.autoplay=False
        def autotrain(self):
            self.random_factor=renpy.random.random()
            self.training_actor= self.training_actor+((1/self.training_actor)*self.random_factor)
            store.strength=self.training_actor#WARNING: HOTWIRED!
        def train(self):
            self.random_factor=1
            self.training_actor+=1/self.training_actor
            store.strength=self.training_actor#WARNING: HOTWIRED!
        def toggle_autoplay(self):
            self.autoplay=not self.autoplay
        def get_string(self):
            return "Strength: {:.2f}".format(self.training_actor)
screen Training_Overlay():
    vbox at top:
        vbox at top:
            text "Training Overlay"
        vbox at top:
            if training_logic.autoplay:
                timer 1.0 repeat True action Hide("Training_Overlay"), training_logic.autotrain, Show("Training_Overlay")
                bar value AnimatedValue(value=1.0, range=timespan, old_value= 0.0) align (0.5, 0.9) xmaximum 300
    vbox:
        yalign 0.1
        xalign 0.05
        
        
        text training_logic.get_string()   
label start:
    $strength=1.0
    python:
        training_logic= Game_Training_Overlay(strength)
        q= False
        timespan=0
    show screen Training_Overlay()
    while q == False:
        #$autoplay=training_logic.autoplay
        
        menu:
            "Train" if not training_logic.autoplay:
                python:
                    training_logic.train()
            "Auto-train: [training_logic.autoplay]":
                $training_logic.toggle_autoplay()
                $ timespan=1
            "Leave" if not training_logic.autoplay:
                $ q= True

    hide screen Training_Overlay
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

Post Reply

Who is online

Users browsing this forum: No registered users