How to create "create new gameobject" algorithm

closed account (DEhqDjzh)
i have a gameobject class and what i want to do is when user pressed a button it creates a new gameobject while rendering existing gameobjects. Here is my main 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
GameObject texturetest;
GameObject sword;


texturetest.init(true, false,sr,true, "Untitled.png");
sword.init(true, false, sr, false, "mythicalsword.png");

while (gameisrunning)
{
SDL_RenderClear(sr); /// render

texturetest.render(sr, 45,12, 256, 256);
sword.render(sr, 12, -45, 256, 256);


SDL_RenderPresent(sr); /// render ends

// create game object code here


handleevent();


} 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
SDL_Event e;

while (SDL_PollEvent(&e) == 1)
{
   if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_c)
	      {
                    // user pressed 'c' button
                    GameObject someNewGameObject; // new GameObject created
          
                     // Do something with new object
               }		
}
Last edited on
closed account (DEhqDjzh)
@Repeater with this function can i create 4 different gameobjects ? (ship,horse,katana,cannon)
1
2
3
4
GameObject ship;
GameObject horse;
GameObject katana;
GameObject cannon;

Four different GameObjects. One named ship, one named horse, one named katana, one named cannon
closed account (DEhqDjzh)
@Repeater but the creation of ship,horse,katana,cannon will be when user presses a button so I cant add it in code. Check out http://www.cplusplus.com/forum/general/238988/ for more info.
@Repeater but the creation of ship,horse,katana,cannon will be when user presses a button so I cant add it in code.


That makes no sense. Code is all you have. If you don't add it in the code, there is nothing else.
Kicking a dead horse here, but anyways:

Listen, make sure you read all the relevant tutorials from lazyfoo, including the articles section. But remember, lazyfoo is great for leaving room for improvement and being as minimal as possible, but it isn't a very good tutorial when it comes to good programming techniques, and it is not scalable at all.

I would highly recommend learning how to make an object factory for what you are trying to do:
https://github.com/hungchicheng/DesignPattern/blob/master/C%2B%2B/Factory.cpp

You may even find some other Gang of Four patterns useful in the github repo. You will find these patterns frequently in code, and this repo doesn't even show all the possible patterns in GoF.

I would recommend books, but you have to do more research on your own for what you need, since you are a long trek away from making a well rounded engine.
Topic archived. No new replies allowed.