Saving and Loading Information Stops Working?

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
Draite
Newbie
Posts: 23
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Saving and Loading Information Stops Working?

#1 Post by Draite » Wed Feb 12, 2020 1:14 am

Currently I'm working on some RPG elements, including an overworld map which lets the player travel between different locations. However I've run into an issue where after saving the game and loading it the location and map system completely breaks down.

The way it's supposed to work is I have a Locations class which contains information about the area, a list of random encounters that can occur, if the area has been discovered, etc.

I have a default variable map_point that contains the current location and gets updated whenever the player travels.

I have the map_point display the current location name on the screen. This continues to display the correct location on load, however when trying to travel everything just breaks, but only breaks if the game was loaded. If you never save and load the map runs fine.

Is there anyway to force variables to save and load correctly? Or is there a better way to be handling this sort of a system?
I know this is extremely vague but if anyone has any suggestions or has done a travel system and could direct me to it I'd really appreciate it.

Code: Select all

class Location:
        def __init__(self,img,bg_day,bg_night,x,y,name,_scene,scav,explore,encounter,hostility,discovered):
            # Image is the map icon used
            # bg is the background image to use while in this location
            # Name is a string containing the location name
            # scene is a string containing the default scene label for this location
            # Scav is a list of lists containing potential encounters and the chance of that encounter that can be discovered while scavanging
            # Explore is a list of potential lists containing encounters found while exploring as well as a list of potential connections that can be discovered, each with a weight associated with them
                ## Each Scavenge or Explore is made of a list (Encounter_scene, Encounter_chance, Weather, Season, Hour_begin, Hour_end)
            # Encounter contains a list of hostile encounters that can be encountered
                # Each encounter contains the Mob,
            # Connections is a list of tuples containing other locations that directly connect to this location and the time required to travel
                ## Connections (location,hours,minutes)
            # Discovered is a boolean that determines if the location has been discovered or not
            self.img = img
            self.bg_day = bg_day
            self.bg_night = bg_night
            self.x = x
            self.y = y
            self.name = name
            self._scene = _scene
            self.scavenge = scav
            self.explore = explore
            self.encounter = encounter
            self.hostility = hostility
            self.discovered = discovered

Code: Select all

def travel(_start,_destination):
        _path = None
        for _r in roads:
            if (_r.locale_1 == _start or _r.locale_2 == _start) and (_r.locale_1 == _destination or _r.locale_2 == _destination):
                _path = _r

        store.map_point = _destination
        store.selected_locale = None

        # Check for encounters
        # Update time
        _t = renpy.random.randint(_path.tt_minutes - _path.tt_var,_path.tt_minutes + _path.tt_var)
        add_time(0,_t)

        renpy.jump(map_point._scene)
        # Reset Variables

Map Screen, displays locations and roads available. Clicking on a location highlights it and clicking again triggers the travel function

Code: Select all

