6 Replies - 324 Views - Last Post: 05 June 2014 - 09:49 AM Rate Topic: -----

#1 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 630
  • Joined: 24-April 10

Parsing an image location, in the constructor or where?

Posted 04 June 2014 - 02:41 PM

Hello. I'm working on a small game, and I have a quick question about parsing a file.

Basically, I have a utility method, which loads an XML file containing the details of all the items for the level, and then pulls the info and passes it to the Item constructor.

A snippet of the XML file is:
<item key = "Weapon1" name = "Sword"  description = "A basic sword... etc" hint = "picked up SWORD" givesPlayer = "basicSword:1" bitmap = "5,5" />

And a snippet of the class is:
public class Item extends Entity {

	String name;
	private String description;
	private String hint;
	private Map<String, Integer> givesPlayer = new HashMap<>();
	private boolean pickedUp;

	// A
	public Item(String name, String description, String hint, String givesPlayer, String frames) {
			
			this.name = name;
			this.description = description;
			this.hint = hint;		
			this.currentFrame = 0;
			this.pickedUp = false;
			
	}
	
	// B
	public Item(String name, String description, String hint, HashMap<String, Integer> givesPlayer, Bitmap[] frames) {
			
			this.name = name;
			this.description = description;
			this.hint = hint;
			this.givesPlayer = givesPlayer;
			this.frames = frames;
			this.currentFrame = 0;
			this.pickedUp = false;
			
	}
}

The XML file contains this field:
bitmap = "5,5"
which basically specifies a column and row where it should grab an image from, located elsewhere.

My question is this:
Should I parse this "5,5" in my utility method and pass the image(s) to the constructor(// B Consturctor), or is it ok to pass the location as a String and parse it in the Constructor( // A Constructor) ?

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing an image location, in the constructor or where?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Parsing an image location, in the constructor or where?

Posted 04 June 2014 - 02:49 PM

For me I dont see that much different in your options, though I will prefer to have the parsing activities together in one place and leave the class that hold data away from parsing activities...
This also gives a good separation of different activities and simplifies maintenance and enhancement.

This post has been edited by smohd: 04 June 2014 - 02:51 PM

Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Parsing an image location, in the constructor or where?

Posted 04 June 2014 - 02:54 PM

I would much rather go with solution B
Item should have no relation to the xml file, it should not know what the value "5,5" means
Let the deserializer/parser handle that

I would personally use elements instead of attributes for your xml
You had to explain to us what bitmap = "5,5" means instead of letting the xml speak for itself
<item>
	<key>Weapon1</key>
	<name>Sword</name>
	<description>A basic sword... etc</description>
	<hint>picked up SWORD</hint>
	<givesPlayer>basicSword:1</givesPlayer> <!-- What does this mean? -->
	<bitmap>
		<column>5</column>
		<row>5</row>
	</bitmap>
</item>


Was This Post Helpful? 2
  • +
  • -

#4 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 630
  • Joined: 24-April 10

Re: Parsing an image location, in the constructor or where?

Posted 04 June 2014 - 03:57 PM

Cheers, thanks for the input, it'S much appreciated.

<givesPlayer>basicSword:1</givesPlayer> <!-- What does this mean? -->
This something that I'm still working on. Some items when picked up, will give the player a few items/things instead of just whatever the picked up item is. The format is whichItem:howMany;whichItem:howMany etc all separated with a semi colon. I'm storing the items in a HashMap<String, Integer>, so whichItem represtents the key, howMany represents the quantity. I'm not 100% set on this approach, but until I come across a better alternative, it's what I'm using.

This post has been edited by mike73: 04 June 2014 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Parsing an image location, in the constructor or where?

Posted 05 June 2014 - 04:58 AM

Wouldn't that be a bag of items though?
I wouldn't put that kind of information in the Item class itself

The more object oriented approach would be to have a class which handles the stacks
A HashMap would be able to work, but I prefer implementing a class for the task itself
That way you can easily add a max quantity to the stack as well, or some other logic later on
public class LootBag {
	private ItemStack[] items;
	
	public LootBag(ItemStack... items) {
		this.items = items;
	}
}

public class ItemStack {
	private Item item;
	private int quantity;
	
	public ItemStack(Item item) {
		this(item, 1);
	}
	
	public ItemStack(Item item, int quantity) {
		this.item = item;
		this.quantity = quantity;
	}
	
	public void addQuantity(int delta) {
		quantity += delta;
	}
	
	public Item getItem() {
		return item;
	}
	
	public int getQuantity() {
		return quantity;
	}
}



I of course know nothing about your game, and maybe it doesn't make sense with a LootBag
Though I would recommend having a ItemStack

This post has been edited by CasiOo: 05 June 2014 - 04:59 AM

Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,598
  • Joined: 29-July 11

Re: Parsing an image location, in the constructor or where?

Posted 05 June 2014 - 08:08 AM

I guess if I were making a game that allowed users to craft their own items, I might save that data to and then pull it from xml JSON, or a database, but what is the reasoning behind it here? If you are not doing that, you have to create the classes with data anyway, so why bother typing everything twice, reading it from xml, then creating the class with the data? It makes more sense to me to just hard-code this data to the class.
Was This Post Helpful? 0
  • +
  • -

#7 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 630
  • Joined: 24-April 10

Re: Parsing an image location, in the constructor or where?

Posted 05 June 2014 - 09:49 AM

Yeah I originally just had it all hard coded, but for practice sake I moved it all to XML files. I don't have much experience with parsing files, so it seemed like a nice opportunity to try it out and learn something.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1