|
-
March 4th, 2009, 12:16 AM
#1
a better way?
I got a host of semi-complex functions that you feed in some data and then spit it out. Problem is I need to make sure the data is correctly saved separately if a different object is using the function
so for instance
public void ChangeLevel(int newlevel, int levelid, int strength, int vitality, int agility, decimal classtype)
we are going to give the function some data to work correctly
newlevel = whatever the new level is 1,2,3,4etc...
levelid = which object are we leveling?? object 1 object 2, etc...
that particular objects stats, object 1 strength, object 1 vitality, etc
and finally that objects class (fighter, mage)
lately I have been doing this the obvious and nasty way
I check which object it is
if (levelid == 1)
if so, then I use these sets of variables
newStrength1 = strength;
newVitality1 = vitality;
newAgility1 = agility;
as you can quickly see, if I have 5+ more objects, the code can get pretty long and nasty
if (levelid == 2)
newStrength2 = strength;
newVitality2 = vitality;
newAgility2 = agility;
if (levelid == 3)
newStrength3 = strength;
newVitality3 = vitality;
newAgility3 = agility;
I was wondering perhaps to shorten the code and make useful for any number of objects. I was thinking of a way to give ownership to the variables depending on which object is being used.
one of the ways I was thinking of was to append the universal variable's (name) with the object id.
string owner = Convert.ToString(levelid);
newStrength+owner; // the name has been appended
newStrength+owner = strength; //the value has changed
but this doesnt work cause newStrength is an int, I dont want to change its value, I just want the reference to belong to the object (levelid). Bah.
-
March 4th, 2009, 12:33 AM
#2
Re: a better way?
Well, why not putting your stuff like:
newAgility1
newAgility2
newAgility3
...
into an array? This is the way I would go.
-
March 4th, 2009, 12:42 AM
#3
Re: a better way?
time to learn arrays then!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|