• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Xanather

Array of structs

4 posts in this topic

A struct in C# is a value type.

For a 2D tile based game I am developing I just made a 2D array of tile.cs structs, this tile struct contains basic tile information (i.e. tile type, draw frame, etc...). I thought making a array of structs for tile data would be better than creating a array of class types (as structs do not need a memory reference and are allocated on the stack). I think using structs did indeed lower memory usage aswell.

The problem I have is, structs can act very differently to classes sometimes, and it just confuses me. Say I have a x int value in a struct, and I want to incrementt that x value by 1. Typing struct[index].x++ just does not let me? Why??

I have a feeling that it may be because of the following quote from MSDN on structs:

[quote]Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.[/quote]

What do other programmers think? Use structs or classes in this case. The size of the array that I am making is 16000x4000, so very large.

All replies are appriciated, thanks.
Xanather.
0

Share this post


Link to post
Share on other sites
How about a class which wraps an array of your structs? Then you could pass that to methods by reference and you could access the underlying struct array. There shouldn't be much difference using structs or classes for your tiles, as long as the classes are simple (no virtual methods, etc..) but I don't know much about how classes work in C#. Perhaps there is a lot of overhead in creating and managing a class relative to a simple struct.

Is it a bottleneck?
0

Share this post


Link to post
Share on other sites
I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

[quote]
Do not define a structure unless the type has all of the following characteristics:[list=1]
[*]It logically represents a single value, similar to primitive types (integer, double, and so on).
[*]It has an instance size smaller than 16 bytes.
[*]It is immutable.
[*]It will not have to be boxed frequently.
[/list]
[/quote]

I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct, except in the very rare case where your struct instance is a local variable.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them. I've *never* needed to use structs to address performance issues, and misuse of structs can actually hurt performance compared to classes.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches. Edited by Nypyren
0

Share this post


Link to post
Share on other sites
You need to pass any variable you want a function to change in C# with a ref tag, this must also be added to the function definition to work. This way you will tell C# to actually pass the parameter by reference instead of by value. See the example below.
[code]
class Program
{
public struct POD
{
public int m_value;
}
static void modifyStructsByRef(ref POD[] structs)
{
for (int counter = 0; counter < structs.Length;++counter )
{
structs[counter].m_value++;
}
}
static void modifyStructsByValue(POD[] structs)
{
for (int counter = 0; counter < structs.Length; ++counter)
{
structs[counter].m_value++;
}
}
static void Main(string[] args)
{
POD[] structs = new POD[4];
for (int counter = 0; counter < structs.Length; ++counter)
{
structs[counter].m_value = counter;
}
modifyStructsByValue(structs);
for (int counter = 0; counter < structs.Length; ++counter)
{
Console.WriteLine(structs[counter].m_value);
}
modifyStructsByRef(ref structs);
for (int counter = 0; counter < structs.Length; ++counter)
{
Console.WriteLine(structs[counter].m_value);
}
Console.ReadKey();
}
[/code]
0

Share this post


Link to post
Share on other sites
Thanks for all the posts.
@Nypyren - that link was very helpful, as so was your post :). I dont know why my C# 4.0 book didnt go into details about this. After learning about such things I am sure as heck just going to use a class lol.

Thanks again,
Xanather.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0