(I'm using godot 3.5.1)
I was trying to make a temporary save system to save the initial state of a level so when the character dies it goes back to initial state of the level.

var temporarylist = null

func tempsave():
	temporarylist = [generallist,emotionlist,inventorylist,inventorystak,homelist,shoplist,coinlist,coin]

func loadsave():
	generallist = temporarylist[0]
	emotionlist = temporarylist[1]
	inventorylist = temporarylist[2]
	inventorystak = temporarylist[3]
	homelist = temporarylist[4]
	shoplist = temporarylist[5]
	coinlist = temporarylist[6]
	coin = temporarylist[7]

So i created the variable temporary list and made 2 functions, the tempsave function saves all the data and puts it into the temporarylist.
The loadsave function takes the temporarylist data and takes it to the temporarylist. The problem here is that for some reason the temporarylist data updates to the current state of the data, making the saving useless. The temporarysave function is called at the start of each level meanwhile the loadsave function is called every time the player clicks retry in the gameover menu. I checked if the temporarysave function is called multiple times using print but it was called only when intended to. I tried changing the name of the temporarylist variable to see if it was called in any other part of my code but it wasn't. (by the way this is a global script if that makes any difference)

  • xyz replied to this.
    9 days later

    xyz
    this is what's inside the lists but that's not really important

    ``#0
    var generallist = [40,40,0]
    #general index 0 health
    #general index 1 healthmax
    #general index 2 coins
    
    #1
    var emotionlist = [1,0]
    var alreadyplayed = false
    #emotion index 0 emotion
    
    var candialogue = false
    #2
    var inventorylist = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    #3
    var inventorystak = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    #inventory index = slot -1
    #4
    var inventorygeneral = [false,true]
    #inventorygeneral index 0 menuopen
    #inventorygeneral index 1 canopen``

    these are not all of the lists but the data inside is not really the problem

    A possible issue is that you're creating a reference to an Array, rather than creating a copy of the Array,

    var a: Array = [1, 2, 3]
    var b: Array = a
    var c: Array = a.duplicate()

    a and b are the same Array. Changing either one changes both.
    c is initialized to be the same as a but it's a separate Array. Changing one has no effect on the other.

    That can be verified by running this code:

    print_debug(a,b,c)
    a[0] = 100 # changes a and b
    print_debug(a,b,c)
    b[0] = 200 # changes a and b
    print_debug(a,b,c)
    c[0] = 300 # changes only c
    print_debug(a,b,c)