[SOLVED]Help with function poiners.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

[SOLVED]Help with function poiners.

Post by Zer0XoL »

If you ever played zelda majoras mask you know you can wear masks with different effects.
I tried making masks so when i use them a special function is executed, it compiles however does not print any text, so i assume the functions is not executing properly.
Also if someone knows a better way to do this, please say it! :P

here is the main.cpp:

Code: Select all

//main.cpp
#include <iostream>
#include "mask.h"

int main()
{
    Player Link;
    Link.UseMask();
    std::cin.get();
    std::cin.get();
    return 0;
}
and here is the mask.h:

Code: Select all

//mask.h
#include <iostream>
using namespace std;

void mNormal()
{
     cout << "Normal";
}
void mDecu()
{
     cout << "DECCU!!";
}
void mGoron()
{
     cout << "GROONEN!!?";
}
void mZora()
{
     cout << "zorraen!";
}
void mMajora()
{
     cout << "Majjorra";
}

enum masks{NORMAL=0,DECU=1,GORON=2,ZORA=3,BAJS=4,MAJORA=5};

struct mask
{
       string name;
       void (*Use)();
};

class Player
{
      private:
              int mask_wearing;
              mask Maskz[20];
      public:
             void UseMask()
             {
                  Maskz[mask_wearing].Use;
             }
             Player()
             {
              mask_wearing = ZORA;
              Maskz[NORMAL].name = "NONE";
              Maskz[NORMAL].Use = &mNormal;
              Maskz[DECU].name = "Deccu mask";
              Maskz[DECU].Use = &mDecu;
              Maskz[GORON].name = "Goron mask";
              Maskz[GORON].Use = &mGoron;
              Maskz[ZORA].name = "Zora mask";
              Maskz[ZORA].Use = &mZora;
              Maskz[MAJORA].name = "Majoras mask";
              Maskz[MAJORA].Use = &mMajora;
             }
             
};
Please correct me about how i should handle .cpp and .h files, thanks. :)
Last edited by Zer0XoL on Sat Aug 29, 2009 4:08 pm, edited 2 times in total.
Image
Im Blue
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: Help with function poiners.

Post by dani93 »

Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Help with function poiners.

Post by Zer0XoL »

dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
Image
Im Blue
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: Help with function poiners.

Post by dani93 »

Zer0XoL wrote:Nope it says nothing, i use dev cpp.
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Zer0XoL wrote:Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
I don't know if this is better, but that's the way I handle such things in my map editor:
Every tile has an index, depending on the position in the tilesheet. E.g. the first tile has the index 0. If the index is larger than the width of the tilesheet divided by the width of a single tile, it is not in the first row. And so you can calculate the position in the tilesheet. You could make a row for every mask and add a (constant) value for the status of the character (looking right, left, etc) to the index.

Example:
single tile: 32*32
tilesheet: 128*256 (4 rows, 8 columns)
first mask index: 8 (first tile in the third row)
moving down: first in the third row (nothing to be done)
moving right: second in the third row (add 1 to the index)
moving left: third in the third row (add 2 to the index)
...

But you must always know where which mask begins. This should be marked by constants.

Just my suggestion :)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help with function poiners.

Post by Falco Girgis »

I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Dev-C++ uses GCC (MinGW).
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Help with function poiners.

Post by Zer0XoL »

dani93 wrote:
Zer0XoL wrote:Nope it says nothing, i use dev cpp.
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Zer0XoL wrote:Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
I don't know if this is better, but that's the way I handle such things in my map editor:
Every tile has an index, depending on the position in the tilesheet. E.g. the first tile has the index 0. If the index is larger than the width of the tilesheet divided by the width of a single tile, it is not in the first row. And so you can calculate the position in the tilesheet. You could make a row for every mask and add a (constant) value for the status of the character (looking right, left, etc) to the index.

Example:
single tile: 32*32
tilesheet: 128*256 (4 rows, 8 columns)
first mask index: 8 (first tile in the third row)
moving down: first in the third row (nothing to be done)
moving right: second in the third row (add 1 to the index)
moving left: third in the third row (add 2 to the index)
...

