Some questions about FileAction

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
yoosir
Regular
Posts: 25
Joined: Thu Mar 09, 2017 10:09 am
Contact:

Some questions about FileAction

#1 Post by yoosir »

The FileAction's role is to save/load archive.

Code: Select all

  #the source code  of save/load archive  (part)
            ## The grid of file slots.
            grid gui.file_slot_cols gui.file_slot_rows:
                style_prefix "slot"

                xalign 0.5
                yalign 0.5

                spacing gui.slot_spacing

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

                    $ slot = i + 1

                    button:
                        action FileAction(slot)

                        has vbox

                        add FileScreenshot(slot) xalign 0.5

                        text FileTime(slot, format=_("{#file_time}%A, %B %d %Y, %H:%M"), empty=_("empty slot")):
                            style "slot_time_text"

                        text FileSaveName(slot):
                            style "slot_name_text"

                        key "save_delete" action FileDelete(slot)

I have a demand :
I want to save some data when i go to archive!(the data and the archive are one-to-one correspondence)
of course ,when I want to load the archive, I would like to load the data corresponding to this archive。

So, I need the name of archive,then let the name be the key of my data
I defined an customFileAction method instead of the FileAction method

Code: Select all

    # 存档读档 获取id
    def coustomFileAction(name, page=None, **kwargs):
        saveCustomAttributes(name)
        return FileAction(name,page,**kwargs)

    def saveCustomAttributes(name):
        # do something with name
        #slot_current_page = FileCurrentPage()
        layout.yesno_screen("-------- " + str(name), None,None)
To see to you, I can do something with the name in the saveCustomAttributes method

But, The question:

When I launch the game, the coustomFileAction method is called immediately

Why ??

yoosir
Regular
Posts: 25
Joined: Thu Mar 09, 2017 10:09 am
Contact:

Re: Some questions about FileAction

#2 Post by yoosir »

this is my mistake:
the source code of FileAction:

Code: Select all

    def FileAction(name, page=None, **kwargs):
        """
         :doc: file_action

         "Does the right thing" with the file. This means loading it if the
         load screen is showing (current screen is named "load"), and saving
         otherwise.

         `name`
             The name of the slot to save to or load from. If None, an unused slot
             (a large number based on the current time) will be used.

         `page`
             The page that the file will be saved to or loaded from. If None, the
             current page is used.

         Other keyword arguments are passed to FileLoad or FileSave.
         """

        if renpy.current_screen().screen_name[0] == "load":
            return FileLoad(name, page=page, **kwargs)
        else:
            return FileSave(name, page=page, **kwargs)

class FileLoad .......
class FileSave ......
so i should return a class :

Code: Select all


    # 存档读档 获取id
    def coustomFileAction(name):
        return AttributeSave(name)

    class AttributeSave(object):

        def __init__(self,name):
            self.name = name
            self.fileAction = FileAction(name)

        def __call__(self):
            #保存一个数据
            # TODO save my data with self.name
            
            self.fileAction()
            #layout.yesno_screen(str(self.name), None,None)

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Some questions about FileAction

#3 Post by gas »

Oh, finally I understand.

RENPY SAVE EVERYTHING, all the variables are saved.
But you can't read the values before the game is loaded.

DON'T CHANGE THE CORE SCRIPT or when updating Ren'py your changes will disappear.

Use JSON functions to store extra data inside an "archive" (please note: IS NOT ARCHIVE, is SAVE FILE).

1 - create a json callback that store your data

Code: Select all

init python:
    def jsondata(charadata): #charadata is the name of the dictionary you have to check in the slot screen
        #now register values into the dictionary
        charadata["points"]=score
    config.save_json_callbacks.append(jsondata) #append the function before each save action
2 - retrieve the data in the slot screen, like this:

Code: Select all

 if FileLoadable(i):
        $ slotdata=FileJson(i,"points") #retrieve your custom variables from the json
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

yoosir
Regular
Posts: 25
Joined: Thu Mar 09, 2017 10:09 am
Contact:

Re: Some questions about FileAction

#4 Post by yoosir »

gas wrote:Oh, finally I understand.

RENPY SAVE EVERYTHING, all the variables are saved.
But you can't read the values before the game is loaded.

DON'T CHANGE THE CORE SCRIPT or when updating Ren'py your changes will disappear.

Use JSON functions to store extra data inside an "archive" (please note: IS NOT ARCHIVE, is SAVE FILE).

1 - create a json callback that store your data

Code: Select all

init python:
    def jsondata(charadata): #charadata is the name of the dictionary you have to check in the slot screen
        #now register values into the dictionary
        charadata["points"]=score
    config.save_json_callbacks.append(jsondata) #append the function before each save action
2 - retrieve the data in the slot screen, like this:

Code: Select all

 if FileLoadable(i):
        $ slotdata=FileJson(i,"points") #retrieve your custom variables from the json

Now,I know that the variables are automatically saved when go to save action

But, What's "i" ??
Can you elaborate on it?,please !

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Some questions about FileAction

#5 Post by gas »

It was an iterator, in the slot screen.

Code: Select all

# Display ten file slots, numbered 1 - 10.
            for i in range(1, columns * rows + 1):

                # Each file slot is a button.
                button:
                    action FileAction(i)
                    xfill True
In python, you use an iterator in the for... cycles.
In this case, the iterator is "i" and go from 1 to columns * rows + 1.
Each step of the cycle, i increase by 1.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

yoosir
Regular
Posts: 25
Joined: Thu Mar 09, 2017 10:09 am
Contact:

Re: Some questions about FileAction

#6 Post by yoosir »

gas wrote:It was an iterator, in the slot screen.

Code: Select all

# Display ten file slots, numbered 1 - 10.
            for i in range(1, columns * rows + 1):

                # Each file slot is a button.
                button:
                    action FileAction(i)
                    xfill True
In python, you use an iterator in the for... cycles.
In this case, the iterator is "i" and go from 1 to columns * rows + 1.
Each step of the cycle, i increase by 1.
Thank you so much! :D

Post Reply

Who is online

Users browsing this forum: AdsBot [Google], Google [Bot], Ocelot, snotwurm