Hello there, I decided to dabble around with Swift, and to try and get the hang of things I am trying to create an RPG that is based upon a C++ tutorial found here, and I have gotten most things to work, but I am having some problems implementing menus for players to see stats and their inventories.
While a player is in the middle of an area, they have a list of options, and, currently a hidden option to bring up a menu if they press 0, and that menu does appear.
However, if a player enters the number to just view items in their inventory, as opposed to weapons, armor, or the inventory, as whole, nothing shows up, not even a statement that there is nothing there.
When I tried having the program print out the array size, it did not show up, which suggests that the function itself is not running, because the code to display the whole inventory or just weapons or just armor works fine, by telling me that they are empty, is not really that different from what is giving me grief.
Here is the code for the player menu, which calls the troublesome function:
// create function for menu player can access any time
func playerMenu() {
var playerDialog: Int!
var playerChoices: [String] = ["Views Items", "View Weapons", "View Armor", "View All Items", "View Stats", "Save"]
repeat {
playerDialog = Dialogue(report: "What would you like to do (Enter '0' to exit)? ", choices: playerChoices).activate()
if (playerDialog > 0 && playerChoices[playerDialog-1] == "View Items") {
self.viewItems()
} else if (playerDialog > 0 && playerChoices[playerDialog-1] == "View Weapons") {
self.viewWeapons()
} else if (playerDialog > 0 && playerChoices[playerDialog-1] == "View Armor") {
self.viewArmor()
} else if (playerDialog > 0 && playerChoices[playerDialog-1] == "View All Items") {
self.viewAllItems()
} else if (playerDialog > 0 && playerChoices[playerDialog-1] == "View Stats") {
self.viewStats()
} else if (playerDialog > 0 && playerChoices[playerDialog-1] == "Save") {
if (self.savePlayer(player: player)) {
print("Player data saved successfully!")
} else {
print("Soory, player could not be saved.")
}
}
} while (playerDialog != 0)
return // return nothing, so that game can resume
} // end function
Here is the code for the troublesome function:
// function to view items in inventory
func viewItems() {
var itemDialog: Int!
var itemList: [String:Int] = [:]
var itemOptions: [String] = [String]()
if (self.player.inventory.items.count > 0) {
for item in self.player.inventory.items {
let keyExists = itemList[item.name] != nil
if (!keyExists) {
itemList[item.name] = 1
} else {
itemList[item.name] = itemList[item.name]!+1
}
}
var display = "Item Inventory: \r\n\r\n" // create vaiable to display items
// add dictionary items to string variable for output
for (item, total) in itemList {
display = display + item + ": " + String(total) + "\r\n\r\nDescription: " + self.player.inventory.items[self.player.inventory.items.index(where: {$0.name.caseInsensitiveCompare(item) == .orderedSame})!].report
itemOptions.append(item)
}
print(display)
repeat {
itemDialog = Dialogue(report: "What item would you like to use (Enter '0' to go to main menu)?", choices: itemOptions).activate()
if (itemDialog > 0 ) {
let result = self.player.useItem(item: self.player.inventory.items[self.player.inventory.items.index(where: {$0.name.caseInsensitiveCompare(itemOptions[itemDialog-1]) == .orderedSame})!])
if (result != 0){
print(self.player.name + "'s health has been restored by " + String(result) + "points.")
} else {
print("item did nothing.")
}
}
} while (itemDialog != 0)
} else {
print(self.player.name + " does not have anything in their items inventory.")
}
} // end function
And just so you guys know how similar the other stuff is, here is the code for viewing weapons, armor, and the whole inventory
Weapons:
// function to view weapons in inventory
func viewWeapons() {
var itemDialog: Int!
var itemList: [String:Int] = [:]
var itemOptions: [String] = [String]()
if (self.player.inventory.weapons.count > 0) {
for weapon in self.player.inventory.weapons {
let keyExists = itemList[weapon.name] != nil
if (!keyExists) {
itemList[weapon.name] = 1
} else {
itemList[weapon.name] = itemList[weapon.name]!+1
}
}
var display = "Weapon Inventory: \r\n\r\n" // create vaiable to display items
// add dictionary items to string variable for output
for (weapon, total) in itemList {
display = display + weapon + ": " + String(total) + "\r\n\r\nDescription: " + self.player.inventory.weapons[self.player.inventory.weapons.index(where: {$0.name.caseInsensitiveCompare(weapon) == .orderedSame})!].report + "\r\n\r\nAttack: " + String(self.player.inventory.weapons[self.player.inventory.weapons.index(where: {$0.name.caseInsensitiveCompare(weapon) == .orderedSame})!].damage)
itemOptions.append(weapon)
}
print(display)
repeat {
itemDialog = Dialogue(report: "What Weapon would you like to equip (Enter '0' to go to main menu)?", choices: itemOptions).activate()
if (itemDialog > 0 ) {
self.player.equipWeapon(weapon: self.player.inventory.weapons[self.player.inventory.weapons.index(where: {$0.name.caseInsensitiveCompare(itemOptions[itemDialog-1]) == .orderedSame})!])
print("player has equipped " + itemOptions[itemDialog-1])
}
} while (itemDialog != 0)
} else {
print(self.player.name + " does not have anything in their weapons inventory.")
}
} // end function
Armor:
// function to view armor in inventory
func viewArmor() {
var itemDialog: Int!
var itemList: [String:Int] = [:]
var itemOptions: [String] = [String]()
if (self.player.inventory.armor.count > 0) {
for armor in self.player.inventory.armor {
let keyExists = itemList[armor.name] != nil
if (!keyExists) {
itemList[armor.name] = 1
} else {
itemList[armor.name] = itemList[armor.name]!+1
}
}
var display = "Armor Inventory: \r\n\r\n" // create vaiable to display items
// add dictionary items to string variable for output
for (armor, total) in itemList {
display = display + armor + ": " + String(total) + "\r\n\r\nDescription: " + self.player.inventory.armor[self.player.inventory.armor.index(where: {$0.name.caseInsensitiveCompare(armor) == .orderedSame})!].report + "\r\n\r\nDefense: " + String(self.player.inventory.armor[self.player.inventory.armor.index(where: {$0.name.caseInsensitiveCompare(armor) == .orderedSame})!].defense) + "\r\n\r\nSlot: " + String(self.player.inventory.armor[self.player.inventory.armor.index(where: {$0.name.caseInsensitiveCompare(armor) == .orderedSame})!].slot)
itemOptions.append(armor)
}
print(display)
repeat {
itemDialog = Dialogue(report: "What Weapon would you like to equip (Enter '0' to go to main menu)?", choices: itemOptions).activate()
self.player.equipArmor(armor: self.player.inventory.armor[self.player.inventory.armor.index(where: {$0.name.caseInsensitiveCompare(itemOptions[itemDialog-1]) == .orderedSame})!])
print("player has equipped " + itemOptions[itemDialog-1])
} while (itemDialog != 0)
} else {
print(self.player.name + " does not have anything in their armor inventory.")
}
} // end function
Whole inventory:
// function to view all items in inventory
func viewAllItems() {
var itemDialog: Int!
var itemList: [String:Int] = [:]
if (self.player.inventory.all().count > 0) {
for item in self.player.inventory.all() {
let keyExists = itemList[item.name] != nil
if (!keyExists) {
itemList[item.name] = 1
} else {
itemList[item.name] = itemList[item.name]!+1
}
}
var display = "Inventory: \r\n\r\n" // create vaiable to display items
// add dictionary items to string variable for output
for (item, total) in itemList {
display = display + item + ": " + String(total) + "\r\n\r\nDescription: " + self.player.inventory.all()[self.player.inventory.all().index(where: {$0.name.caseInsensitiveCompare(item) == .orderedSame})!].report + "\r\n\r\n"
}
print(display)
repeat {
itemDialog = Dialogue(report: "Enter '0' when you are done looking at inventory:", choices: []).activate()
} while (itemDialog != 0)
} else {
print(self.player.name + " does not have anything in their inventory.")
}
} // end function
With these functions being so similar, I am not really getting why the one to view the items will not run at all, while the others do what they are supposed to. Anyone have an idea of what is going on?
Also, as I was not too sure if you guys would need anything else, though Iam pretty sure the problem only resides in the class containing all these functions, I was going to upload the gzipped tar file with the JSON files and all Swift classes, but I could not do that, so I will probably need to put them up on Github, if you need any more than this.