Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 03 May 2009
Offline Last Active Aug 29 2016 04:04 PM

#5244488 Collectable Items, Persistence and ECS Architectures

Posted by on 04 August 2015 - 08:03 AM

My current project is a 2D platformer where the whole game is made up of a series of "rooms" (like a level of Shovel Knight, but without a world map to split it up).


My rooms are currently defined like this (pseudo code):

room = {
    tilemap = "tilemap.tmx",
    init = {
         // Perform the initialisation of anything not in the tilemap, e.g. enemies and collectable items
    // Other irrelevant stuff

Each time you enter a room, the init code is run and all of the enemies/collectables you killed/picked up/whatever are all back in their original place. This is exactly how I want this to work.


At the moment saving the game is simple, I just serialize out the state of the player, which room they are on and that's it.


The problem I'm having is that I'm now at the point where I want to add one-off collectables (like the morph ball in the Metroid games) or one-off enemies (e.g. an extra hard one that spawns only once), once you've collected/defeated them, they are gone - and when you enter/re-enter a room they shouldn't be re-spawned along with everything else.


I'm struggling to come up with a concept to handle this without some kind of global manager to look after everything which feels a bit wrong to me.


I'm working with an ECS architecture (as a purely educational exercise, I don't want to go into the merits of ECS vs. non-ECS), and I know that I shouldn't try and force everything into it, but it feels like this is a problem that I should be able to solve with it.


As a rough stab at it I was thinking of the following:

  • a Persistence component which stores a key
  • a PersistenceSystem which keeps track of these keys and their state
  • when a Persistence component is added to an entity, if the PersistenceSystem says the entity shouldn't exist anymore, destroy it (this is to handle the case when I reload a room)
  • when I save the game I serialize the keys and their states from the PersistenceSystem along with my other data

Is this is a sensible approach? Is this something that should sit outside of the ECS? How have other people tackled this problem?

#5228346 Setting up CMake for SDL-GL

Posted by on 11 May 2015 - 06:46 AM

This is how I do this in my project. On Windows I populate CMAKE_PREFIX_PATH with the paths to the various library directories, and on OS X/Linux it "just works".

  # Tell CMake where to search for includes, libs and DLLs on Windows
find_package(OpenGL REQUIRED)
message(FATAL_ERROR "OpenGL not found!")
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})

find_package(GLEW REQUIRED)
message(FATAL_ERROR "GLEW not found!")
target_link_libraries(${PROJECT_NAME} ${GLEW_LIBRARY})

find_package(SDL2 REQUIRED)
message(FATAL_ERROR "SDL2 not found!")
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})


#5217084 Stuttering problem - first attempt at fixed timestep game loop

Posted by on 17 March 2015 - 09:07 AM

Have you tried making use of SDL_GetPerformanceCounter (alongside SDL_GetPerformanceFrequency) rather than SDL_GetTicks? It should give you a more accurate timing value.

#5197124 Which of these games would you like to read more about?

Posted by on 09 December 2014 - 04:00 AM

Based how on how great the series has been so far I'm happy to read about anything you're willing to write about! The first two parts have been fantastic and I'm looking forward to the third. Keep up the good work.

#5069116 SDL 2.0 undefined functions and constants problem

Posted by on 12 June 2013 - 06:47 AM

A lot of methods/constants have been changed/removed for SDL2. For example, SDL_GL_SwapBuffers() is now SDL_GL_SwapWindow().

You need to update your code to use the new SDL2 methods.

#5062032 MVC: Struggling to remove graphics from the model

Posted by on 15 May 2013 - 07:14 AM

For my latest (small) project I thought I'd try out a slightly different way of doing things, namely I've decided to try using MVC for a game.
I've got a reasonable handle on it in terms of observers and events in the model and communicating these to the view (and controller), however I'm having a bit of difficultly in figuring out how to completely eliminate data about the graphical representation from the model.
Take this (massively simple) example of a fairly basic RPG. Since my model is data-driven, I have a single "Unit" class which contains information about the units (no need for Troll, Goblin, etc subclasses).
class Unit
    string name; //Friendly name, e.g. Dave
    int hp;
    int damage;
int range;
    Unit(string name, int hp, int damage);