But you must always know where which mask begins. This should be marked by constants.

Just my suggestion :)
Ooh, im sorry but you may not know what i ment by "masks", i mean wearable masks than gives you powers in a game called zelda majoras mask, not tile masks or something like that but thanks anyway. xD
Actually you helped me know alot more about how i can make maps. :) :worship:
Image
Im Blue
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: Help with function poiners.

Post by dani93 »

I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Dev-C++ uses GCC (MinGW).
I thought it has its own compiler. If it uses MinGW you should be able to activate the -Wall flag somewhere in the compiler options.
Zer0XoL wrote:Ooh, im sorry but you may not know what i ment by "masks", i mean wearable masks than gives you powers in a game called zelda majoras mask, not tile masks or something like that but thanks anyway. xD
Soory, misunderstood you :shock2:
Can't think of a better way to manage this at the moment.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Help with function poiners.

Post by Joeyotrevor »

Zer0XoL wrote:
dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
A "more c++" way would be having a Mask interface and use inheritance and polymorphism, like this:

Code: Select all

//Mask.h

class Mask
{
protected:
  string name;
public:
  virtual void Use() = 0;
}

Code: Select all

//Decu.h

class DecuMask : public Mask
{
public:
	Decu()
	{
		name = "Decu mask";
	}

	void Use()
	{
		cout << DECCU!!";
	}
};
Etc...

Then you would make your Mask array an array of mask pointers and do this:

Code: Select all

masks[DECU] = new Decu();
masks[GORON] = new Goron();
to initialize it, instead of

Code: Select all

Maskz[DECU].name = "Deccu mask";
Maskz[DECU].Use = &mDecu;
Maskz[GORON].name = "Goron mask";
Maskz[GORON].Use = &mGoron;

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: [SOLVED]Help with function poiners.

Post by Falco Girgis »

I completely agree with Joeyotrevor. His C++-ified implementation is basically the same thing as a "functor."

Check them out here:
http://www.newty.de/fpt/functor.html

Function pointers are nifty and all, but they are rather limited, ugly, and fairly complex to do things with. Functors are just much cleaner ways to handle them, and that's how the old Elysian Shadows engine handled different AI actions.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: [SOLVED]Help with function poiners.

Post by Zer0XoL »

Joeyotrevor wrote:
Zer0XoL wrote:
dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
A "more c++" way would be having a Mask interface and use inheritance and polymorphism, like this:

Code: Select all

//Mask.h

class Mask
{
protected:
  string name;
public:
  virtual void Use() = 0;
}

Code: Select all

//Decu.h

class DecuMask : public Mask
{
public:
	Decu()
	{
		name = "Decu mask";
	}

	void Use()
	{
		cout << DECCU!!";
	}
};
Etc...

Then you would make your Mask array an array of mask pointers and do this:

Code: Select all

masks[DECU] = new Decu();
masks[GORON] = new Goron();
to initialize it, instead of

Code: Select all

Maskz[DECU].name = "Deccu mask";
Maskz[DECU].Use = &mDecu;
Maskz[GORON].name = "Goron mask";
Maskz[GORON].Use = &mGoron;
Oh, thanks a lot :shock2:
Im not done learning c++ and now you got me exited xD
GyroVorbis wrote:I completely agree with Joeyotrevor. His C++-ified implementation is basically the same thing as a "functor."

Check them out here:
http://www.newty.de/fpt/functor.html

Function pointers are nifty and all, but they are rather limited, ugly, and fairly complex to do things with. Functors are just much cleaner ways to handle them, and that's how the old Elysian Shadows engine handled different AI actions.
Thank you, i didnt know about functors :D
Its kinda cool to get help from you xD
Image
Im Blue
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: [SOLVED]Help with function poiners.

Post by dandymcgee »

Zer0XoL wrote:Im not done learning c++ and now you got me exited xD
Look what you've done, you made the poor guy leave.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply