8 Replies - 1248 Views - Last Post: 26 October 2009 - 05:42 AM Rate Topic: ****- 1 Votes

#1 xnewix   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 211
  • Joined: 23-May 09

if statements

Posted 22 October 2009 - 08:05 PM

Hello all, recently i've been writing a small game, and have come across an issue.
If I had a load of if statments that where basically following the same format, is there a way in which I can place them into a function inside the UnitData class.

The UnitData class is just a set of var with functons to change them and return their values

This bit of code is declared at the begining of main()

UnitData unitdata;
UnitData* unitlist = new UnitList[256];


This is futher down the file, the tags are strings that apper in textfiles as the data is read in through a filereading system

for(int readfile=0; readfile < half; readfile++) {
	open_unitdata_file >> unitdata_statment_str;
	open_unitdata_file >> unitdata_number_str;

	//! Check if string is equal to the unit_type_tag.
	if(unitdata_statment_str.compare(unit_type_tag) ==0) {
		unitlist[i].SetUnitType(unitdata_number_str);
	}
	//! TO DO: std::string SetCloseWeapon(std::string);
	//! TO DO: std::string SetRangeWeapon(std::string);

	//! Check if string is equal to the unit_life_tag.
	if(unitdata_statment_str.compare(unit_life_tag) == 0) {
		 int health_convert = filedata.getnumber(unitdata_number_str);
		 unitlist[i].SetMaxLife(health_convert);
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: if statements

#2 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: if statements

Posted 23 October 2009 - 12:20 AM

View Postxnewix, on 22 Oct, 2009 - 07:05 PM, said:

Hello all, recently i've been writing a small game, and have come across an issue.
If I had a load of if statments that where basically following the same format, is there a way in which I can place them into a function inside the UnitData class.


xnewix, that's actually a good question. Congrats!

Such occurrence of if statements (especially to do something specific depending on a attribute, or on the type) is definitely a code smell that needs a bit of work.

What you are looking for is polymorphism here. You want to create a subclass of UnitData, with the specific behaviour when if(unitdata_statment_str.compare(unit_life_tag) == 0) -- except you won't need this statement anymore, except maybe when creating your object instance. You'll probably have something like UnitLifeData, subclass of UnitData, and redefining UnitData methods to include the specific stuff.

Edit: proper tagging! ;)

This post has been edited by EdwinNameless: 23 October 2009 - 12:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 xnewix   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 211
  • Joined: 23-May 09

Re: if statements

Posted 23 October 2009 - 05:52 AM

thanks i'll have a look into it. but can you explaine this idea of subclasses a little more
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: if statements

Posted 23 October 2009 - 06:56 AM

Not to step on anyone's toes here -- but if you are loading a bunch of data and want to classify it as you are building up your objects you will still need if-statements determine which kind of object you need. Polymorphism is not magic.

Once the object have been created, well then polymophism can be helpful since you don't necessarily have to worry about writing functions that determine what kind of object "this" is -- but upon initial loading you will still need if-statements to know what kind of objects to create.

You can encapsulate this all into a single function sure, but you are going to have to preform the conditional statements.

One thing that can be helpful at this point is meta-programming which can make writing out the if-statements a little easier. This is one of the kinds of places where Macro's (evil as they may be) actually can be very useful.

I don't want to turn you away from polymorphic types -- I just want to point out that they don't make your frustration with if-statements any easier.

Oddly enough though -- there are some "tricks" you can use to make things a tiny bit easier. For example using a hash table.
Was This Post Helpful? 0
  • +
  • -

#5 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: if statements

Posted 23 October 2009 - 07:22 AM

View PostNickDMax, on 23 Oct, 2009 - 05:56 AM, said:

Not to step on anyone's toes here -- but if you are loading a bunch of data and want to classify it as you are building up your objects you will still need if-statements determine which kind of object you need. Polymorphism is not magic.


That goes without saying: you'll definitely need a if ... else when initializing your instances. Ideally you'll even use the factory pattern/abstract factory for this. ;)

But here we're talking about proliferation of if ... else statements all throughout the code to do different things depending on the type of data you're handling. That's definitely a symptom (again, we refer to this as a "code smell") of something gone bad, and this screams for refactoring, and I'm sure you'll agree the best solution for this kind of things is polymorphism.

Quote

I don't want to turn you away from polymorphic types -- I just want to point out that they don't make your frustration with if-statements any easier.


Maybe not, but at least it localizes the problem (if your type behaviour changes, you'll do the change in only one place, not wherever an if ... else appears all throught the code; if you have a new one, you'll have to go through all these if ... else and potentially add new layers ), and it encapsulates behaviour where it belongs in proper OOP.

Edit: Buttery fingers...

This post has been edited by EdwinNameless: 23 October 2009 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 xnewix   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 211
  • Joined: 23-May 09

Re: if statements

Posted 24 October 2009 - 04:56 PM

The only way I am able to concive is through the use of macros. I'm not to sure what a hash table is lol oh well.
Was This Post Helpful? 0
  • +
  • -

#7 xnewix   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 211
  • Joined: 23-May 09

Re: if statements

Posted 25 October 2009 - 08:12 AM

Okay i've attached my code um. . . . all of it. It may be easier to look at the problem this way. The if statments are in Script.cpp the code starts at around about line 160, ish

There are also some other problems with the code, I wanted to write so error functions but didn't know how to go about it.

would I have to use;

try catch 


I also tried adding bool's to the code but this didn't really work, I think at the moment it has a load of jj++ all over the place, not good, this code also is a waste of space as it dosn't do anything.



*** MOD EDIT: Removed executables from ZIP file to reduce size ***

***NEWI EDIT: Uploaded again, file was corrupt. All .exe removed

Attached File(s)


This post has been edited by xnewix: 25 October 2009 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: if statements

Posted 25 October 2009 - 08:41 AM

Hmmm...might this be better served in the Games Programming forum?
Was This Post Helpful? 0
  • +
  • -

#9 xnewix   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 211
  • Joined: 23-May 09

Re: if statements

Posted 26 October 2009 - 05:42 AM

Okay I did a bit of reading and this is Inheritance Polywatsite or whatever it was called (apperantly). Though looking at it I'm not sure how to really use it to be effective in cutting down my code. Correct me if i'm wrong but is this the correct way to do it?

#include <iostream>
#include <string>

class FileReading {
	private:
	std::string filename;
	public:
	virtual void setfilename(std::string newfilename) {
		filename = newfilename;
	}
	virtual std::string getfilename() {
		return filename;
	}
	
};
class Unit : public FileReading {		
};
class Weapon : public FileReading {
};

int main() {
	Unit unit;
	
	std::string str = "test";
	
	unit.setfilename(str);
	std::cout << unit.getfilename();
	
	system("pause"); //! please don't grill me for this it was just a quick test :)
	return 1;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1