Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 20 May 2009
Offline Last Active Sep 11 2015 04:10 PM

#4963066 Preventing Chaos in your game code

Posted by on 25 July 2012 - 03:12 PM

I usually refactor when I see one of the following:

1 - I have the same code in mutliple places - a good sign that the common functionality needs to be refactored out into its own method or object
2 - I have a largish glob of complex or hard to read code - a good sign that I should seperate the logic into multiple methods/classes.
3 - I keep having to tack on extra checks or variables to handle corner-cases, or the code is beginning to feel "hackish" - this is usually a sign that I need to rethink my design on a higher level.

I don't necessarily see any of this in your posted code, although these rules are somewhat subjective anyway. There may be idiomatic C++ concerns as well that I am not qualified to comment on.

As the complexity grows, you may find that you want to isolate your input handling code, or message dispatch, etc. Personally I try and avoid long if-else-else-else... chains if possible, they tend to confuse me when revisiting code later, YMMV.

An easy trap (for me, anyway) to fall into is to try overengineering too early - I may feel clever with the results at first, but later humbled when I find that I need my design to go a completely different direction anyway.

#4960771 2D level editor, what to do..?

Posted by on 18 July 2012 - 08:13 PM

I can only speak from personal experience, and I should say that I've never developed as part of a team, so not sure how applicable any of this would be in your situation.

In past projects, there are 3 routes I've gone for producing maps, each with their own pros/cons:

1. Use existing tile map editing software (Tiled, tIDE, and Mappy)
Pros - already built, generally easy and quick to get content ready
Cons - inevitably, there will be features or editing tools that I want or need, and need to either repurpose a seperate tool or use other workarounds

2. Build custom editor
Pros - will have the exact features and editing tools needed, can be integrated very tightly with the game engine so that level creation to testing can be quick
Cons- Not trivial to create, a completely separate project to maintain, engine and game must be co-developed along with editor to keep components and features in-sync

3. Repurpose other tools for the task, such as using an image editing program, where each pixel or small group of pixels represents a cell's data
Pros - somewhat quick to get content ready, can be used when there really is no existing tool for the job but I don't want to develop a custom one
Cons - still need to develop a solution to convert between bitmap data and game data, using a tool for what it is not designed for can be ineffecient and difficult

Really I've only used 3 when my requirements were unusual and I had no other good option.

So basically I'm wondering how to go about this. I've been talking a bit with a programmer about coding our own level editor / framework kind of thing, with the features we need, but I'm really puzzled that level editing frameworks of with this kind of features are nowhere to be found, nor even mention about how to go about building them, if it is so that everyone builds this kind of thing to make their larger scale games?

Existing tools are generalized, but games will likely have their own specific requirements and corner cases. I suppose it is like anything else, a tradeoff - will you save time, in the end, by developing a custom tool? Or does the tool development time overshadow the difficulties of working with existing tools?

I would like a solution in which it is possible to actually view maps in a sheet like this while doing the editing.

For very large arrays of large maps, this becomes an excercise in optimization. I'm sure it can be done, but it likely would not be trivial, though I agree it would sometimes be nice to have this feature.

Also, never tried this but it may be possible to create one huge map using Tiled (or similar), and just section or grid areas off as "rooms" - assuming of course you stay within the sane constraints of your hardware. You may need an intermediary step to chop this super-map into various rooms for use as final content.

#4960621 passing a value from one class to another

Posted by on 18 July 2012 - 01:48 PM

the problem is that after the enemy die, EnemyKilled variable keep going up. it should be equal to 1 when enemy die, but it goes 1, 2, 3, 10000 etc ...

I have a feeling the answer is hidden right there. What is happening to make the killed counter keep increasing? When shouldn't the counter increase? Is there something else that can be checked, in addition to the collision itself, before increasing the counter?

This is what I think you are looking to do (spoiler tags in case you want to try and work through it yourself first):

