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
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"
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
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
...
...
Code: Select all
START "start scene name"
Code: Select all
IF "scene name" SEND "transition condition1" OR "transition condition2" GOTO "scene name2"
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"
I hope someone serves this little code
Update
Version 1.0.2
- Source code (Python 3) https://github.com/badanni/Logika
fix problem with lexer and token
graphviz for draw the FSM