How to run functions in object array whose array number is below n

closed account (DEhqDjzh)
i am creating an game engine but now i have a problem. i want to create new object when user pressed button. but problem is when new object is being rendered the old objects must get rendered too. here is my loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

        GameObject texturetest; 
	GameObject sword;
	Gameobject test[12]; // avaiable objects
int a;
	
	
	while (e.eisrunning)
	{


		SDL_RenderClear(e.sr); /// render

if(buttonpressed)
{


test[a].init(true, false,e.sr,true, "Untitled.png"))

a++;

}
		// i can render like test[a].render() but i have 2 problem
//1. if a is null then it will give error
	//2. lets say we solved 1. problem but still it will render only current test object not the older ones
		SDL_RenderPresent(e.sr); /// render  ends
	//	
		
		e.handleevent();
		
		
	}
Last edited on
Create a vector of objects to render. Outside the game loop.

As you create objects that must be rendered, put them in the vector.

Each time inside the game loop, render everything in the vector.

When you want to add objects to be rendered, add them to the vector.

Because you created the vector outside the game loop, it will persist. It will not be destoyed each time round the loop. Objects you add to it will stay in it.
closed account (DEhqDjzh)
@Repeater I am sorry my English is not that good can you show it as code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector<GameObject> objects_to_render;

while ( game_is_running)  // ENTER GAME LOOP
{
     if ( want_to_make_a_new_object )   // e.g. by key press
    {
       GameObject new_object;
        objects_to_render.push_back(new_object);
     }


    for (int i = 0; i < objects_to_render.size() ; i++)
    {
          render ( objects_to_render[i] );
     }
}



Don't use arrays. Arrays are not for beginners.


test[a].init(true, false,e.sr,true, "Untitled.png"))
This looks very bad. This looks like your GameObject has an init function. This is bad bad bad. This is C style. You're using C++. In C++, we use constructors. It should be impossible for your GameObject to be created without having all these values set in them.

You need to read about the following:

vector
constructor
Last edited on
@Repeater Two-stage initialisation is very definitely a useful thing in C++, in the right circumstances.

See, frex:

https://stackoverflow.com/questions/3021572/two-phase-construction-in-c
Two-stage initialisation is very definitely a useful thing in C++, in the right circumstances.


That circumstance is "when we have no alternative". It's still bad. Just because we have no alternative doesn't make it a good thing.
Last edited on
closed account (DEhqDjzh)
@Repeater @MikeyBoy Do you recommend vector or array for item system in a game/game engine
std::vector

The list of reasons to use an array is very small indeed. Sometimes std::array (which is not the same an an array) instead of a std::vector, but as a rule of thumb, don't use arrays. Especially as a beginner. Arrays aren't for beginners.
Last edited on
closed account (DEhqDjzh)
@Repeater Hello again! I made some changes with my game engine and now it is C! and there is no Gamobject Class but gameobject struct. So how can I do that in c?
I think you misinterpret what they meant, they didn't mean for you to do it in C, but instead to use std::vector instead of pure arrays
so...I am sorry but change it back to C++...unless you want to deal with manual memory management ( I have found it to be...PITA )...
in C++, you can do this instead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    vector< int > vec;
for( int i = 0; i < 10; ++i ) {
    vec.push_back( 10 ); //adds elements to vector
}
//at the end of for, vec has 10 elements
if ( buttonpressed ) {
    vec.push_back( 10 );//add another element;
}
//and then iterate through iterate through iterate
/*
    example :
	for( int i = 0; i < vec.size(); ++i ) {
	    cout << vec.at( i );
	}
	this outputs all elements in vec 
*/


note : I usually use SFML the only time I have learnt anything about SDL is when I use D and want to know how to graphics in it, so I went for SDL instead of SFML because I thought I want to learn something new, ( I went back to SFML ), so my SDL knowledge is limited and I am pretty sure that e is something you made, right ?
So how can I do that in c?


First, switch to C++.
closed account (DEhqDjzh)
@Repeater @Flaze07 The Project must be in C. So is there any way to do it in C?
yes. one way is just make a linked list of pointers to the objects you want to draw.

another is to make a draw flag on the object in the array, iterate the array, and draw the ones that are set to true.

closed account (DEhqDjzh)
@jonnin Can you show it as code, please ?
pseudo code maybe. I am not that industrious.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct object
{
    int blah;
    double stuff;
    char drawme;  //does C have a bool yet?  I forget. 
};

object test[12];
… code to assign object values and such left out here

for(int x = 0; x < 12; x++)
 {
     if (test[x].drawme)
           drawfunction(test[x]);
 }



the list version is more trouble, but it isn't fixed at size 12 max either. If you need a bunch of them..

you can also wrap the above if 'object' was already defined and you don't want to change it.
struct wrapper
{
objectwithoutabool *o; //with a pointer, it can refer back to an existing one without copy or other nonsense very easily. it could even refer to several of them in an array, if you wanted to draw them all in chunks, eg a car made of 50 objects all drawn...
char drawme;
};

and the same ideas flow from there.


Last edited on
closed account (DEhqDjzh)
@jonnin thank you
Topic archived. No new replies allowed.