FSM and Ren'Py(label)

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
badanni
Newbie
Posts: 9
Joined: Fri Jan 24, 2020 11:57 am
itch: badanni
Location: Quito, Ecuador
Contact:

FSM and Ren'Py(label)

#1 Post by badanni »

My native language is Spanish, sorry for erratum

FSM and Ren'Py
I wrote a small code, FSM (finite state machine) implementation for Ren’Py, FSM have a draw (graphic) that is a representation of the logic and the flow of process (label for Ren’Py).

https://img.itch.zone/aW1nLzI5MDgyMDUuc ... 3WaxwW.png This is a FSM draw

Why to use FSM?
Well, when your history/game have very complex paths, is more easy only change the logic, with that you can concentrate in the writing process. For example you need to add a new scene between two scenes, that need to check the code and validate that don’t break the code, but with FSM you only change the logic add a new line and change one.
The logic before change:

Code: Select all

IF scene_old1 SEND next GOTO scene_old2
IF scene_old2 SEND next GOTO scene_old3
The logic after change:

Code: Select all

IF scene_old1 SEND next GOTO scene_new1
IF scene_new1 SEND next GOTO scene_old2
IF scene_old2 SEND next GOTO scene_old3

How to use?
First, circle in the figure is a script (label and its content), arrows are how they move (flow) and the pharse to continue (return value), for exmple "CH1" is.

Code: Select all

label ch1:
a "bla bla bla"
return "next"
In the "script.rpy" write this.

Code: Select all

Label start:
 $ logic_game=FSM_logic()
     $ state=logic_game.logic("")
     call expression state
     while (_return!="gracias"): #key phrase to finish
         $ state=logic_game.logic(_return)
         call expression state
         # $ print logic_game.read_logger()
     return
I wirte the class and an aplication "Logika" to create the rpy class with the logic you write it, recognize uppercase and lowercase letters, the phrases for logic can be in both, like START, start, OR, or, SEND, send, etc .
https://img.itch.zone/aW1nLzI5MDgyMTQuc ... AZUhrB.png Logika aplication

The logic for the FSM draw is:

Code: Select all

START Prologue
IF Prologue SEND next GOTO ch1
IF ch1 SEND next GOTO ch2_beginnig
IF ch2_beginng SEND next GOTO path1a
IF ch2_beginng SEND next2 GOTO path2a
IF ch2_beginng SEND next3 GOTO path3a
IF path1a SEND next GOTO path1b
IF path1b SEND back GOTO path1a
...
...
the sintax is:

Code: Select all

START "start scene name"

Code: Select all

IF "scene name" SEND "transition condition1" OR "transition condition2" GOTO "scene name2"
The class is:

Code: Select all

init -1 python hide:
    config.log = "debuglog.txt"

init python:

    import math
    import pickle

    class FSM_logic(object):
        '''Finite State Machines logic for game'''
        def __init__(self, label_0="Prologue",logger=False):
            self.state=label_0
            self.logika = logger
            self.log_FSM=[]
        def logger(self,data):
            f=open("debug.img","wb")
            pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
            f.close()
            return 0
        def read_logger(self):
            s=file("debug.img","rb")
            value=pickle.load(s)
            s.close()
            return value
        def logic(self,command):
            old_state=self.state
            ####### 
            put here logic here
            #######
            self.log_FSM.append(self.state)
            if self.logika:
                self.logger(self.log_FSM)
            if renpy.has_label(self.state):
                return self.state
            else:
                renpy.log("Warning: Something happened there is no label: {} / Class FSM_logic".format(self.state))
                renpy.notify("Something happened there is no label: {}".format(self.state))
                #puedo configurar para que retroceda un paso
                return old_state
                #return "error404"
The aplication is wirten in python3 and PyQt5, is for change logic to python logic and add that too the code (class), i want to draw the FSM but i have problems with pyGraphviz.

I hope someone serves this little code

Update

Version 1.0.2
Attachments
Logika_v102.rar
(9.05 MiB) Downloaded 35 times
Logika.rar
(48.62 KiB) Downloaded 37 times
Last edited by badanni on Fri Feb 21, 2020 5:24 pm, edited 1 time in total.

User avatar
Moshibit
Regular
Posts: 50
Joined: Wed Oct 16, 2019 1:58 pm
Location: Mexico
Contact:

Re: FSM and Ren'Py(label)

#2 Post by Moshibit »

Nice, this is very useful. I had the idea that complex stories should be handled by a state machine, but there was no time to program a script to do it. Muy bien amigo.

prestristan
Regular
Posts: 25
Joined: Sat Apr 11, 2020 1:28 pm
Contact:

Re: FSM and Ren'Py(label)

#3 Post by prestristan »

Let me just say... Yeah I thought It was "Flying Spaghetti Monster"
but it turns out to be a useful tool! Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users