Displaying probability of success/other dynamic variables in choice menu item

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
Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Displaying probability of success/other dynamic variables in choice menu item

#1 Post by Arcadia »

Yes, I'm back again already!

My half-RPG, half-VN project is at the latest roadblock. I'm working in a somewhat Fallen London-esque style where the probability of success for every action is clearly identified, and I have the code for doing that down fairly well. The big hurdle now is how to actually show it.

I'm using standard menus so far, and I'm having a hell of a time working out how to call my function and return it for each relevant menu item. I know it's easy enough to insert a variable into a menu item, but what I need is to do dynamically.

Does anyone know how to tackle this? Am I better off making a custom menu function?

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

Re: Displaying probability of success/other dynamic variables in choice menu item

#2 Post by philat »

Arcadia wrote: Fri Sep 10, 2021 1:00 am Am I better off making a custom menu function?
Probably. Dunno how you've set everything up but standard menus are a bit inflexible. That said, depending on how the system works, you could pass the probability into a menu argument and thus to the choice screen. Hard to say for certain without knowing more though.

Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#3 Post by Arcadia »

I was worried that might be the case. Just in case, my current code is set up this way:

Code: Select all

    def successChance(stat,target):
        mod = player.get_stat(stat)
        comfactor = target+mod
        chance = 100-comfactor
        return str('chance of success is {}%.'.format(chance))
I have a function I use for when the object is actually clicked, which works fine from my testing.

The menu is just set up like so, using the standard label and menu functions:

Code: Select all

label arbitrarylabel:
 "some text"
 	menu:
 		"Option 1":
 			blahblah
 		"Option 2":
 			blahblahblah
 		...
So essentially what I'm hoping to do is set it up to return "Option 1"+successChance somehow, without having to set it in the label (which is doable, but annoying), so that it returns "Option 1 chance of success is x%.", with x as the returned string. The way I've currently worked out is to set a variable - e.g. $variable1 - under the label to a combined string ('Option 1 '+successChance(stat,target))and change Option 1 itself to be [variable1], but for larger menus that's going to produce a lot of extra code.

Azephir
Newbie
Posts: 8
Joined: Fri Sep 03, 2021 1:05 pm
Projects: The Alchemist - VN bara (azephir.itch.io/the-alchemist)
itch: azephir
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#4 Post by Azephir »

Hello,

Maybe I have a solution for you...

Try this :

Code: Select all

label arbitrarylabel:
	"some text"
	$prob1=sucessChance(stat1, target1);
	$prob2=sucessChance(stat2, target2);
	menu:
		"Option1 [prob1]":
			"Blahblah"
			
		"Option2 [prob2]":
			"Blahblahblah"
		...
	
Hope it i'll work for you.
Cheers

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: Displaying probability of success/other dynamic variables in choice menu item

#5 Post by Remix »

You could pass the stat and target in as kwargs to the item itself then use a modified choice screen to evaluate the chance

Code: Select all

default strength = 18
default dexterity = 18

screen prob_choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            $ prob = ("" if not i.kwargs.get("stat") 
                     else " ({:.01%})".format(
                        float(i.kwargs.get("stat"))/i.kwargs.get("target", 20)))
            textbutton "{}{}".format(i.caption, prob) action i.action

label start:

    menu (screen="prob_choice"):
        "Smash the door down" (stat=strength, target=15): ## stat/target
            "Kpow"
        "Pick the lock" (stat=dexterity): ## Prob evaluated against default 20
            "Kerplunk"
        "Go the other way": ## No prob shown
            "Zoom"
Not sanitized and just using float(stat)/target to get a float value and format :.01% to convert that to percentage
Frameworks & Scriptlets:

Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#6 Post by Arcadia »

Hello again folks! I've finally managed to get back to work after an unexpected hospitalization, and I'm struggling to integrate the code Remix has suggested with my actual needs. Remix's code works great - with one caveat: it doesn't display the actual possibility of success in my use case, as each check has a 1d100 roll added to it. I think I'd usually be able to crack it but an enforced break full of painkillers hasn't done me any favours. Does anyone have insight?

Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#7 Post by Arcadia »

It's a hideous mess but I think I have the makings of a solution that combines my code with Remix's to provide an accurate percentage chance where using a randint. I'm sure someone'll spot the problems in seconds but that's part of being a novice! One of the problems is that my successchance function was, well - bad. Like, completely broken bad. Numbers aren't my strong suit.

Fortunately, it seems - and I'm sure someone will be along to correct me, or at least I hope so! - like that's an easy fix that makes the rest fall into place. First, we need to change the successChance function to:

Code: Select all

def successChance(stat,target):
        dicerange = range(1,100)
        percentilecounter = 0
        mod = player.get_stat(stat)
        for i in dicerange:
            if i+mod >= target:
                percentilecounter += 1
        return str('chance of success is {}%.'.format(percentilecounter))
What we're doing here is setting up an extremely basic - and inefficient, which means it won't scale at all well but for displaying 1 or 2 menu options at a time it should be fine - iterator that literally just checks for the possibility of success for every given possible roll (dicerange = range(1,100), which can be modified if you want to use a d20 system, etc) + the skill modifier exceeding the target number, adding it to the percantile counter. This is then returned as a string, but you could also just return the raw value of percantilecounter, which is probably a better solution! dicerange probably isn't necessary either if you're looking for efficiency - i in range(1,100) should work fine!

Then, we take a modified version of Remix's code:

Code: Select all

screen prob_choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            if i.kwargs.get("stat"):
                $prob = successChance(i.kwargs.get("stat"),(i.kwargs.get("target")))
            else:
                $prob = ("")
                
            textbutton "{}{}".format(i.caption, prob) action i.action
Doing it with the successChance function calls our new, proper evaluator, and prob - where it's actually called - is set accordingly as the return string. I've done a bit of testing, nothing too robust, and it seems to be pretty accurate, other than always returning 99% rather than 100% if it's actually at 100% - and that's probably just down to my mangling the dicerange or needing to set percentilecounter to 1 rather than 0.

Naturally, if anyone spots a major problem with this code, I'd love to hear it. I'm still confused about how half my existing code works after my hospital stay, let alone something with numbers in the mix too.

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

Re: Displaying probability of success/other dynamic variables in choice menu item

#8 Post by philat »

Not that your code is wrong, exactly, but given the way you're calculating it, isn't the success chance just (range - target + stat) / range ? (i.e., if d100 then it's 100 - target + stat / 100, if it's d20 it's 20 - target + stat / 20. Adjust one based on whether it's >= target or > target, I guess, but. )

Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#9 Post by Arcadia »

philat wrote: Wed Oct 27, 2021 9:08 am Not that your code is wrong, exactly, but given the way you're calculating it, isn't the success chance just (range - target + stat) / range ? (i.e., if d100 then it's 100 - target + stat / 100, if it's d20 it's 20 - target + stat / 20. Adjust one based on whether it's >= target or > target, I guess, but. )
Pretty much, yes - this is where it starts to show that I got a little too caught in trying to fix what I had rather than just writing a fresh function, I think. I actually tried working using that equation but it gave some very odd results when I tried doing the maths directly in the screen if/else section via the **kwargs, so I suspect I managed to mangle it somewhere in the mix.

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

Re: Displaying probability of success/other dynamic variables in choice menu item

#10 Post by philat »

Seems likely any weird math is just the integer division thing in python 2.7 (you'll notice that Remix explicitly forces float). You don't HAVE to do the calculation in the screen and honestly if it works, probably not much reason to fiddle with it, I just thought you didn't have to go through a whole function for it.

Arcadia
Newbie
Posts: 13
Joined: Sat Aug 28, 2021 11:31 pm
Contact:

Re: Displaying probability of success/other dynamic variables in choice menu item

#11 Post by Arcadia »

philat wrote: Wed Oct 27, 2021 11:12 am Seems likely any weird math is just the integer division thing in python 2.7 (you'll notice that Remix explicitly forces float). You don't HAVE to do the calculation in the screen and honestly if it works, probably not much reason to fiddle with it, I just thought you didn't have to go through a whole function for it.
I was actually unaware of that quirk of python, so I'm glad you brought it up since knowing about it is going to save me some headaches in future.

Post Reply

Who is online

Users browsing this forum: Google [Bot]