Jump to content

Variables inside functions


Kazar
 Share

Recommended Posts

I'm rather new to C-programming and I have mostly some php background. I'm struggling with variables inside functions. I have a function that, among other things, controls the banking of an airplane that is turning. Here's the short version of it:

void HowToFly (TMesh airplane) {
		double blend;
		double frame,frameend,framebegin;
		TVec3 altitude = EntityPosition(airplane, 0);
		TVec3 meshrotation, meshrotation_old;

		// Turning animation
		meshrotation = EntityRotation (airplane,1);
		if ((meshrotation.Y > (meshrotation_old.Y+AppSpeed()*1.0)) && altitude.Y > 0.5) { // no banking when near the ground
			framebegin=80.0;    // turning to left keyframes
			frameend=81.0;
			blend=blend+(AppSpeed()*0.05);
		}
		else if (((meshrotation.Y+AppSpeed()*1.0) < meshrotation_old.Y) && altitude.Y > 0.5) { // no banking when near the ground
			framebegin=60.0;	// turning to right keyframes
			frameend=61.0;
			blend=blend+(AppSpeed()*0.05);
		}
		else {
			blend=blend-(AppSpeed()*0.025);
		}

		// level flight
		frame=AppTime()/10.0;
		frame=fmodf(frame,1.0-0.0)+0.0;		
		Animate(airplane,frame,1.0,0,true);	

		// blended animation
		frame=AppTime()/10.0;
		frame=fmodf(frame,frameend-framebegin)+framebegin;		
		Animate(airplane,frame,blend,0,true);	

		blend=max(blend,0.0);
		blend=min(blend,1.0);

		meshrotation_old = meshrotation;
}

 

Of course this worked as part of the main program with blend,frame,frameend,framebegin, etc. - variables initialized before the mainloop. I had to make a function out of it to control multiple plane's animation and this breaks it because all my variables are reset at the start of each loop.

 

I have ran out of all ideas to make it work (except global variables but that really isn't good solution for tens of planes, I think) and I could really use a nudge in to the right direction.

 

 

 

 

Oh, and sorry for the blatant crudeness of my code ;)

Core i5-750 - GTX 460 1GB - 12GB DDR3 - Win 7 x64

Link to comment
Share on other sites

If you want to stick with just C, you would create structs for each plane that has the variables per plane that is needed. I would use C++ and a class for this though. Make a plane class that has all the variables/functions you need.

 

class Plane
{
private:
  // variables for this plane go here
public:
  void HowToFly()
  {
  }
};

Link to comment
Share on other sites

Actually, I did read about structs (wrote a nice paper note about it to learn) but for some reason came to conclusion it wasn't what I needed. Seems I was wrong, though.

I will eventually move to C++ but it seems I have a lot to learn about C still ;)

 

Thanks for the help, I think I'll use the C++ class, since it seems more straightforward.

Core i5-750 - GTX 460 1GB - 12GB DDR3 - Win 7 x64

Link to comment
Share on other sites

That won't work for multiple planes. They would all still be sharing the same variable, which I thought isn't what you were looking for? If you have 10 planes, you would need 10 different static variables in that method and know which one to use. Otherwise they would all share that 1 static. It's not really even an option in my view.

Link to comment
Share on other sites

Yeah, in this case it would basically act the same. Statics just retain their value from where they were defined. So if you define one in a function, every time that function gets called it'll be that same exact variable with the same value it had the last time it was called. I almost never use static variables inside functions because it's almost always a bad design idea. There are other uses for static variables though inside classes that make them useful. Like in a Singleton pattern.

 

Let me know if you need any help with the C++ class. I love helping people learn C++ ;)

Link to comment
Share on other sites

Here is a quick try with not having access to a compiler and my kid screaming.

 

This would be the general idea anyway.

 

class Plane
{
private:
double blend, frame, frameend, framebegin;
TVec3 meshrotation_old;
public:
Plane()
{
	blend = 0.0;
	frame = 0.0;
	frameend = 0.0;
	framebegin = 0.0;
	meshrotation_old = Vec3(0);
}
void HowToFly (TMesh airplane)
{
       	TVec3 altitude = EntityPosition(airplane, 0);
               TVec3 meshrotation;

               // Turning animation
               meshrotation = EntityRotation (airplane,1);
               if ((meshrotation.Y > (meshrotation_old.Y+AppSpeed()*1.0)) && altitude.Y > 0.5) { // no banking when near the ground
               	framebegin=80.0;    // turning to left keyframes
                       frameend=81.0;
                       blend=blend+(AppSpeed()*0.05);
               }
               else if (((meshrotation.Y+AppSpeed()*1.0) < meshrotation_old.Y) && altitude.Y > 0.5) { // no banking when near the ground
               	framebegin=60.0;        // turning to right keyframes
                       frameend=61.0;
                       blend=blend+(AppSpeed()*0.05);
               }
               else {
                       blend=blend-(AppSpeed()*0.025);
               }

               // level flight
               frame=AppTime()/10.0;
               frame=fmodf(frame,1.0-0.0)+0.0;         
               Animate(airplane,frame,1.0,0,true);     

               // blended animation
               frame=AppTime()/10.0;
               frame=fmodf(frame,frameend-framebegin)+framebegin;              
               Animate(airplane,frame,blend,0,true);   

               blend=max(blend,0.0);
               blend=min(blend,1.0);

               meshrotation_old = meshrotation;
}
};



// There are a number of better ways to create all of these, but this is the easiest to start learning
Plane plane1, plane2, plane3;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...