screen map():
    modal True
    zorder 50
    add "gui/rooms/worldmap.jpg"

    # Menubar
    fixed:
        pos (909,8)
        xysize (355,80)
        imagebutton auto "gui/button/exitbutton_%s.png" action [Hide("map"),Show("menubar")] area (279,0,80,80) anchor (0.5,0.5) focus_mask "gui/button/button_mask.png"

    # Map Details
    for _i in range(len(roads)):
        if roads[_i].discovered and roads[_i].locale_1.discovered and roads[_i].locale_2.discovered:
            $ _dis_a = roads[_i].locale_1.x - roads[_i].locale_2.x
            if _dis_a < 0:
                $ _dis_a *= -1
            $ _dis_b = roads[_i].locale_1.y - roads[_i].locale_2.y
            if _dis_b < 0:
                $ _dis_b *= -1

            $ _points = int(math.sqrt((_dis_a * _dis_a) + (_dis_b * _dis_b))/12)

            # Draw Paths from current location
            for _o in range(_points):
                if (roads[_i].locale_1 == map_point or roads[_i].locale_2 == map_point) and (roads[_i].locale_1 == selected_locale or roads[_i].locale_2 == selected_locale):
                    add "gui/misc/path_highlight.png":
                        xpos roads[_i].locale_1.x + (((roads[_i].locale_2.x - roads[_i].locale_1.x)/_points) * _o)
                        ypos roads[_i].locale_1.y + (((roads[_i].locale_2.y - roads[_i].locale_1.y)/_points) * _o)
                        anchor (0.5,0.5)
                elif roads[_i].locale_1 == map_point or roads[_i].locale_2 == map_point:
                    add "gui/misc/path_visible.png":
                        xpos roads[_i].locale_1.x + (((roads[_i].locale_2.x - roads[_i].locale_1.x)/_points) * _o)
                        ypos roads[_i].locale_1.y + (((roads[_i].locale_2.y - roads[_i].locale_1.y)/_points) * _o)
                        anchor (0.5,0.5)
                else:
                    add "gui/misc/path_hidden.png":
                        xpos roads[_i].locale_1.x + (((roads[_i].locale_2.x - roads[_i].locale_1.x)/_points) * _o)
                        ypos roads[_i].locale_1.y + (((roads[_i].locale_2.y - roads[_i].locale_1.y)/_points) * _o)
                        anchor (0.5,0.5)

    for _i in range(len(locations)):
        # Draw map icons that are not the current position of the player
        if locations[_i].discovered:
            if map_point != locations[_i]:
                # Draw map icons outside movement range
                $ _loc = locations[_i]
                if not map_point.is_connected(_loc):
                    add _loc.img + "grey.png" pos (_loc.x,_loc.y) anchor (0.5,0.5)
                else:
                    if selected_locale == _loc:
                        imagebutton idle _loc.img + "color.png" pos (_loc.x,_loc.y) anchor (0.5,0.5) action [Hide("map"),Show("menubar"),Function(travel,map_point,selected_locale)] focus_mask "gui/button/map_mask.png"
                    else:
                        imagebutton idle _loc.img + "white.png" hover _loc.img + "color.png" pos (_loc.x,_loc.y) anchor (0.5,0.5) action [SetVariable("selected_locale",_loc)] focus_mask "gui/button/map_mask.png"
            else:
                    add map_point.img + "color.png" pos (map_point.x,map_point.y) anchor (0.5,0.5)

    if selected_locale != None:
        for _i in range(len(roads)):
            if roads[_i].discovered and (roads[_i].locale_1 == map_point or roads[_i].locale_2 == map_point) and (roads[_i].locale_1 == selected_locale or roads[_i].locale_2 == selected_locale):
                $ _mrk_x = (map_point.x + selected_locale.x) / 2
                $ _mrk_y = (map_point.y + selected_locale.y) / 2
                add "gui/button/map_marker.png" pos (_mrk_x,_mrk_y) anchor (0.5,1.0)
                text str(roads[_i].tt_minutes) + " minutes" pos (_mrk_x,_mrk_y - 40) size 14 color "#000" anchor (0.5,1.0)
    else:
        add "gui/button/map_marker.png" pos (map_point.x,map_point.y) anchor (0.5,1.0)

User avatar
RicharDann
Veteran
Posts: 264
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Saving and Loading Information Stops Working?

#2 Post by RicharDann » Thu Feb 13, 2020 4:46 pm

Most of the time (in my experience), save and loading problems have to do with the way you define your variables. It's very hard to know for sure just by looking at the code you posted.
How did you define your map_point variable? For a system like this it probably should be initialized with default statement:

Code: Select all

default map_point = None

Same with the Locations, and other variables that will be used by more than one function or screen.
If this isn't the cause, can you tell us how exactly does the game "breaks"? Do you get an error message, or does it run but doesn't behave as it should? If you get an error it would be helpful to post it, most of the time error messages point exactly towards the part of the code causing the problem.
The most important step is always the next one.

Draite
Newbie
Posts: 23
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Saving and Loading Information Stops Working?

#3 Post by Draite » Thu Feb 13, 2020 7:13 pm

Locations are defined like this

Code: Select all

define south_forest_02 = Location("gui/button/map_locale_generic_","bg forest_day","bg forest_night",924,532,"Southern Forest 02","scene_forest.south_02",
[["scav_forest.big_stick",1,"any","any",0,24],["scav_forest.flowers",3,"any","any",0,24],["scav_forest.scuffle",1,"any","any",0,24],["scav_forest.berries",2,"any","any",0,24],["scav_forest.old_camp",1,"any","any",0,24],["scav_forest.nothing",2,"any","any",0,24]],
[],
[["battle_forest.goblin_basic",5,"any","any",0,24],["battle_forest.slime_basic",1,"any","any",0,24],["battle_forest.werewolf_basic",3,"any","any",0,24]],
6,True)
Map point is defined like this