Then in my model I keep a list of these Units as well as a method to retrieve them so the view can access them.
class Model // LevelState or similar would be a better name for this
    Unit[] units;
    Unit[] GetUnits();
The problem occurs when I come to drawing the units, as far as my model is concerned it doesn't care about if the unit is a goblin, an archer or a giant salamader, all it cares about is the unit's stats which define it's behavior.
However, the view very much cares about if the unit is a goblin, an archer or a giant salamader as it will have to draw different sprites for each of them.
What is the best way to solve this? I could add an enumerated "type" field to the Unit class, but this feels wrong as it's polluting the model with data that it doesn't care about, but I really can't see any other way of doing this.
Am I just overthinking and getting too concerned with what is "right" and "clean"?

#5041848 XNA - Texture2D won't get rendered after ~10 frames.

Posted by on 11 March 2013 - 08:32 AM

I noticed you mentioned you removed some Dispose() calls. I can't think of any regular situation where you'd need to call Dispose on a Texture in XNA, so this rings alarms bells for me.


Could you post up some code or link to your repository? It might offer some clues to what's going on.

#5040817 How feasible is using the NDK?

Posted by on 08 March 2013 - 08:10 AM

I'm currently using the NDK with SDL2 (ex. 1.3) and it's been a piece of cake. Sure, the debuging options are limited, but so long as you setup some defines for logging (so you can use the logcat) and learn how to use gdb it's not too bad. It helps if you can help your project for a different platform (e.g. Linux or Windows) and debug on there, but this may not be possible depending on what you are doing.


I'm sharing code between Windows, OSX, Linux and Android (all non-NEON devices, as SDL doesn't build correctly for them) at the moment, using CMake to build all of the projects except the Android one - where I use the NDK tools on Linux, but I've tested Cygwin and that works too. Eventually I'll add iOS in the mix, but I thought I'd leave that for a while (this is probably a massive mistake) as I wanted to get on and actually start developing.


At some point I'll clear out all the project specific code and upload a blank project template to bitbucket or github, since it's taken a little bit of work to get it all working, but now it is all setup it's a doddle.

#5038011 How many draw calls per frame are possible without awful performance?

Posted by on 01 March 2013 - 08:02 AM

You could try sorting your tiles by texture before drawing them and seeing how that effects performance.


It would be helpful if you could post some of your drawing code (i.e. the bits that are called every frame) to see if there are any obvious issues in there that could be causing problems.

#5017099 Drawing a square in DX11

Posted by on 03 January 2013 - 07:54 AM

You also need to change the line that reads "d3d11DevCon->Draw(3,0);" so that it knows you want it to draw 6 vertices instead of 3.

#5009797 c# help

Posted by on 12 December 2012 - 06:35 AM

The post above is 100% correct, you really need to take the time to learn C# and it's syntax before going any further.

As for your code, I believe something along the lines of the following is what you were actually trying to achieve:

class Lexington
    private String weapon = "lazarus revolver";
    private String armor = "military uni";
    private int level = 70;

#5009067 Can't draw a simple cube

Posted by on 10 December 2012 - 07:24 AM

At no point are you drawing anything. Unless I've missed something there are no calls to any of the Device.Draw....() methods in your code.

EDIT: Just noticed you have your DrawPrimitives call commented out.

#4995820 error X3087: texture object does not have methods

Posted by on 31 October 2012 - 07:46 AM

You need to use a one of the Texture object types listed here:

Texture2D should work in this case (rather than just texture).

#4984701 SFINAE equivalent in C#?

Posted by on 28 September 2012 - 06:16 AM

I believe Marshal.SizeOf and the typeof() operator should do what you need, it's been a while since I've used it though and I'm not sat in front of a computer I can test this on, but hopefully the following should work.

int size = Marshal.SizeOf(typeof(T));

#4758226 New Forum FAQ

Posted by on 13 January 2011 - 04:29 AM

A big note saying "Post your code if you want better (any) decent help and/or feedback" would be a good start.

If specific DirectX/XNA things can be included then:

  • Debug runtimes setup "tutorial", for DirectX/SlimDX/XNA
  • PIX setup "tutorial", for DirectX/SlimDX/XNA
  • An explanation of the differences between DirectX, SlimDX and XNA