I'm trying to make a 2D game engine on my own

I'm trying to make a 2D game engine on my own.

How do I iterate through every class and object in the game in an optimal way?

Other urls found in this thread:

gameprogrammingpatterns.com/
github.com/purpasmart96/pmd_test
twitter.com/SFWRedditGifs

Iterate through an array and have some function called performAction or something. But you should look into frameworks where smarter people have laid the groundwork for you.

Consider not doing that

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y;
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}

not hard desuuuuuuuuuuuu

>How do I iterate through every class and object in the game in an optimal way?
By not iterating through those that don't need to be iterated. Simple case you have static objects and then you have dynamic objects. Every frame, you only update the positions of those objects that can move.

sir are you developer sir? u know AMD product now very good for developer ... please buy AMD sir

Read the Game Engine Architecture book by Jason Gregory. It's very good

>2D game engine
>Q_rsqrt
Are you retarded? That's only useful for ray-casting in a 3D engine.

Not a game dev but other than this:
>By not iterating through those that don't need to be iterated. Simple case you have static objects and then you have dynamic objects. Every frame, you only update the positions of those objects that can move.
I'd try to add some sort of spatial ordering/hierarchy with e.g. a quad tree so that in case you only need to access objects in a certain area you can easily skip large parts of your data you are currently not interested in anyway.

Consider also asking

Maybe have the scripted objects have their own Update thread?

No offense OP but if you have to ask this question you are not ready to make your own game engine. However, that being said, for each type of object you want to iterate over, make sure the class has a static array. In the constructor add the object reference to the static array. That's literally it.

user was referring to an old meme. Karlie Kloss once supposedly tweeted guys code VS girls code, where the following code was written by the guys accordingly to Miss. Kloss.

>No offense OP but if you have to ask this question you are not ready to make your own game engine.
This is most likely a hobby project. He doesn't have to be a game engine theory expert already, he can just start, encounter problems and then fix them. That's a pretty normal process and also makes you think about solutions yourself instead of reading some textbook.
Also, you don't learn creating neat algorithms by reading textbooks but by thinking about solutions and applying them.

- Make a pool of interactive items, including map terrain (tiles, as I see you want to make a 2D platformer) and sprites. Each one of them has a hitbox class.
- Make a base object with a method to calculate collisions. This shit must be very efficient, as the method to calculate collisions will have a complexity of O(n^2), 60 times a second.
- Have all your map objects in an array.
- Each frame check for collisions. You don't have a workaround but to have a N^2/2 collision checks, N being the amount of collisions in the map.
- Depending on your object's properties you can run some actions, like decrease a sprite's health or not fall into oblivion.

>hacker code used to steal nude photos

Which one of you was that?

>giving a fuck
99% of the users use more cpu on botnets and those who don't won't see a difference.

It's code from quake, nerd.

Another suggestion, game programming patterns:
gameprogrammingpatterns.com/

That's not an old meme, and it's not what he was referring to. That code snippet is decades old

It's arguably one of the most important bits of programming inspiration in 3d gaming. At least if you like high framerates.

There is no optimal way because we don't know what kind of game you're making.
If you're just writing an engine, try to avoid making any assumptions about the final product.

Trying to make the engine too open ended & expandable leads to project creep. I recommend adopting a client/server model to make adding to and editing the engine easier and starting with only what you need the engine to do.

Hmm.... are you sure you're up to the task?

I mean, you do know that objects are instances of classes, right?

To answer your question though, you don't. If you have to, then there are probably some serious design issues in your program.

LINKED.

LISTS.

>iterate through every class and object
how the fuck do you iterate a class? are you writing an interpreter?

It depends on the language you are using.

Use a big array, iterate through every object.

If that's too slow later on, refactor it into something else. A slow game is better than no game.

Consider that in most applications, you don't actually need to iterate through every static class and object in the game.
If you're trying to perform distance-relevant calculations (i.e. collision), Octrees if 3D. Quadtrees if 2D.

If you're trying to execute tasks once a certain condition is reached, consider performing an analysis of the problem instead.
Take the following situation: You are required to write a program that simulates the entire Port of Los Angeles (largest port in the US) performing an evacuation in the event of a nuclear strike or similar emergency. How long does it take for all of the trucks to leave the port?
A stupid way to do this would be to simulate every single truck moving a distance, stepping through time instances, and checking every entity to see whether they hit a stop light (because they are still required to follow traffic lights given an evacuation.)

A smart way to do this would be to realize that all you're really doing is waiting for the trucks to hit a stop light so you can calculate the changes in traffic. Trucks move in a known pattern (they will not be changing positions due to the rules of traffic), so you know that they will inevitably hit a traffic light at a certain time. So why not just fire an event when a truck hits a stop light, and that's it?

There are very few situations where you would want to iterate through every static class and object in existence, and your situation probably isn't one of them.

Not Op, but I've working on this for an while as an hobby, problem is that there isn't that much tutorials for 2d game development using modern open gl.
github.com/purpasmart96/pmd_test

Or rather, you could put the trucks in a min-heap of how close they are to a stop-light and use that as a trigger.

Only update when the object changes, duh.

>iterate through every class and object in the game
Do people seriously do this kind of shit?

Someone dumb-down the point of quadtrees for me?

Just quadranting off areas of the game to arbitrary depths to cut down on the number of collision comparisons that need doing?

This thread made me look up q_sqrt and I am in awe.

Only if you do it wrong. Look into component based design and event based messaging

Just put them in a list, a vector, any form of data structure with linear iteration speed, there are a million of them. And adding/removing Actors is going to occur so infrequently (compared to all other things a game engine is doing) it honestly doesn't even matter which one you pick.

Of course. If you call the police and tell them a crime has been committed they're going to want to know WHERE the crime was committed so they can look just in that area of the city instead of searching everywhere at once.

That's not the problem, the problem will be dynamically loading / unloading objects so that objects that are far away are not updated.