Stat Based Initiative System

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
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Stat Based Initiative System

#1 Post by Meinos Kaen »

Hello thar, Meinos Kaen here.

My fan game is doing great, to the point that I have also started working on my own original game. The art is flowing in, I'm doing better with code, but there's one thing that still eludes me and that is an Initiative system for my battles.

In both the fan game I'm working on and the original project I feature turn-based battles. So far, the acting order has been pre-ordained by me. Renpy goes through a list and depending if the switch is on or off, it goes to that character's turn or the next in the list, until you reach the last and it resets everything.

This, though, has obvious limitations -including the fact that sometimes certain characters will act twice in a row, like the switch doesn't properly set for some reason-. So, I'd like to migrate to a stat Based initiative system, where the higher of a certain STAT a character has the further up the list he/she goes. How would you do it?

Also, this is how I've been setting up battle order so far:

Code: Select all

if moveEvelyn == True and moveJack == True and movePenny == True and moveDavid == True and moveBH1 == True and moveBH2 == True and moveBH3 == True and moveBH4 == True:
    $ moveJack = False
    $ movePenny = False
    $ moveDavid = False
    $ moveEvelyn = False
    $ moveBH1 = False
    $ moveBH2 = False
    $ moveBH3 = False
    $ moveBH4 = False
    if enDefence == True:
        $ endefence -= 35
        $ enDefence = False
    if enBoost == True:
        $ enattack -= 30
        $ enBoost = False

if moveEvelyn == False:
    jump TurnEvelynBH1
elif moveJack == False:
    jump TurnJackBH1
elif moveBH1 == False:
    jump TurnBH1
elif movePenny == False:
    jump TurnPennyBH1
elif moveBH2 == False:
    jump TurnBH2
elif moveDavid == False:
    jump TurnDavidBH1
elif moveBH3 == False:
    jump TurnBH3
elif moveBH4 == False:
    jump TurnBH4
The double turn issue is probably caused by the fact that and only works with two conditions, doesn't it?

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

Re: Stat Based Initiative System

#2 Post by philat »

