Author |
Message |
Timothy Willard
|
Posted: Sun Apr 21, 2013 9:47 pm Post subject: Any way to check all variables in a specific class? |
|
|
What is it you are trying to achieve?
My friend and I are trying to create an inventory system for a game, and we have run into a problem with adding items to the inventory.
What is the problem you are having?
Currently, all of the items we have in the inventory are instances of the class "Item," while the inventory itself is an array with 10 slots. To add an item, the program gets an input from the user, as a string, and we am forced to hard code in every item we have to add it to the inventory.
We managed to eliminate the hard coding for removing items from the inventory (proc removeItem), but we can't manage to do the same for adding items.
Describe what you have tried to solve this problem
We considered creating a loop that checks every variable in the class "Item" for its "name"(a variable inside the "Item") (because the string the user inputs can't be compared to the instance) but we do not know how to create a list of those variables and check them all without hardcoding in each Item, which is what we are trying to avoid.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
http://pastebin.com/BHGpXLnr
(the large sections commented out are the points where things were hardcoded. It is proc addItem where the problem is.)
Please specify what version of Turing you are using
Turing Version 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jr5000pwp
|
Posted: Mon Apr 22, 2013 7:45 am Post subject: RE:Any way to check all variables in a specific class? |
|
|
A string name might not be the best identifier for your situation. Consider using an integer which happens to have the wonderful benefit of doubling as an array index. |
|
|
|
|
|
Raknarg
|
Posted: Mon Apr 22, 2013 6:32 pm Post subject: RE:Any way to check all variables in a specific class? |
|
|
Have every item with an ID, so you can numerically search through the item types.
In fact, you could still use a string if you wanted to. Just have a list of all items possible (probably as a text file). |
|
|
|
|
|
Kyle Biro
|
Posted: Mon Apr 22, 2013 8:30 pm Post subject: RE:Any way to check all variables in a specific class? |
|
|
But that's where things go wrong. We can't actually figure out how to specifically search, say, "damage" in:
var woodSword : ^Item
new woodSword
Item (woodSword).name := "wooden sword"
Item (woodSword).description := "A weak sword made of wood."
Item (woodSword).damage := 3 |
|
|
|
|
|
Raknarg
|
Posted: Mon Apr 22, 2013 8:42 pm Post subject: RE:Any way to check all variables in a specific class? |
|
|
Well, you could create an array that contains all the information of your items. That way you could use a for loop and look at each name that way. |
|
|
|
|
|
Raknarg
|
Posted: Mon Apr 22, 2013 8:44 pm Post subject: RE:Any way to check all variables in a specific class? |
|
|
Waait I think I misunderstood. You mean you want to access the variable damage by entering damage by text? |
|
|
|
|
|
Kyle Biro
|
Posted: Fri Apr 26, 2013 2:01 am Post subject: RE:Any way to check all variables in a specific class? |
|
|
Well...
Say you typed "take wooden sword". We want to check to see if "wooden sword" is stored in "name", "description", or "damage" in woodSword or steelSword, and if so, put said item in the inventory. |
|
|
|
|
|
Insectoid
|
Posted: Fri Apr 26, 2013 6:18 am Post subject: RE:Any way to check all variables in a specific class? |
|
|
Why would you want to check damage or description? Would you ever type 'take a weak sword made of wood'? No. If the player types 'take', then what follows should always be a name. There is no need to look at the other variables. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Fri Apr 26, 2013 7:29 am Post subject: RE:Any way to check all variables in a specific class? |
|
|
even so, if you really wanted to o it your way, you'd have to use a bunch of if statements, I don't think there's a way around that |
|
|
|
|
|
|