#4960609 passing a value from one class to another

Posted by on 18 July 2012 - 01:14 PM

Ok can you please give me an example where I should be using inhertance?
My professor explained it to me like this.
A car is a class, and it has many parts. A car can inherit from class wheel and class engine and class light etc...

is that right? guessing from what you just told me, then thats wrong.

Far be it from to argue with a professor, but IMO I do not think this is the intention of inheritance. Using analogies to describe inheritance is always on shaky ground, but to use your example I would say:

A car IS A vehicle.
A bicycle IS A vehicle.
A vehicle HAS one or more wheels.
A car HAS an engine.

This would set up the hypothetical relationship as follows:
class Vehicle
   Wheel[] wheels;

class Bicycle : Vehicle

class Car : Vehicle
  Engine engine;

I have a feeling that perhaps the professor was misunderstood, or that I am not getting the complete story, or perhaps he/she is simplifying things for now to clarify later. You could always ask your professor for clarification on the matter.

You can also read up the subject, searching for "inheritance vs. composition" will net many hits, for example this one: http://www.codeproje...-VS-Inheritance

#4960565 passing a value from one class to another

Posted by on 18 July 2012 - 11:42 AM

Typically you pass the already constructed Map object in the Enemy constructor and store the reference in a private field.

On a side note, I'm not sure why Map class inherits from Tile - this looks like possibly a better fit for composition rather than inhertiance. Inheritance is often described as a "Is A" relationship, whereas composition is described as "Has A". In my mind, "A Map is a Tile" makes less sense than "A Map has Tiles".

Also, when designing a class, think about what it should "know" or express as data. Does each tile need to know it's position in the map? They might, but if we change around the relationship between maps and tiles as above, they may not:

(pseudo code)
class Tile
  public int Width{get;}
  public int Height{get;}

class Map
  private Tile[,] tiles = new Tile[width, height];
  public Rectangle GetTileRectangle(int posX, int posY)
	 Tile currentTile = tiles[posX, posY];

	 return new Rectangle(posX, posY, posX + currentTile.Width, posY + currentTile.Height);

class Enemy
  private Map _map;

  public Enemy(Map map)
	_map = map;

I'm not sure if this is what you are intending (since I don't know the overall design of your game), but the above makes more immediate sense, IMO.

#4960467 2D rpg, active or not active, AI-unit handling

Posted by on 18 July 2012 - 07:20 AM

Two options spring to my mind:

1. In each cell, where you store tile info, you could also store a reference (or handle/key) to agent(s) that are at that location. Of course, you would have to update this information in the cells as each agent moves.

2. A smarter collection for storing agents. For example, instead of a list, you could group agents by region, or use a sparse 2d array, or similarly use a dictionary and key (hash) them by coordinate value*. It may take some experimentation to see which data structure provides the needed efficiency.

EDIT: *note that this may require frequent insertions/removals, so depending on how often agents are moved this may or may not be viable.

From the infomation you provided, I would think #1 would be the simplist to implement.

#4959729 Save State and Loading Advice

Posted by on 16 July 2012 - 02:36 PM

I haven't done much with Java, so I am only guessing here, but I'd look through the standard lib docs for things such as "binary serialization", "binary IO", "binary streams" or "binary files". I'd be suprised if Java didn't include some high-level methods for simplifying this. Maybe someone here with more Java experience can comment.

#4959664 Save State and Loading Advice

Posted by on 16 July 2012 - 11:31 AM

Text file are generally more inefficient in speed and memory footprint. The simple reason is that you are storing extra (unneeded) data. It then needs to be parsed when deserialized. The only benefit to text files is that they are human-readable.

So, if you do not need the files to be human readable, what you are looking for is binary storage. Depending on the language/framework/libraries you are using, the specific implementation will vary. If memory footprint of the file is a concern, you can also use compression, but you will generally trade some speed in the process.

#4957096 [help] how should I save objects?

