Jump to content

Using variables that were declared inside a function, but in int main()

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
////////////////////////////////////////////////////////////

// Headers

////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>

#include <fstream>

#include <stdio.h>

#include <string.h>

#include <sstream>

#include <iostream>  // I/O 

#include <iomanip>   // format manipulation

using namespace std;


////////////////////////////////////////////////////////////

/// Entry point of application

///

/// \return Application exit code

////////////////////////////////////////////////////////////

int create_bullet(int bullet_count, int x, int y, int speed, int direction, int color_r, int color_g, int color_b)

{

	int create_id = bullet_count + 1;

	int bullet_x[3], bullet_y[3], bullet_speed[3], bullet_direction[3], bullet_color_r[3], bullet_color_g[3], bullet_color_b[3];

	

	// Position of the bullet

	bullet_x[create_id] = x;

	bullet_y[create_id] = y;


	// Speed of the bullet

	bullet_speed[create_id] = speed;


	// Direction of the bullet

	bullet_direction[create_id] = direction;


	// Color of the bullet

	bullet_color_r[create_id] = color_r;

	bullet_color_g[create_id] = color_g;

	bullet_color_b[create_id] = color_b;


	// We've got one extra bullet now,

	// don't forget to add 1 extra after creation!


	// Create successful!

	return 1;

}


int main()

