Attention | Topic was automatically imported from the old Question2Answer platform. | |
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)