Jump to content

  • Log In with Google      Sign In   
  • Create Account

laztrezort

Member Since 20 May 2009
Offline Last Active Oct 19 2014 04:58 PM

#4960609 passing a value from one class to another

Posted by laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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.

P.S.:

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 laztrezort 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 laztrezort 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 laztrezort 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 laztrezort 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.


#4953779 2D game, pursuit code Q's

Posted by laztrezort on 28 June 2012 - 04:31 PM

I'm currently working on a rogueish/nethack text game, I'm somewhat stuck on how to implement the pursuit code (how a monster chases down your character). I somewhat took a stab at it and currently have the monster take your X,Y, and by using a series of subtractions/additions relative to it's own position, did manage to have it head towards your general direction quite consistently. But say it runs into a wall that's in it's way, should I code it somehow so that it knows it's looking at a wall and have it try to figure out how to walk around the wall to get to you? Or should I just make it cheat a little, and just record your keystrokes from the moment you start fleeing and just have the monster follow your footsteps exactly Posted Image ? Thanks for any info!


There are a couple ways this can be handled, depends on how you want to model your AI. If you want "perfect" AI, then you can use pathfinding (with A* or A-star being the common one).

If you want imperfect AI (or more realistic), you can try simulating tracking by scent and/or sound. There is an article here about this: http://roguebasin.roguelikedevelopment.org/index.php/Tracking_by_Scent_and_Sound

While I'm link roguebasin, here is another that might be relavent: http://roguebasin.roguelikedevelopment.org/index.php/The_Incredible_Power_of_Dijkstra_Maps


#4953315 When it comes to C# I am at a deadend when it comes to what to do.

Posted by laztrezort on 27 June 2012 - 06:38 AM

Practice, practice, practice. Choose a goal project, get it done. If it is too overwhelming, downsize your goal until you can finish it. Reading books and completing small excercises is fine to a point, but doing only this will not teach you how these specific items really fit together. You need to develop the "engineering" side of things, get comfortable with the larger picture.

As you keep building projects, you will notice that you keep using the same patterns, refactoring the same set of code. Polymorphism theory and practice (for example) can be explained in a book, but until you use it, get comfortable with it, notice how it simplifies and leads to more elegant code, over and over again in 20 different projects, you will not really grasp it.


#4951406 Tile size in a 2D Map

Posted by laztrezort on 21 June 2012 - 10:24 AM

This question gets asked alot, and there is no general good answer - it depends on a lot of factors. If you search for "tile size" on this site, you should get a number of good posts.

One suggestion I can put out there, though, is to design your tile engine to support any size of tile, so that if your art direction changes (and it probably will!), you can adjust accordingly without having to re-write a bunch of code.




PARTNERS