Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

StormUK

How would i pass an array of a class to a function?

This topic is 5581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Title says it all - here is what i have tried so far
  
#include <iostream.h>
#include <windows.h>
#define null 0

class mob
{
public:
	mob()
	{
		posx=0;
		posy=0;
		type=0;
		health=0;
		mana=0;
		ac=0;
		dc=0;
		id=0;
		lastattack=0;
		lastcast=0;
		spellid=0;
		buffid=0;
		castdelay=0;
		attackdelay=0;
	}
	void move(){

	}
	void attack(){
		int now=GetTickCount();
		if(now>=(lastattack+attackdelay))
		{
			cout<<"Attack!"<<endl;
			lastattack=GetTickCount();
		}
	}
	void spell(){
		int now=GetTickCount();
		if(now>=(lastcast+castdelay))
		{
			cout<<"Spell!"<<endl;
			lastattack=GetTickCount();
		}

	}
	void buff(){
		int now=GetTickCount();
		if(now>=(lastcast+castdelay))
		{
			cout<<"Buff!"<<endl;
			lastattack=GetTickCount();
		}
	}

	int posx, posy;
	int type, health, mana, ac, dc, id;
	int spellid, buffid;
	int lastattack, lastcast, castdelay, attackdelay;
};
class spell
{
public:
	spell();
	void effects(){

	}
	spell *next;
private:
	int id;
	int targetid;
	char name[25];
	int duration;
	int tick;
	int range;
	int AC;
	int DC;
	int mana;
	int health;
	int speed;
	int damage;
	int costMP;
	int costHP;
	int level;
	int castTime;
	int radius;
	int cooldown;
	char effect[256];
	char icon[256];
};
class buff
{
public:
	buff();
	void effects(){

	}
	buff *next;
private:
	int id;
	int targetid;
	char name[25];
	int duration;
	int tick;
	int range;
	int AC;
	int DC;
	int mana;
	int health;
	int speed;
	int damage;
	int costMP;
	int costHP;
	int level;
	int castTime;
	int radius;
	int cooldown;
	char effect[256];
	char icon[256];
};

mob mobs(mob monster[]);

int main()
{
	bool done=false;
	int i=0;

	mob monster[10];
	
	//Load Map

	//Load Spells

	//Load Buffs

	//Load Mobs

	//Load Items

	//Load Player


	while(!done)
	{
		monster=mobs(monster, 10);
		
		//Spells

		//Buffs

	}


	return 0;
}

mob mobs(mob monster[], int nummon)
{
	int i=0;
	for(i=0;i<nummon;i++)
	{
		monster[i].attack();
		monster[i].buff();
		monster[i].spell();
	}
				
	return monster;

}
  
Thanks, Chris.

Share this post


Link to post
Share on other sites
Advertisement
class A {
friend void B(A&,int);
private:
int var;
}

..
..

void B(A& integer, int number)
{
integer.var=number; //this works now
}


Something like that





Share this post


Link to post
Share on other sites
gah, i dont 'think' you undersand what i want..

ill try again

Say i had an array and i had a function and i wanted the function to take the array, manipulate the data, then pass it back to the main loop, how would i do this?

could i use the same method if i had an array of a class?

eg;

      
class a{
int blah
void somthing() {
blah++;
}
};

a somestuff(a myclass);

int main(){
a myclass[10];

myclass=somestuff(myclass);
for(int i=0;i<10;i++){
cout<<myclass[i].blah<<endl;
}
return 0;
}

a somestuff(a myclass){
for(int i=0;i<10;i++){
myclass[i].somthing
}
return myclass;
}


[edited by - StormUK on March 7, 2003 10:33:32 AM]

Share this post


Link to post
Share on other sites
so the array is declared from the main function and
you want to pass the WHOLE array to an outside function and have that data pass back to main??

do something like this...

void ManipData(myclass array[])
{
..the magic happens here
...

}

int main()
{
myclass myarray[10];
ManipData(myarray);
}


myarray holds the address of the myarray[0]

sorry if i still don''t understand your question

Share this post


Link to post
Share on other sites
You can just return the array refernce from the paramter. Since you are effectively passing a pointer to the first item in your array, any modification done to the array contents by the function will still be present when the function exits. Passing the array back is needless really.

if you really still want the array back, check this little sample out:


  
#include <string>
#include <iostream>

using namespace std;

class myclass {
public:
int x;
};


myclass* func(myclass p[]) {

//magic code of goodness here


return p;
};


void main() {


myclass array[10];
myclass* arrayptr;

arrayptr = func(array);


for(int i=0;i<10;i++) {
array[i].x = i;
}


cout << arrayptr[1].x << endl;

getchar();

}


Unless I missed the boat one what you are trying to do If so I am sorry.



============================
A guy, some jolt, and a whole lot of code

[edited by - Tyson on March 7, 2003 1:47:06 PM]

Share this post


Link to post
Share on other sites
The array is a pointer, when you change the data in another function, then the real original data is changed, you dont need to return it to the main loop

If God with me, Who against me?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!