Ren'Py memo

Ren'Pyの個人的なメモ

セーブロック機能を付ける

出典元 
Making a "Lock Save" action - Lemma Soft Forums

1.pythonを書く

options.rpy

init python:
(省略)

    if persistent.locked is None:
        persistent.locked = []

    def lockSave(slotname):
        persistent.locked.append(slotname)
        if persistent.locked is None:
            persistent.locked = []

    def unlockSave(slotname):
        persistent.locked.remove(slotname)
        if persistent.locked is None:
            persistent.locked = []

一応options.rpyと書きましたが、init python:内であればどこでも良いと思います。
セーブスロットに付いてる番号にひもづけたlock用リストを作ってる感じです(?)
def~はそのlock用リストのえんぴつと消しゴムをつけた感じです。

2.screenに記述する

screens.rpy

screen file_slots(title):

    use game_menu(title):

        fixed:

            order_reverse True

            grid gui.file_slot_cols gui.file_slot_rows:
                style_prefix "slot"

                xalign 0.5
                yalign 0.3

                spacing gui.slot_spacing

                for i in range(gui.file_slot_cols * gui.file_slot_rows):

                    $ slot = i + 1
                    $ file_name = FileSlotName(slot, gui.file_slot_cols * gui.file_slot_rows)
                    $ file_shot = FileScreenshot(slot,empty=_("Empty Slot."))

                    button:

#セーブロックに関係ない箇所#
                        text FileTime(slot, format=_("{#file_time}%Y/%B/%d %H:%M"), empty=(""))
#セーブロックに関係ない箇所#

                        if title == "LOAD":
                            action FileLoad(slot, confirm=persistent.ask_load)
                        else:
                            if file_name in persistent.locked:
                                action None
                            else:
                                action FileSave(slot, confirm=persistent.ask_save)
                        add FileScreenshot(slot) xalign 0.5

                        if file_name in persistent.locked:
                            if file_shot != "Empty Slot.":
                                imagebutton:
                                    idle "images/lock_close.png"
                                    hover "images/lock_open.png"
                                    action [Function(unlockSave, file_name)]
                                    alt _("セーブろっくぼたん")
                        else:
                            if file_shot != "Empty Slot.":
                                imagebutton:
                                    idle "images/lock_open.png"
                                    hover "images/lock_close.png"
                                    action [Function(lockSave, file_name)]
                                    alt _("セーブろっくぼたん")

                        if file_name not in persistent.locked:
                            key "K_DELETE" action FileDelete(slot, confirm=persistent.ask_del)

#セーブロックに関係ない箇所#
                        text FileSaveName(slot)
#セーブロックに関係ない箇所#

                        if file_shot != "Empty Slot.":
                            imagebutton:
                                if file_name in persistent.locked:
                                    action None
                                    alt _("セーブさくじょぼたんろっくちゅう")
                                else:
                                    action FileDelete(slot, confirm=persistent.ask_del)
                                    alt _("セーブさくじょぼたん")
                                idle "images/g9.png"
                                hover "images/g8.png"
                                insensitive "images/g7.png"
(省略)

ほぼ全部載せたらよくわからなくなりましたね(位置やスタイルはやや省略しました)
削除ボタンとか混沌レイアウトとかが混ざりすぎで私もよくわからないです。
あと例文をまんま参照してるのでサンプルレイアウトとは食い違ってもう大変。


とりあえずセーブロック関係のところだけ抜き出し。

if title == "LOAD":
 action FileLoad(slot, confirm=persistent.ask_load)
else:
 if file_name in persistent.locked:
   action None
 else:
   action FileSave(slot, confirm=persistent.ask_save)

もしロード画面だったらボタンにロードの機能をつける。
それ以外で、ロック中のボタンだったら何もしない。
それ以外でロックもされてなかったらボタンにセーブの機能をつける。

if file_name in persistent.locked:
   if file_shot != "Empty Slot.":
      imagebutton:
       idle "images/lock_close.png"
       hover "images/lock_open.png"
       action [Function(unlockSave, file_name)]
else:
   if file_shot != "Empty Slot.":
      imagebutton:
       idle "images/lock_close.png"
       hover "images/lock_open.png"
       action [Function(lockSave, file_name)]

ロックしている時にロックボタンを押したらロック解除。
ロックされてない時にロックボタンを押したらロック。

if file_name not in persistent.locked:
  key "K_DELETE" action FileDelete(slot, confirm=persistent.ask_del)

ロックしている時に削除キーが作動しないようにする命令。
(Ren'Pyはセーブデータを選択、hoverしてる時にdelキーを押すとセーブを消せる)

persistent.ask_delは別の機能(削除時にダイアログ表示するか否か)で
使ってるやつなのでセーブロックの機能にはまったく関係ないです。
というかpersistent.locked以外のpersistentは全部関係ないです。

if file_shot != "Empty Slot.":
#ファイルが空ではなかった場合、削除ボタンを表示。
 imagebutton:
#スタイルの中でもif文は可能。
    if file_name in persistent.locked:
     action None
     alt _("セーブさくじょぼたんろっくちゅう")
    else:
     action FileDelete(slot, confirm=persistent.ask_del)
     alt _("セーブさくじょぼたん")

ロック中は削除ボタンが反応しないようにする。

3.完成図

説明しようと思ったけど理解があやふやで全く説明できませんでした。
とりあえずこれで動いてます。