Advertisement

a simple question(i think)

Started by October 25, 2000 01:06 PM
9 comments, last by Necromancer 24 years, 2 months ago
How do you make a function that takes multiple objects from two or more struct''s and manipulates thare member data? The function will need to take a different object struct and a second different object from a new struct and manipulate one object''s member data baised on bolth''s member data... Like this: struct A{ int characteristic; int life; }; struct B{ int characteristic; }; void function(A Target, B TargetA){A.life = A.characteristic - B.characteristic;}; How do i make the function take two objects, one struct A and one struct B, that arnt named Target and TargetA but rather what ever objects i make from the struct''s like: //so i could call the function whith any object''s i make from //the struct... B Brick; A Pick; function(Pick, Brick); //or A Cat; B Dog; function(Cat, Dog); So how do you make a universal function that will take objects and manipulate thare struct baised on the name... and i shouldnt have to overload because im using the same two struct''s but just different objects... help if you can...?
Keep conjuring the undead, my friends...
Im a little confused, you said the function should change the member variables by what the struct''s names are. Well, youl have to have a string in the struct telling which name. If thats not anything close to what your asking, try to change the way your asking and Ill try to help,
later,
Advertisement
If I understand what you''re asking, what you are saying cannot be done in C/C++. I believe you can do it in Java with reflection, but I wouldn''t put any money on that. You could do it in Scheme or most other functional languages, but I don''t think anyone in their right minds are interested in programming games in those. (Except as scripting languages).
it should take two struct''s objects... not the name''s just like the current object im using...
maby this will help: (Like in an RPG senerio...):
struct Enemy{
string Name;
num LVL;
num HP;
num MP;
num STR;
num AGI;
num INT;
};
struct Hero{
string Name;
string Class;
string Race;
num EXP;
num LVL;

num HP;
num MP;

num STR;
num AGI;
num INT;
num LUC;

attack(Enemy Target,Hero Attacker){
Target.HP =
Target.HP - Attacker.STR;};
};
Hero Vailin;
Vailin.STR = 30;
Enemy Goblin;
Goblin.HP = 70;
Vailin.attack(Goblin,Vailin);
//the attack needs to work in any case
Hero guy;
Enemy Blob;
guy.attack(blob,guy);

i need the function to be able to be used on any by any Hero and any Hero needs to be able to attack any Enemy
so the attack function needs to take the target and the one attacking and use thare member data to compute damage(HP loss)
i dont know if pointers will work or how to make them work...
but referances dont work...

I just want the function to to think attack("do damage to this object of the Enemy struct", "is the Hero object doing the damage")
i hope that makes it more clear...
Keep conjuring the undead, my friends...
quote: Original post by Necromancer


i dont know if pointers will work or how to make them work...
but referances dont work...




Actually, references should work. Just define your attack function as:

void attack(Enemy & target, Hero & attacker)




----------
Andrew





Edited by - acraig on October 25, 2000 3:08:08 PM
yes i thought so too but how
please dont leave me hanging...

(fix my example with referances)...
much appreaceated!
Keep conjuring the undead, my friends...
Advertisement
yes i thought so too but how
please dont leave me hanging...

(fix my example with referances)...
much appreaceated!
Keep conjuring the undead, my friends...
how do you fix my function body with referances...
.
attack(Enemy & Target,Hero & Attacker){
Target.HP =
Target.HP - Attacker.STR;};
how do i make the referance convert to member data...
can i make referances in my struct''s that automaticly refer to the object made by the struct...
help me?..
Keep conjuring the undead, my friends...
seriously don''t use references, that is not normal.

//define parameters as pointers:attack(Enemy *Target,Hero *Attacker){Target->HP =Target->HP - Attacker->STR;};};Hero Vailin;Vailin.STR = 30;Enemy Goblin;Goblin.HP = 70;Vailin.attack(&Goblin,&Vailin);//or Hero *Vailin = new Vailin;Vailin->STR = 30;Enemy *Goblin = new Goblin;Goblin->HP = 70;Vailin.attack(Goblin,Vailin); 






cmaker- I do not make clones.
Thanx (you have no idea what a painful question that was to ask)


Keep conjuring the undead, my friends...
Keep conjuring the undead, my friends...

This topic is closed to new replies.

Advertisement