{

    // Create the main rendering window

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet Fusion C++ Version");


	// Initialize the player :3

	int player_hspeed = 0;

	int player_vspeed = 0;

	int bullet_count = 0;

	int level_number = 1;

	const double pi = 3.1415;


	const int player_hspeed_max = 125;

	const int player_vspeed_max = 125;


	// Load the sprite image from a file

    sf::Image PlayerImage;

	sf::Image BulletImage;

	if (!PlayerImage.LoadFromFile("data/gfx/player1.png"))

        return EXIT_FAILURE;


	if (!BulletImage.LoadFromFile("data/gfx/bullet1.png"))

        return EXIT_FAILURE;


	[...]


    // Create a sprite for the player image.

    sf::Sprite PlayerSpr(PlayerImage);

	sf::Sprite BulletSpr(BulletImage);


	// Set up some properties for it.

	PlayerSpr.SetCenter(27.f, 4.f);

	BulletSpr.SetCenter(14.f, 14.f);

    PlayerSpr.SetPosition(400.f, 520.f);


    // Start game loop

    while (App.IsOpened())

    {

        // Process events

        sf::Event Event;

        while (App.GetEvent(Event))

        {

            // Close window : exit

            if (Event.Type == sf::Event::Closed)

                App.Close();

        }


        // Get elapsed time

        float ElapsedTime = App.GetFrameTime();


[...]


        // Clear screen

        App.Clear();


        // Display sprite in our window

		// Initialize a variable for our personal uses

		create_bullet(bullet_count, 26, 26, 1, 45, 255, 0, 0);


		int i = 0;


		// Enter the bullet update loop

		while (i < bullet_count)

		{

			// Update every bullet's position

			bullet_x[i] = bullet_x[i] - sin(bullet_direction[i]*pi/180);

			bullet_y[i] = bullet_x[i] - cos(bullet_direction[i]*pi/180);


			BulletSpr.SetPosition(bullet_x[i], bullet_y[i]);

			BulletSpr.SetColor(sf::Color(0, bullet_color_r[i], bullet_color_g[i], bullet_color_b[i]));

			App.Draw(BulletSpr);

		}

[...]

}

1>.\SFML_Test.cpp(212) : error C2065: 'bullet_x' : undeclared identifier

1>.\SFML_Test.cpp(212) : error C2065: 'bullet_x' : undeclared identifier

1>.\SFML_Test.cpp(212) : error C2065: 'bullet_direction' : undeclared identifier

1>.\SFML_Test.cpp(213) : error C2065: 'bullet_y' : undeclared identifier

1>.\SFML_Test.cpp(213) : error C2065: 'bullet_x' : undeclared identifier

1>.\SFML_Test.cpp(213) : error C2065: 'bullet_direction' : undeclared identifier

1>.\SFML_Test.cpp(215) : error C2065: 'bullet_x' : undeclared identifier

1>.\SFML_Test.cpp(215) : error C2065: 'bullet_y' : undeclared identifier

1>.\SFML_Test.cpp(216) : error C2065: 'bullet_color_r' : undeclared identifier

1>.\SFML_Test.cpp(216) : error C2065: 'bullet_color_g' : undeclared identifier

1>.\SFML_Test.cpp(216) : error C2065: 'bullet_color_b' : undeclared identifier

Title + above should be enough for you to understand my problem. I declare all those arrays in the function bullet_create, but int main() doesn't know that. Also, am I declaring this stuff right? It seems like there is something major-ly wrong with my code and I don't know what.

Edited by Samuka97, 02 November 2010 - 05:23 AM.
Telling people in the title that this is already solved.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you declare a variable in a function, it ceases to exist once that function ends unless you pass it back to the calling function as a return variable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You can create a "bullet" structure or class and apply the function to it, calling it in int main.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts
How do I do that? I'm new to C++, just know the basics.
Edit: After a few minutes of searching:
struct bullet {

		int x;

		int y;

		double speed;

		double direction;

		int color_r;

		int color_g;

		int color_b;

	} bullet_instance[10000];

But what now? How do I make my bullet creation and update functions actually use that structure?
Edit 2: Took me about half an hour, but I finally got it. However, I'm having another problem now... my drawing loop isn't working as it should. Do I make a new topic or do I ask in this one? My code is quite big, so I think making a new one would be better...

Edited by Samuka97, 01 November 2010 - 06:52 AM.


#5
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Yes please send us some part of code so we could understand what problem you're facing.

Munir

#6
vinodtakarkhede

vinodtakarkhede

    Newbie

  • Members
  • Pip
  • 3 posts
Mentioned errors are due to missing header file.Include "SFML_Test.h" file.

#7
Samuka97

Samuka97

    Newbie

  • Members
  • PipPip
  • 15 posts

vinodtakarkhede said:

Mentioned errors are due to missing header file.Include "SFML_Test.h" file.
...wha? I didn't even include SFML_Test.h in my source code, and now that I am using a struct for the bullets, I only have a logical problem with my drawing loop. The drawing loop:
		// Enter the bullet update loop

		int i = 0;

		for (i=0;i<=bullet_count;i++)

		{

			// Calculate the bullet's trajectory

			//bullet_instance[i].x = bullet_instance[i].x - sin(bullet_instance[i].direction*pi/180);

			//bullet_instance[i].y = bullet_instance[i].y - cos(bullet_instance[i].direction*pi/180);


			// And update it accordingly.

			BulletSpr.SetPosition(bullet_instance[i].x, bullet_instance[i].y);

			BulletBackSpr.SetPosition(bullet_instance[i].x, bullet_instance[i].y);


			// Color the bullet...

			BulletSpr.SetColor(sf::Color(bullet_instance[i].color_r, bullet_instance[i].color_g, bullet_instance[i].color_b, bullet_instance[i].color_a));

			

			// And finally, draw it.

			App.Draw(BulletBackSpr);

			App.Draw(BulletSpr);

		}


		i = 0;


        // Display window contents on screen

        App.Display();
bullet_create + bullet structure:
////////////////////////////////////////////////////////////

// Structures

////////////////////////////////////////////////////////////

struct bullet {

		int x;

		int y;

		double speed;

		double direction;

		int color_r;

		int color_g;

		int color_b;

		int color_a;

	} bullet_instance[10000];


////////////////////////////////////////////////////////////

// Custom Functions

////////////////////////////////////////////////////////////

int create_bullet(int bullet_count, int x, int y, double speed, double direction, int color_r, int color_g, int color_b, int color_a)

{

	int create_id = bullet_count + 1;


	// Position of the bullet

	bullet_instance[create_id].x = x;

	bullet_instance[create_id].y = y;

	bullet_instance[create_id].speed = speed;

	bullet_instance[create_id].direction = direction;

	bullet_instance[create_id].color_r = color_r;

	bullet_instance[create_id].color_g = color_g;

	bullet_instance[create_id].color_b = color_b;

	bullet_instance[create_id].color_a = color_a;


	// We've got one extra bullet now,

	// don't forget to add 1 extra after creation!


	// Create suceful!

	return 1;

}

The problem is that my drawing loop draws the same bullet around 17-25 times for no reasons... at least not obvious ones.
Edit: Just to show you guys. I changed 1 line of code to be like this:
BulletSpr.SetPosition(bullet_instance[i].x+i*6, bullet_instance[i].y);
(I just added the +i*6 to the original code, everything mentioned above is still here) And it draws this:
Posted Image
Edit: I placed the bullet creation code inside the game's main loop. Oh my, that was dumb.

Edited by Samuka97, 02 November 2010 - 05:22 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users