Dictionary value found, then not found next time it's checked.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Squatnet

I am having an issue retrieving values from a dictionary.
This script works perfectly to populate some labels in my scene based on the values in shipyard When the scene is loaded it will get the dictionary values for shipyard[shipsList[showRoomNum]] in this case “Shuttle” and fill out the labels. When the player clicks the button _on_Next_pressed(): the “Corvette” is also correctly loaded.
However, the expected outcome after clicking it again is that it will once again go back to “Shuttle”. It does not, Although the showRoomNum and shipName variables are correctly updated.

var showRoomNum  # currently displayed ship 
var shipslist = ["Shuttle","Corvette"] # an array containing ships sold here 
 #The dictionary containing all the ships 
var shipyard = {"Shuttle":{"size":"small",
							"value":10000,
							"jumpdrive":{"lvl":1,"mod":"none","range":6},
							"fuel":{"lvl":1,"current":8,"capacity":8},
							"cargo":{"lvl":1,"current":0,"capacity":10,	"contents":{}}
	                                                },
				"Corvette":{"size":"medium",
							"value":100000,
							"jumpdrive":{"lvl":1,"mod":"none","range":10},
							"fuel":{"lvl":1,"current":16,"capacity":16},
							"cargo":{"lvl":1,"current":0,"capacity":48,"contents":{}}
							}
                              }
 func _ready():
	showRoomNum = 0 # always load on first ship
	updateShYrd(showRoomNum) 
func updateShYrd(val):
	var shipName = shipslist[val] # get the name of the ship
	var ship = shipyard[shipslist[showRoomNum]] # get the dictionary for a particular ship
	$"TabContainer/Sold Ships/Control/ShipModel".set_text(shipName)
	$"TabContainer/Sold Ships/Control/ShipPrice".set_text(str(ship.value))
	$"TabContainer/Sold Ships/Control/ShipCargo".set_text(ship.cargo.capacity) 
 # this function loads the next ship
func _on_Next_pressed():
	if showRoomNum < soldShipsNum:
		showRoomNum += 1
	else:
		showRoomNum = 0
	updateShYrd(showRoomNum)
:bust_in_silhouette: Reply From: hilfazer

By “correctly updated” you mean they have values lesser than 2?

Because if your soldShipsNum is equal to size of shipslist you need to contact Houston. Value of 2 or greater is not a legal index for shipslist array.
I tried your code with soldShipsNum set to 1 and it works fine.

I added print statements to the code.
1st load

showRoomNum = 0
shipName = "Shuttle"
ship = {"size":"small", "value":10000, "jumpdrive":{"lvl":1,"mod":"none","range":6}, "fuel":{"lvl":1,"current":8,"capacity":8},"cargo":{"lvl":1,"current":0,"capacity":10, "contents":{}}}

2nd load

showRoomNum = 1
shipName = "Corvette"
ship = {"size":"medium","value":100000,"jumpdrive":{"lvl":1,"mod":"none","range":10},"fuel":{"lvl":1,"current":16,"capacity":16},"cargo":{"lvl":1,"current":0,"capacity":48,"contents":{}}}

3rd load ( should be shuttle)

showRoomNum = 0
shipName = "Shuttle"
ship = {}

So the value of shipyard is being lost. why?

Squatnet | 2018-03-28 15:36