CodingRendle & C#

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Mikee      
35151.6 In reply to 35151.3 
One other thought on optimisation: you will be tempted to believe that arrays are better than List<T>'s, and that for loops are better than foreach. Neither of these things are true.

The List<T> class is heavily optimised and often performs better than an array. If you are returning a list of items from a method purely for iteration, use IEnumerable<T> as the return type and the yield keyword in the method:

C# code:
public IEnumerable<Vector3> GetVectors()
{
  Vector3 v;
  while ((v = MakeVector()) != null)
  {
    yield return v;
  }
}
 
...
 
foreach (Vector3 v in GetVectors())
{
  // do something
  if (condition) break;
}


The C# compiler optimises the shit out of this kind of thing. It's co-processed, so each call to MakeVector only happens as you iterate through the foreach loop. If you break out of the foreach, the IEnumerable is destroyed, so you don't make unnecessary calls to MakeVector, which you might if you populated an entire array in advance. Also, if you implemented parallelism at some point in the future, the operation could be split across multiple cores.

Twinkle, twinkle, little star, I don't wonder what you are:
You're the cooling down of gases, forming into solid masses.
0/0
 Reply   Quote More 

 From:  Mikee   
 To:  THERE IS NO GOD BUT (RENDLE)     
35151.7 In reply to 35151.6 

Hmm. Interesting stuff. I think I've done something similar in the past, but probably just due to copying and pasting someone elses work.

 

Thanks for your help.

0/0
 Reply   Quote More 

 From:  Mikee   
 To:  ALL
35151.8 
Ok, I've got another one for you if you could be so kind.

Basically, we've extended the XNA framework slightly and bastardized it slightly to suit our needs.

In XNA the main 'Game' class calls methods.. (Initialize(), LoadContent(), Update(), Draw()). It calls update and draw x times per second.

We've set things up so that you add Screens to the main game class, then you add components to screens. Components also have their own list of components and they have their own list of components and so on.

When Update() is called in the main Game class, it'll call Update() on all the screens, which'll call Update() on all the Components and their components..etc..

So, a small example would be like this:

code:
 
Game    Screen        Component    Component
------- ------------- ------------ ----------
 
Game -> 3dScreen   -> Sheep     -> AI
                                -> Render
                                -> Physics
 
                   -> Dog       -> Controller
                                -> Animation
     -> MenuScreen -> Setup
     -> HUDScreen  -> Radar
                   -> Healthbar
 


Although we're not at the stage of making any actual game logic just yet, I know that components will need to talk to eachother and/or their parent component.

In an example of an engine that i've seen, they use this big messaging system to talk between components/screens, whereby components (or screens) can listen to a certian messages and will receive all messages of that type.

This seems all complicated to me and using events and eventhandlers seems like the way to go.

Can you think of a better way to do any of this? I'm also concerned about speed.. for example, would a onPositionChanged event on model (which can fire 100 times a second) be too much?
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Mikee      
35151.9 In reply to 35151.8 
I would certainly start by using event handlers, rather than reinventing the wheel. If you try to implement your own system, you risk problems with objects not getting dereferenced and garbage collected.

Twinkle, twinkle, little star, I don't wonder what you are:
You're the cooling down of gases, forming into solid masses.
0/0
 Reply   Quote More 

Reply to All    
 

1–9

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats