Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-29-2011, 08:09 PM   #1 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Smile How would I store text and image paths for items in an inventory?

The best way to describe it would be like an RPG. I want to assign a tag to a spot in the inventory. Lets say its a sword I would like to assign that spot's icon to dataStorageFile.sword.icon and then when I tap it, a view comes up and the label's text is assigned to dataStorageFile.sword.description and then the price (let's say in a shop) set to dataStorageFile.sword.price and maybe I want it to require a level so on tap I want it to do

if(playerLevel > 5 && playerMoney > 60) {
playerMoney = playerMoney - 10;
[playerInventoryArray addObject:sword];
}

This is all jibberish of course just to get my thought across, but how would I store icon paths and description text and pull it out for use in code. While we're here, how would I store something besides text?
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 08:36 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, going strictly off the code you've posted, I don't think you're approaching it in the right direction.

If the player has not leveled high enough, or if they do not have enough money, why are they able to tap the button the first place? I guess it depends on what UI you are going for, but if you're not going to disable the button, then you're going to have to go with alerts or something to explain why something couldn't happen.

But let's imagine a simple table view listing out all of these items for sale. You're going to have an array of objects to populate this table. Not sure what your thinking is at this point, but you really should be making a custom model class for stuff like this. For lack of a better term, I'll call it an InventoryItem.

You don't want to hard-code the price like you have shown here, unless everything you could possibly buy would be the same price. I'd say the same thing for the skill level. So, things like this should be properties of your InventoryItem model. Ex:

InventoryItem
--- NSString name
--- int minimumSkillLevel
--- int (or float, depending) price

How far beyond this you go depends on what you need. You could either create separate models for each type you might have - Weapon, Food, Armor, etc - or you could add another property:

--- int classification

Here you'd have to keep track of some list of constants or something. Weapon = 1, Food = 2, and so on.

Then you'd have a method somewhere to build all of your standard items.

Code:
InventoryItem *item = [[InventoryItem alloc] init];
[item setMinimumSkillLevel:5];
[item setPrice:10];
[item setClassification:kWeaponSword];
[item setName:@"Sword"];

[[self masterInventoryList] addObject:item];

[item release], item = nil;
So you throw all of these into an array, and put those into the table. But can the user buy them?

- (BOOL)isAvailableForSale

Somehow this method would need to be able to access the player's data, so you could either pass in the player object, or get access to it via other means. Compare price to available funds, current skill level against the minimum required, and so on. Now you know whether to enable the button or not.

Your code would then change to something more along these lines.

Code:
InventoryItem *item = [[self masterInventoryList] objectAtIndex:row];

playerMoney = playerMoney - [item price];
[playerInventoryArray addObject:item];
...possibly add this depending on how you want to answer the UI question above:

Code:
if (playerLevel > [item minimumSkillLevel]) ...
Add whatever properties to the model object that you need. File paths to images, etc.

Saving gets a little more complicated with a custom object, but it's not too bad. You can investigate NSCoding, or you can create a method that will convert your custom model into a dictionary, at which point you can save it however you want.

Oh yeah, and you should have a Player model object, too.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-29-2011, 08:55 PM   #3 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

Omg man, it amazes me how helpful you are. To think you wrote all of that for me. This could probably be considered a guide. This is all great stuff and yeah this is a much better approach to things.

I'm going to pull out my "card" again though, and ask how would I create a class?
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 09:13 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Model objects, typically, will be even simpler than what you are already doing with view controllers. You have a .h and a .m, properties, methods, etc.

So I'm guessing you've already done a bunch of view controller subclasses. This will be the same thing, except that you will subclass NSObject directly (this is one of the options when you create a new file)

Code:
@interface InventoryItem : NSObject
The rest is exactly the same as view controllers, just with fewer stubbed-in methods in the template. Add properties. Add methods. Make it be as smart or as stupid as you want. It could be nothing but properties. It could be properties and 500 methods. Depending on the situation, mine are often just properties, plus a couple of methods that convert dictionary<->custom model. And I'll typically customize the -description method to report on the contents of all of the properties.

Even if you only need 1 or 2 properties, you should strongly consider just starting out with custom models (vs dictionaries). Dictionaries are harder to document, and harder to troubleshoot, particularly when it comes to leaks. Aw crap, I'm leaking a dictionary. Which one? Where? Who knows? Vs: Aw crap, I'm leaking an InventoryItem object. Ah-ha, that narrows down where I need to look.

Plus, the code is so much more readable.

Code:
InventoryItem *item = [[self theArray] objectAtIndex:1];

[[cell textLabel] setText:[item name]];
vs.

Code:
NSDictionary *item = [[self theArray] objectAtIndex:1];

[[cell textLabel] setText:[item objectForKey:@"name"]];
You have to keep track of keys, you get no code-completion help when typing in the strings (so you really should use constants), etc. Ick.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
data, description, icon, image, store

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 352
15 members and 337 guests
7twenty7, blasterbr, Clouds, dre, EvilElf, iAppDeveloper, jeroenkeij, jimmyon122, Mah6447, Morrisone, n00b, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0