XNA Creators Club Online
Page 2 of 2 (35 items) < Previous 1 2
Sort Posts: Previous Next

Useful XNA Framework tips

Last post 10-10-2008 3:48 PM by JasonD. 34 replies.
  • 11-10-2007 11:02 AM In reply to

    Useful XNA Framework tips

    Whenever i start a new codefile, alot of the time i find myself haveing to add all the xna refrences at the top, so i realised this would make an exilent snippit

    save the following xml as usingXNA.snippet

    - <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    - <CodeSnippet Format="1.0.0">
    - <Header>
      <Title>XNA Using statments</Title>
      <Author>NekoCake Industries</Author>
      <Description>Inserts all the using statments needed for good xna useage</Description>
      <HelpUrl>www.nekocake.com</HelpUrl>
    - <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Shortcut>UsingXNA</Shortcut>
      </Header>
    - <Snippet>
    - <Code Language="CSharp">
    <![CDATA[
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;
    #endregion
      ]]> f(clean);
      </Code>
      </Snippet>
      </CodeSnippet>
      </CodeSnippets>
     
    Then start up GSE, click tools -> code snippet manager -> add then find where you saved the xml, and load it in. whenever you need the xna refences, just type UsingXNA and press tab twice when intelesence picks it up!
  • 11-20-2007 6:26 AM In reply to

    Re: Useful XNA Framework tips

    I forget who mentioned it but someone said something about String.ToLower() being very ineffecient.

    Is this because the String object in c# is immutable? I know this is the case with Java so doing a toLower() will create a new String object. Is this the case in c# and the reason why this would be an inefficient thing to do in a tight loop or something? Not to mention also meaning the old String object has to be GC at some point?
  • 10-10-2008 2:34 AM In reply to

    Re: XNA Useful Facts

    Bapa:
    ...using "foreach" to render all of the meshes is a bad idea. This is because whenever you hit a foreach, it actually creates allocates a new object, which results in many, many unnecessary object allocations per second. If efficiency is a must, don't use foreach. Make your own way of looping...

    Bapa, I have lots of arrays of structs, for objects like particles and bullets, in my 2D shooter game.  I am stepping through them each frame using foreach.  Is this really making a new object each time that must be GC'ed?  I thought the CLR Profiler would show this, and that I'd have crazy GC slowdown as a result on the Xbox 360, which I don't see.  So, are you certain?  I would think such a commonly used language syntax as 'foreach' would not be so taxing on the system.  How should I be stepping through these arrays?  With an index?  I had assumed that'd be even slower than using 'foreach'.


    Ok wait... I just ran into Shawn's mention of:  Foreach, Garbage, and the CLR Profiler  Reading it now...  Ok, it appears that using 'foreach' with an array, if the collections are used explicitly, they will not create garbage.  I'm not sure what he means, but I believe this is what I'm doing.  I have arrays of basic structs, and I use 'foreach' to go through them.  Can anyone else confirm that this will not generate garbage?

    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
  • 10-10-2008 3:03 AM In reply to

    Re: XNA Useful Facts

    Foreach isn't really taking and it highly depends on what you are looping over as to whether or not it allocates. Even if it does allocate, I'd still argue that it's rather negligent. The GC on the Xbox isn't as good as it is on Windows, but I still think its effects are exaggerated. Pre-optimization is terrible. You are aware that it may allocate which means if it affects your game, you will have an idea of where to start. But since foreach does simplify looping over lists where you do not need the numerical index, the ease of use greatly outweighs the paranoia that it will somehow kill your game.


    Nick Gravelyn -- Microsoft XNA MVP
    Blog | Metacreature Games | Super Gravelyn Bros | Bloc | Next-Gen
  • 10-10-2008 3:46 AM In reply to

    Re: XNA Useful Facts

    JasonD:
    ...it appears that using 'foreach' with an array, if the collections are used explicitly, they will not create garbage.  I'm not sure what he means, but I believe this is what I'm doing.  I have arrays of basic structs, and I use 'foreach' to go through them...

    I've just run the CLR Profiler, and I see no allocations or GC invokes on any of the numerous foreach loops I am using, which interate through generic structs that hold plain old data.  So, foreach is safe in these cases.

    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
  • 10-10-2008 3:49 AM In reply to

    Re: XNA Useful Facts

    Nick Gravelyn:
    Foreach isn't really taking and it highly depends on what you are looping over as to whether or not it allocates. Even if it does allocate, I'd still argue that it's rather negligent. The GC on the Xbox isn't as good as it is on Windows, but I still think its effects are exaggerated. Pre-optimization is terrible. You are aware that it may allocate which means if it affects your game, you will have an idea of where to start. But since foreach does simplify looping over lists where you do not need the numerical index, the ease of use greatly outweighs the paranoia that it will somehow kill your game.

    I have a lot of bullets and particles in my game, so if each iteration through them, each frame, was creating garbage, then i'd be worried.  But, I was actually generate garbage by accident at 1,000x this rate, since I stupidly used a struct rather than a class for a simple plain 'ol data type, and even that craziness only caused a hiccup every 2 seconds in the game.  So, it appears even with Xbox 360's GC can handle quite a bit before performance becomes an issue.

    ...Ok, wait...

    I think I just realized that the garbage created by the foreach is not made per element but just once for the iterator, so this wouldn't be a big deal no matter how many more elements I am iterating through.

    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
  • 10-10-2008 3:53 AM In reply to

    Re: XNA Useful Facts

    Jamezila:
    ... I ... accidentally introduced about 4000ms of GC at least once a minute which had a substantial effect on gameplay--I had to run the Performance Monitor and CLR Profiler to find the culprit foreach calls. ...

    Jamezila, I'd love to know what the foreach calls were iterating through, to cause this GC mess.  Collections of reference values?

    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
  • 10-10-2008 4:11 AM In reply to

    Re: XNA Useful Facts

    leclerc9:
    The belief that "foreach" is slow is misguided in the majority of cases: http://blogs.msdn.com/brada/archive/2004/04/29/123105.aspx

    That page states:

    "The code gen is basically identical... guidance is to use foreach for clarity unless you plan to modify the contents of the array as you go, in which case for is required.  Enumerators are not created in the array case.  Note this analysis is not applicable to the collection class case, this is for arrays."

    However, I use foreach and I modify the contents of the array as I go, and the contents stay changed.  So, what gives?

    It seems to me that you aren't allowed to change the contents if the array is of a value type (the code won't even compile; you'll get a Compiler Error CS1656).  But, you can change the contents if it's a reference type.

    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
  • 10-10-2008 12:47 PM In reply to

    Re: XNA Useful Facts


    JasonD:

    leclerc9:
    The belief that "foreach" is slow is misguided in the majority of cases: http://blogs.msdn.com/brada/archive/2004/04/29/123105.aspx

    That page states:

    "The code gen is basically identical... guidance is to use foreach for clarity unless you plan to modify the contents of the array as you go, in which case for is required.  Enumerators are not created in the array case.  Note this analysis is not applicable to the collection class case, this is for arrays."

    However, I use foreach and I modify the contents of the array as I go, and the contents stay changed.  So, what gives?

    It seems to me that you aren't allowed to change the contents if the array is of a value type (the code won't even compile; you'll get a Compiler Error CS1656).  But, you can change the contents if it's a reference type.

     By change the contents, he probably means adding and removing items to/from the array/collection. With other collections it causes an exception. I am not sure about arrays. My guess it will cause an exception or will result in items being skipped, going past the bounds of the array, or other bad things.

     

  • 10-10-2008 3:48 PM In reply to

    Re: XNA Useful Facts

    cilcoder, ok, right.  With programming, you need to be very specific when you talk.  He was vague, and it threw me off.  I know what you mean though.  It's like changing the length of an array when you're iterating through it yourself.  You need to be aware that the index may now point to a new item, and you may have missed one in the process.  Thanks for the clarification.
    Jason Doucette / Xona Games
    Project: Duality: ZF (classic 2D shooter)
Page 2 of 2 (35 items) < Previous 1 2 Previous Next