This is a pretty complex system and getting it set up in a sophisticated fashion would likely take way too much time. As such, the simplest suggestion I have is to put all characters in a list and iterate through them. (You mentioned that you use a list but there's no evidence of that in the code you provided.) Determining the list order dynamically is just a matter of sorting the list by some other value (say, a speed stat), but in I don't know enough about what you want specifically to suggest anything further.

Code: Select all

$ initiative_list = ["Jack", "Penny", "BH1", "David"]
$ counter = 0

label battleloop:
    if counter == len(initiative_list):
        jump breakloop
    counter += 1
    jump expression "move" + initiative_list[counter-1]

label moveJack:
    # do stuff
    jump battleloop

label breakloop:
    # do more stuff - if you want to loop through again, reset counter and then jump back to battleloop
For instance, this simple code would jump to moveJack, movePenny, moveBH1, and moveDavid in that order.

User avatar
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Re: Stat Based Initiative System

#3 Post by Meinos Kaen »

philat wrote: Sat Jul 22, 2017 12:56 pm This is a pretty complex system and getting it set up in a sophisticated fashion would likely take way too much time. As such, the simplest suggestion I have is to put all characters in a list and iterate through them. (You mentioned that you use a list but there's no evidence of that in the code you provided.) Determining the list order dynamically is just a matter of sorting the list by some other value (say, a speed stat), but in I don't know enough about what you want specifically to suggest anything further.

Code: Select all

$ initiative_list = ["Jack", "Penny", "BH1", "David"]
$ counter = 0

label battleloop:
    if counter == len(initiative_list):
        jump breakloop
    counter += 1
    jump expression "move" + initiative_list[counter-1]

label moveJack:
    # do stuff
    jump battleloop

label breakloop:
    # do more stuff - if you want to loop through again, reset counter and then jump back to battleloop
For instance, this simple code would jump to moveJack, movePenny, moveBH1, and moveDavid in that order.
Exactly, I'd like to sort the list dynamically with a SPEED stat. How would that be done in a simple fashion?

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: Stat Based Initiative System

#4 Post by Remix »

If you held objects rather than strings in the initiative_list you could access attributes of those objects in order to sort...
Typed on the fly, so ummm debug at leisure...

Code: Select all

$ initiative_list = [jack, penny, bh1, david] # these are the object references, like for e = Character("Eileen") we would use e

label battleloop: # or screen, or whatever
     initiative_list.sort(key=lambda x: x.speed) # sort by attribute speed of each object
     for actor in initiative_list:
         "[actor.name]"
Frameworks & Scriptlets:

User avatar
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Re: Stat Based Initiative System

#5 Post by Meinos Kaen »

Remix wrote: Tue Jul 25, 2017 6:53 am If you held objects rather than strings in the initiative_list you could access attributes of those objects in order to sort...
Typed on the fly, so ummm debug at leisure...

Code: Select all

$ initiative_list = [jack, penny, bh1, david] # these are the object references, like for e = Character("Eileen") we would use e

label battleloop: # or screen, or whatever
     initiative_list.sort(key=lambda x: x.speed) # sort by attribute speed of each object
     for actor in initiative_list:
         "[actor.name]"
So basically I define a new class of, let's say, Fighters, create the objects and put them in the initiative list. Let's say I wanted to make the Attributed Speed of the class Fighters dependant on another stat, how would I go about that?

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: Stat Based Initiative System

#6 Post by Remix »

There are a number of options and it mostly depends upon how you are defining your battlers and how you are giving them stats in the first place. Is Penny a renpy.Character() or overloaded sub class of that? Is BH1 a user defined class Monster() or a python dictionary?

Your likely (and I say likely as without more info I am guessing wildly) easiest case is to pull the 'speed' or 'initiative' from a function...

initiative_list.sort( key=lambda x: get_initiative( x ) )
# sort list based upon numeric return of a get_initiative function that would be coded to account for all possible objects that each x could be
Frameworks & Scriptlets:

User avatar
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Re: Stat Based Initiative System

#7 Post by Meinos Kaen »

Remix wrote: Sun Sep 24, 2017 6:23 pm There are a number of options and it mostly depends upon how you are defining your battlers and how you are giving them stats in the first place. Is Penny a renpy.Character() or overloaded sub class of that? Is BH1 a user defined class Monster() or a python dictionary?

Your likely (and I say likely as without more info I am guessing wildly) easiest case is to pull the 'speed' or 'initiative' from a function...

initiative_list.sort( key=lambda x: get_initiative( x ) )
# sort list based upon numeric return of a get_initiative function that would be coded to account for all possible objects that each x could be
Wasn't using class definitions so far. I was defining each stat as a separate statement, since I also needed them for other screens.

Code: Select all

# Battle Stats II
init:
    $ alvl = 1 # Aura Level
    $ atk = 5 # David Attack
    $ end = 5 # David Endurance 
    $ stm = 5 # David Stamina
    $ inl = 5 # David Intelligence
    $ spd = 5 # David Speed
    $ baura = ((stm + alvl) * 100) # David Base HP
    $ aura = baura # David HP
    $ ini = ((spd + alvl) * 10) # David Initiative Level
How would I got into turning them into a Class Object?

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: Stat Based Initiative System

#8 Post by Remix »

Well, once again there are numerous ways and it all rather depends upon what you really want...
A class that 'only' holds named data values might as well be a dictionary or even just a lambda:0 (var = lambda:0 creates an object that supports dot notation variables)

Anyway... a more readable version might be:

Code: Select all

init python:
    # Basic class that supports dot notation 
    # var = Entity( a=7 )
    # print var.a >> 7
    class Entity(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
        # Most people would add more methods to make this class more
        # relevant to the things it is used for, e.g. maybe a 'heal' method that tops up hp

    # note that this function is not part of the class
    # we pass the object into the function as we might be dealing with more than just Entity objects
    def get_initiative( obj ):
        global alvl
        return ((obj.spd + alvl) * 10) if hasattr( obj, 'spd') else 100
        # return the math if the object has a .spd attribute, else just default 100

default alvl = 1
default david = Entity( name="David", atk=5, spd=5 ) # etc with the rest

label start:
    $ test = get_initiative( david )
    "David's Initiative is [test]"
Frameworks & Scriptlets:

User avatar
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Re: Stat Based Initiative System

#9 Post by Meinos Kaen »

Remix wrote: Sun Sep 24, 2017 7:44 pm Well, once again there are numerous ways and it all rather depends upon what you really want...
A class that 'only' holds named data values might as well be a dictionary or even just a lambda:0 (var = lambda:0 creates an object that supports dot notation variables)

Anyway... a more readable version might be:

Code: Select all

init python:
    # Basic class that supports dot notation 
    # var = Entity( a=7 )
    # print var.a >> 7
    class Entity(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
        # Most people would add more methods to make this class more
        # relevant to the things it is used for, e.g. maybe a 'heal' method that tops up hp

    # note that this function is not part of the class
    # we pass the object into the function as we might be dealing with more than just Entity objects
    def get_initiative( obj ):
        global alvl
        return ((obj.spd + alvl) * 10) if hasattr( obj, 'spd') else 100
        # return the math if the object has a .spd attribute, else just default 100

default alvl = 1
default david = Entity( name="David", atk=5, spd=5 ) # etc with the rest

label start:
    $ test = get_initiative( david )
    "David's Initiative is [test]"
Thank you very much. Do you have any reading that I can use to familiarize myself more with classes and objects? I'd like to learn and make such custom things on my own.

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: Stat Based Initiative System

#10 Post by Remix »

Tutorials and Guides for Novice and Higher

Not really sure which ones are any good.
Also, just googling 'python novice' or beginner or tutorial or whatever will yield lots
Frameworks & Scriptlets:

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: Stat Based Initiative System

#11 Post by Remix »

On an aside:

Note: Ren'py up to 6.99.13 sits on top of Python 2.7 so some python3 tutorials might not work without minor tweaks.

Always feel free to ask in this forum for anything python related too. There are a lot of talented programmers here.
Frameworks & Scriptlets:

User avatar
Meinos Kaen
Regular
Posts: 107
Joined: Wed Jan 04, 2012 1:01 pm
Completed: JPDE - Sonata of Fire
Projects: JPDE, Ars Oratoria, ONHA
Skype: therealmeinoskaen
Location: Somewhere in Italy...
Contact:

Re: Stat Based Initiative System

#12 Post by Meinos Kaen »

Remix wrote: Tue Sep 26, 2017 5:52 am On an aside:

Note: Ren'py up to 6.99.13 sits on top of Python 2.7 so some python3 tutorials might not work without minor tweaks.

Always feel free to ask in this forum for anything python related too. There are a lot of talented programmers here.
I noticed! Thank you very much

Post Reply

Who is online

Users browsing this forum: No registered users