Sign in to follow this  
In_Yack_Mode

Trying to use functions

Recommended Posts

I have never used functions I built so I decided to try and build some I could use later. I tried to make one that would check if the arrow keys were pressed and if they were move the man. Can that be done with a function? I tried making it and putting it in an include file. Here is my attempt: Source file:
#include "movement.h" // it being included 
(...) void check_keys();// its declaration
(...) check_keys(); // it being called
Header
int man_x = 0; 
int man_y = 0; 
void check_keys();
void check_keys()
    {
        if(key[KEY_RIGHT])			
                        {
				man_x ++;			
			}
        if(key[KEY_LEFT])
			{
				man_x --;
			} 
	if(key[KEY_UP])
			{
				man_y --;
			}
	if(key[KEY_DOWN])
			{
				man_y ++;				
			}   
    }END_OF_FUNCTION(check_keys); 
It compiles fine but does nothing when I try pressing the arrow keys. Do you notice what might me wrong?

Share this post


Link to post
Share on other sites
I'm assuming key[] is an array to store the state of all keys, are you updateing these states every iteration of the main loop? or even updateing them at all?

are you only calling check_keys() once, then the app closes?

do you have a main loop that keeps the app running?

more info on how you have your app setup up to run?

Share this post


Link to post
Share on other sites
Does your IDE have a debugger, might be helpful to watch the man_x value and see how it changes if it does change by stepping through with the debugger.

How are you using the man_x and man_y values?

Share this post


Link to post
Share on other sites
this code is now replaced with check_keys()
 if(key[KEY_RIGHT])			
{
man_x ++;
}
if(key[KEY_LEFT])
{
man_x --;
}
if(key[KEY_UP])
{
man_y --;
}
if(key[KEY_DOWN])
{
man_y ++;
}



I have man_x and man_y set in the sprite drawing function like this: draw_sprite(buffer, man, man_x, man_y);

Share this post


Link to post
Share on other sites
when you run the app, is the sprite always drawn at 0,0 ?

is man_x and man_y in the same scope everywhere you're using it?

is the code where these variables are being used long? if not too long try posting it or atleast the code where the variables are declared, initialized, where the draw_sprite call is used?

can you display font to the screen? try displaying the values of man_x and man_y on the screen so you can see they're values at real time.

Share this post


Link to post
Share on other sites
Yes the sprite is always drawn at 0,0. man_x and _y are both called at the beginning of the program. The code where it's used isn't too long but it's really only used in the snippet I posted a minute ago. Here is where it is defined and set: int man_x = 0; // Holds our pictures X coorinate
int man_y = 0; // Holds our picture's Y coordinate

Share this post


Link to post
Share on other sites
I think whats happening is you have two different man_x's and Man_y's, in different scopes.

ie.

in "movment.h" you declare them and you're probably declareing them in annother file and useing those one to place your sprite.

I'm not sure wich language you're using exactly, but in C/C++ you'd do something like. In movement.h declare the man_x and man_y values like so

extern int man_x;
extern int man_y;

that will make them global.

Share this post


Link to post
Share on other sites
You're doing this:

movement.h

extern int man_x=0;


You can't initialise (assign the zero) to the extern. You need to create a movement.cpp and do this:

movement.h

extern int man_x;


movement.cpp

int man_x=0;


Basically, the extern int man_x; tells the compiler "There is an int called man_x defined somewhere else, possibly in another translation unit, let the linker worry about it."

You can't assign a value to this declaration as you are not actually defining the variable here.

So you need to actually define the variable somewhere else and by not doing that in the header, it means you can include the header multiple times in other translation units without each one getting a copy of the variable.

Share this post


Link to post
Share on other sites
I did that before I posted the error last night but I was getting this:
[Linker error] undefined reference to `man_x'
[Linker error] undefined reference to `man_y'

I figured that wasn't it and put it back like it was. I usually get those errors when I forget to include something but I have it included.

Share this post


Link to post
Share on other sites
What in the world is "END_OF_FUNCTION(check_keys)" supposed to do? All I could imagine is that it would evaluate to nothing, and just be a marker for the end of a function... but is that really necessary? Just use a comment if you really want something telling you what the closing bracket is for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
What in the world is "END_OF_FUNCTION(check_keys)" supposed to do? All I could imagine is that it would evaluate to nothing, and just be a marker for the end of a function... but is that really necessary? Just use a comment if you really want something telling you what the closing bracket is for.


I was reading someone else's function they built and they had it so I thought that it was needed. I just removed it now.

rip-off I read that and might have missed what you were trying to point out so I'm rereading it.

Share this post


Link to post
Share on other sites
Quote:
Original post by In_Yack_Mode
I did that before I posted the error last night but I was getting this:
[Linker error] undefined reference to `man_x'
[Linker error] undefined reference to `man_y'

I figured that wasn't it and put it back like it was. I usually get those errors when I forget to include something but I have it included.


you're getting that because whatever source file you're using the variable in doesn't know about it.

try this

movement.h

extern int man_x;
extern int man_y;



movement.cpp

void SetPosition(void)
{
man_x = 0;
man_y = 0;
}



main.cpp

#include "movement.h" // this will let main.cpp know everything about movement.h

void main(void)
{
SetPosition(); // initialize man_x and man_y

// you should be able to use the variables as you need now.
}













Share this post


Link to post
Share on other sites
Quote:
Original post by In_Yack_Mode
I was reading someone else's function they built and they had it so I thought that it was needed. I just removed it now.

rip-off I read that and might have missed what you were trying to point out so I'm rereading it.


... Sigh.

Programming is a mental discipline, it requires thinking. What rip-off was trying to point out was the entire article; you have to understand how these things work in order to do them properly. Putting things in because you "think they are needed" - because you saw them somewhere else - is a one-way ticket to nowhere as a programmer. Try not to do anything without at least a basic understanding of what you are doing. Try writing things in more basic ways, too, and worry about the language fundamentals before flashy stuff. Understanding how to write and call functions, and organize your source code files, is much more important than figuring out how to process the arrow keys with Allegro (I'm guessing wildly about Allegro, based on the results of Google searching for END_OF_FUNCTION).

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
... Sigh.

Programming is a mental discipline, it requires thinking. What rip-off was trying to point out was the entire article; you have to understand how these things work in order to do them properly. Putting things in because you "think they are needed" - because you saw them somewhere else - is a one-way ticket to nowhere as a programmer. Try not to do anything without at least a basic understanding of what you are doing. Try writing things in more basic ways, too, and worry about the language fundamentals before flashy stuff. Understanding how to write and call functions, and organize your source code files, is much more important than figuring out how to process the arrow keys with Allegro(I'm guessing wildly about Allegro, based on the results of Google searching for END_OF_FUNCTION).


I think I am going to take your advice. Thanks everybody for your help on this though. freeworld your code got it working, but Zahlman is right I need to understand more basic stuff before jumping too Allegro.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this