On coding a complex stat system... [Solved]

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
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

On coding a complex stat system... [Solved]

#1 Post by soggybottoms »

Well, complex for this newbie anyway. n_n I'm trying to code a stat system for my game and would really appreciate some feedback from more advanced programmers to see if my method is good or if there is a more efficient one I could be using instead.

Here's an example of what I'm going for...

Code: Select all

                                if love >= 30: #in love - between 50 and 30, lose 25 points
                                    $ love -= 25
                                elif love >= 15: #very close - between 30 and 15, lose 14 points
                                    $ love -= 14
                                else: #slightly close - between 15 and 1, lose total points
                                    $ love -= love
As you can see from this very trimmed-down example, what I'm trying to accomplish is a system that will decrease less of a percentage of the stat the greater that stat is. If it's not reached a certain amount, the entire thing is deleted. Much like real relationships - the closer you are, the more you can work things out; if it's not that serious yet, it's much more fragile.

It's working alright, but as my code gets more complex putting it in multiple times is becoming a hassle and making my document super long. I'm wondering if there isn't some fancy equation using exponents or some other type of mathematical device I'm less familiar with that I could use to get roughly the same results while taking up a fraction of the space. Thanks a lot!
Last edited by soggybottoms on Mon Dec 08, 2014 4:26 pm, edited 2 times in total.

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

Re: On coding a complex stat system...

#2 Post by xela »

Like what we're doing? Support us at:
Image

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#3 Post by soggybottoms »

Um... I'm not sure how that's supposed to help me. I'm not having problems keeping mins and maxes within their defined limits.. can you explain?

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

Re: On coding a complex stat system...

#4 Post by xela »

Stat ranges are incidental. Example Pytom provides in the link shows you how not to use that code of yours multiple times.

Your problem is that you're forced to write that codebit multiple times, proper solution is not to make the codebit shorter with an equation, that is doable but you'll prolly loose some of the freedom you enjoy with the ifelif forks. Solution should be writing that codebit once and getting it to work automatically whenever love is changed. Link shows you how to do that using a python callback.

I am on android right now so I can't right you example of code. You can also simply put that bit in a label or a function. It will work just as well.
Like what we're doing? Support us at:
Image

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#5 Post by soggybottoms »

Ohh, I see. Thanks for that then. n_n Well since my code is actually happening within a pretty complicated Class function, I'm not sure how I would utilize that.

I'm only needing to write the code over and over into different sections of the Class, then using the results in the if-statements outside of the Class, so I don't see a loss of flexibility to be a concern. If you happen to know how to smooth it out with an equation, I'd love the insight!

Honestly, even just knowing the name of that type of equation would be so helpful. I've been googling everything I can think of - exponential progressive growth, linear extrapolation, percentage of integer functions.... I barely squeaked by in college algebra so this is way over my head but I'm sure I could implement it if I knew what I was looking for.

The problem I'm finding with my Class code is that no matter how short I make the increments between "in love" and "very close", for example, once a new increment is reached the point loss results in the same number as the increment before it. I'd have to use increments of one number per if-statement to get a truly smooth output. I hope I'm explaining my issue accurately.

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#6 Post by soggybottoms »

I'm guessing it probably involves using the current value of "love" with the maximum value (50) and somehow mashing that to determine how many points should be reduced based on how many there already are... but then that might be too straightforward. I still want to have a 'step system' where once you reach a certain amount of points, you won't lose as many of them as you would have previously.

Someone who is "in love" (with 30 or more points) when the negative event occurs should end up with between 6 and 10 points left.

Someone who is "very close" (at 15 - 29 points) when the event occurs will lose more, ending up with between 1 and 4 points.

Someone only "slightly close" with less than 15 points when the event occurs would be left with no points.

Maybe that's how the equation would work even without an extra 'step system' written in. I can't tell. x_x

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

Re: On coding a complex stat system...

#7 Post by xela »

Code in the front post cannot be a part of a class, it has renpy script statements in it.

It also seem to contradict your post or you're not providing the whole picture. That code will take off more points on higher love, not less, making good relationship harder to fix and more difficult to patch up. Your explanation suggest that you want the opposite effect.

So you've confused the hell out of me...

Based off your last post, what you need are percentages.

get a percentage of current value - minrange out of maxrange - minrange.

Then get one percent of the difference between min and max target ranges and multiply that by the percentage. Round up and add to the min target range.
Like what we're doing? Support us at:
Image

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#8 Post by soggybottoms »

Yeah, the code I posted in my original was a mock-up example of the part of code in my Class system I'm having trouble with. It doesn't have the renpy scripting in the actual one.

For that matter, "love" isn't an actual variable for my code either. Let me try to clear things up cause I know I've been confusing. :) I'm building a raising sim. The disciplinary system (ie. Tamagotchi's punish or praise type deal) is the Class I'm talking about, in which multiple factors are considered to determine the character's response to disciplinary types. Some of these factors include age, present bond, present behavioral status, etc. It's involved. x_x

Not even sure if this will lead to a game that's playable and actually fun, but I'm still very new to coding and geeking out about the experimental process of just learning how to do it at all. I hope that makes things a bit more clear. :D

As for the percentage thing you've posted, yes that sounds like probably exactly what I've been looking for.. but my brain struggles a lot with abstracts and I'm having a horrible time trying to figure out how I would put it into actual code. You sound like you really know what you're talking about - do you think you could use my "love" example to illustrate that formula in a way I could apply it?

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

Re: On coding a complex stat system...

#9 Post by xela »

soggybottoms wrote:As for the percentage thing you've posted, yes that sounds like probably exactly what I've been looking for.. but my brain struggles a lot with abstracts and I'm having a horrible time trying to figure out how I would put it into actual code. You sound like you really know what you're talking about - do you think you could use my "love" example to illustrate that formula in a way I could apply it?
Try:

Code: Select all

if love < 15:
    love = 0
elif love < 30:
    love = 1 + int(round((love - 15)/14.0 * 3))
else:
    love = 6 + int(round((love - 30)/20.0 * 4))
But your system still sounds confusing :)
Like what we're doing? Support us at:
Image

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#10 Post by soggybottoms »

Wooow... the mind boggles at fancymath! Thank you so much!! XD Hopefully if I ever actually turn this into a game it'll make more sense haha.

User avatar
Marionette
Regular
Posts: 128
Joined: Thu Apr 21, 2011 12:04 pm
Completed: https://marionette.itch.io/
Projects: Get Meowt of Here
Deviantart: rexx9224
itch: marionette
Location: Ireland
Discord: Marionette#2995
Contact:

Re: On coding a complex stat system...

#11 Post by Marionette »

An slightly easier way might be to have something like an extra love tier variable

So lovetier = 1 # in love
lovetier = 2 # very close
lovetier = 3 # slightly close

And then for your event just have

$love = love - (lovedmg*loveTier)

So if lovedmg = 5 then tier 1 takes 5 pts dmg, 2 takes 10 and so on. ie. Less damage to the relationship the closer they are.

Then just have the tier updated each time you finish updating the current love value.

Code: Select all

UpdateLoveFunction() {
  if love >= 30: #in love 
      $ lovetier = 1
  elif love >= 15: #very close 
      $ lovetier = 2
  else: #slightly close 
      $ lovetier = 3
}
Not really a better way to do it than the other one mentioned, but might be easier to follow in your code at a glance. :p

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: On coding a complex stat system...

#12 Post by soggybottoms »

Thank you, I really like the look of that one too (it's a bit easier to wrap my head around lol). I'm gonna experiment with both of them! I really appreciate all the help.

Post Reply

Who is online

Users browsing this forum: Google [Bot]