Posted by on 08 July 2012 - 07:17 PM

I may have been misunderstanding what I read, but the concern was if I depend on reflection of the assembly, all method execution would be slower,

I don't know anything about this, I'm no .Net expert but AFAIK the framework is pretty good about lazily loading metadata and caches as its needed. The compact framework or others may have tighter constraints on this stuff, though.

if its really a non-issue / insignificant as you suggest then I guess I can continue as I am.

That is my suggestion - worse case, I am wrong, and the reflection code turns out to be an issue (after testing/profiling). Then there are ways to go back and optimize (e.g. smarter caching, caching reflection method calls to delagates, putting reflection lookups into dictionaries, etc.), but at least you won't have wasted time doing all this for no reason.

good luck!

#4957036 Help with xna textures

Posted by on 08 July 2012 - 03:41 PM

Just a guess, but maybe a look here might be in order: http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

#4956966 [help] how should I save objects?

Posted by on 08 July 2012 - 11:05 AM

I'd read using reflection to look through the assembly requires a bunch of extra meta-data be produces and that it can make method execution 4 times slower because of this.
is this wrong? or insignificant?
Also I intend to load the game objects on the go via chunks, so the levels can be fairly large.

Sure, reflection code is slower than non-reflection code (because it has to do a few extra steps), but I am still skeptical it would matter at all in this case, since the IO time will vastly overwhelm everything else. Reflection isn't that slow, after all.


Let's take a completely contrived example, with completely contrived values:

You have your reflection-based object creation system, and it is taking 20 ticks of time. You spend a bunch of time optimizing it, replacing the simple, readable & maintainable code with hackish, brittle code, and now you have a 5 tick system. Great, but the creation system is being called in an outer loading method that is taking 1000 ticks on its own (due to disk IO). Congratulations, you've just increased performance by less than 0.1%. This is why you always see people harping on and on about "profile first, then optimize", since things like "4 times slower!" can be very misleading in the big picture.

#4956831 [help] how should I save objects?

Posted by on 07 July 2012 - 11:32 PM

When loading I need a way to tell what a component is, so I can run that components loading constructor wherein it builds itself with the data following its identifier, then it'll go onto the next component, check what it is, run its loading constructor, etc. etc. etc.

Can't you store the typename along with the object data, then use Activator.CreateInstance to create the objects?

Whenever I look up reflection I always find something about how it brings very notable overhead and is a practice to avoid

If you are loading objects from disk inside your inner loop (60 times a second), I would think the IO would be the bottleneck, not reflection. Also, why would anyone suggest avoiding something as powerful and handy as reflection? Like all the other supposed performance killers (linq, multi-dimensional arrays, regex, polymorphism, etc.) its probably best to use what works well first before handicapping yourself with premature optimization. Especially if you are developing for PC, which I've found can handle an obscene amount of "overhead" Posted Image

#4955441 procedural map generator for sidescroller

Posted by on 03 July 2012 - 02:22 PM

Not sure if it exactly what you are looking for, but Gamasutra had an article on procedural platformer levels: http://www.gamasutra.com/view/feature/170049/how_to_make_insane_procedural_.php

#4954574 reading data file c#

Posted by on 01 July 2012 - 11:04 AM

However, i have come across a problem. I am trying to create a more open ended way of creating monsters for the game by the game reading .txt files.

You could look into something like JSON, there are a couple of libraries available, such as http://json.codeplex.com/. There are also built-in libraries for XML parsing in .NET, but XML is a bit more complicated and probably overkill (IMO).

#4954109 Building a calculator in C#, stuck on doing the math stuff.

Posted by on 29 June 2012 - 03:49 PM

Building an expression parser, using Winforms, is not what I would consider a "beginner project to learn C#".

My advice: start with console applications for now, trying to learn Winforms + C# + general programming all at once is a bit much.

As Telastyn said, make a console application that takes input from the user and adds them together first.