Code: Select all

default map_point = None
default selected_locale = None
I also keep a list of map locations and roads to edit things as the game goes on

Code: Select all

$ locations = []
    $ roads = []
    # SET LOCATIONS LIST
    python:
        # Forest Locations
        locations.append(south_forest_01)
        locations.append(south_forest_02)
        locations.append(south_forest_03)
        # Coastal Locations
        locations.append(south_coast_01)
        locations.append(south_coast_02)
        # Unique Locations
        locations.append(fort_chastity)
        locations.append(the_last_drop)
        locations.append(vergesseneburg)
        locations.append(goblin_outpost_01)
    # SET ROADS LIST
    python:
        roads.append(forest_to_fort_chastity)
        roads.append(forest_path_01)
        roads.append(forest_path_03)
        roads.append(forest_path_04)
        roads.append(forest_path_05)
        roads.append(last_drop_path_01)
        roads.append(last_drop_path_02)
        roads.append(beach_path_01)
        roads.append(path_vergesseneburg_to_beach)
The way it breaks is really strange. The map_point correctly prints out the current location name to the screen. However when I enter the map screen the map seems to no longer agree on the current location. It blacks out all the other areas and doesn't mark the current location. This cause you to be unable to travel. It still renders the locations but they're all 'grey' and unconnected.

Map Rendering Code

Code: Select all

for _i in range(len(locations)):
        # Draw map icons that are not the current position of the player
        if locations[_i].discovered:
            if map_point != locations[_i]:
                # Draw map icons outside movement range
                $ _loc = locations[_i]
                if not map_point.is_connected(_loc):
                    add _loc.img + "grey.png" pos (_loc.x,_loc.y) anchor (0.5,0.5)
                else:
                    if selected_locale == _loc:
                        imagebutton idle _loc.img + "color.png" pos (_loc.x,_loc.y) anchor (0.5,0.5) action [Hide("map"),Show("menubar"),Function(travel,map_point,selected_locale)] focus_mask "gui/button/map_mask.png"
                    else:
                        imagebutton idle _loc.img + "white.png" hover _loc.img + "color.png" pos (_loc.x,_loc.y) anchor (0.5,0.5) action [SetVariable("selected_locale",_loc)] focus_mask "gui/button/map_mask.png"
            else:
                    add map_point.img + "color.png" pos (map_point.x,map_point.y) anchor (0.5,0.5)
Thank you for looking into this for me! I really appreciate it

User avatar
RicharDann
Veteran
Posts: 264
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Saving and Loading Information Stops Working?

#4 Post by RicharDann » Fri Feb 14, 2020 9:58 am

The way it breaks is really strange. The map_point correctly prints out the current location name to the screen. However when I enter the map screen the map seems to no longer agree on the current location. It blacks out all the other areas and doesn't mark the current location. This cause you to be unable to travel. It still renders the locations but they're all 'grey' and unconnected.
The code itself seems fine at a glance, but the way you describe it, it sounds like the map screen is failing to read the locations list correctly, failing to see the discovered locations as such. Not sure what the exact reason could be as right now I'm not able to make a test project to try and replicate the problem, but here are a few changes you can try:
1) Define your location objects with default instead of define and outside of a label:

Code: Select all

default south_forest_01 = Location() #ommitting params here to save space
2) Also default the locations and road variables, outside of a label:

Code: Select all

default locations = []
default roads = []
3) When you need to update the list of locations and roads within a label, do so with the $ statement instead of python statement:

Code: Select all

# Forest Locations
$ locations.append(south_forest_01)
4) Use global in your travel function, instead of store.var:

Code: Select all

def travel(_start,_destination):
        global map_point, selected_locale

        map_point = _destination
        selected_locale = None
To be safe you can also force recompile, delete persistent in the launcher, start a new game, save, close the game, then load and test.
The most important step is always the next one.

Draite
Newbie
Posts: 23
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Saving and Loading Information Stops Working?

#5 Post by Draite » Mon Feb 24, 2020 11:38 pm

Thank you so much. I think this has fixed it. *Fingers crossed*

Post Reply

Who is online

Users browsing this forum: frankfrankstud