• 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
Capoeirista

C# List<T>.Find

34 posts in this topic

Hey folks,
Reasonably new to C# here, and was wondering if you could point out what I'm doing wrong here :

[CODE]
class ObjectManager
{
public ObjectInstance GetObject<ObjectInstance>( int objectId )
{
if( m_objects.ContainsKey(typeof(ObjectInsance) )
{
List<EngineObject> objectList = m_objects[typeof(ObjectInstance)];
// This doesn't compile
EngineObject obj = objectList.Find( i => i.ID == objectId );
}
}
...
Dictionary<Type, List<EngineObject>> m_objects;
}
[/CODE]

I'm trying to return an EngineObject instance given an ID. EngineObject contains the ID property, but compiling generates the following error :

Error 2 'System.Collections.Generic.List<Engine.Objects.EngineObject>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Collections.Generic.List<Engine.Objects.EngineObject>' could be found (are you missing a using directive or an assembly reference?) C:\Code\Outskirts\Engine\Managers\ObjectManager.cs 64

Any ideas what I'm doing wrong here? Edited by Capoeirista
0

Share this post


Link to post
Share on other sites
this line is wrong:
[code]
List<EngineObject> objectList = m_objects[typeof(ObjectInstance);
[/code]
try adding a "]" behind the typeOf(ObjectInstance) Edited by NightCreature83
1

Share this post


Link to post
Share on other sites
[quote name='NightCreature83' timestamp='1347885909' post='4980857']
this line is wrong:
try adding a "]" behind the typeOf(ObjectInstance)
[/quote]
That was just a typo for this post :)

I can get the List<EngineObject> instance without problems - and can iterate through it to find the object with the supplied ID... just can't seem to do it using a delegate for the Find operation. EngineObject is abstract, but changing that to a standard class definition didn't work either.
0

Share this post


Link to post
Share on other sites
Does it work if you extract the lambda you're passing into Find() into a separate function? The error message makes it seem like it's complaining that you're passing it a List<EngineObject> instead of a Predicate<EngineObject>.
1

Share this post


Link to post
Share on other sites
Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..
1

Share this post


Link to post
Share on other sites
Wild guess, but do you have the required using statements?

[code]
using System.Collections.Generic;
using System.Linq;
[/code]
1

Share this post


Link to post
Share on other sites
Is that the only error you are getting while compiling? I see its "Error 2" there, there doesnt seem to be anything wrong with that piece of code, even tho it doesnt compile and show an error there, it can be just a consequence of an error somewhere else.
1

Share this post


Link to post
Share on other sites
[quote name='CyberRascal' timestamp='1347899238' post='4980929']
Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..
[/quote]
Yeah seems like it should to me too. What's the LINQ extension?

No other variables in scope... this is the complete function :

[CODE]
/// <summary>
/// Returns an Object of the supplied type, given it's id
/// </summary>
public T GetObject<T>( int objectId )
{
if( m_objectInstances.ContainsKey(typeof(T)) )
{
List<EngineObject> objectList = m_objectInstances[typeof(T)];
objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );
/*
for( int i = 0; i < objectList.Count; i++ )
{
if( objectList[i].ID == objectId )
{
return (T)((object)objectList[i]);
}
}*/
}
return default(T);
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='laztrezort' timestamp='1347900977' post='4980942']
Wild guess, but do you have the required using statements?

[code]
using System.Collections.Generic;
using System.Linq;
[/code]
[/quote]

Thanks for the idea - I had Collections.Generic, but not System.Linq... doesn't seem to make any difference though :

[CODE]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='Arthur Souza' timestamp='1347904825' post='4980965']
Is that the only error you are getting while compiling? I see its "Error 2" there, there doesnt seem to be anything wrong with that piece of code, even tho it doesnt compile and show an error there, it can be just a consequence of an error somewhere else.
[/quote]
Yeah that's the only error there is now. I think the second error was the same as the first because it came up twice... the code is in a project that builds to a static library, so one error for the lib, and one for the other project that uses the lib.
0

Share this post


Link to post
Share on other sites
[quote name='Oberon_Command' timestamp='1347897952' post='4980922']
Does it work if you extract the lambda you're passing into Find() into a separate function? The error message makes it seem like it's complaining that you're passing it a List<EngineObject> instead of a Predicate<EngineObject>.
[/quote]
Tried adding a method to my class to use for searching, but I get the exact same error back
0

Share this post


Link to post
Share on other sites
[quote name='CyberRascal' timestamp='1347899238' post='4980929']
Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..
[/quote]
I looked up how to use LINQ, and the following does work :
[CODE]
/// <summary>
/// Returns an Object of the supplied type, given it's id
/// </summary>
public T GetObject<T>( int objectId )
{
if( m_objectInstances.ContainsKey(typeof(T)) )
{
List<EngineObject> objectList = m_objectInstances[typeof(T)];
var result = from obj in objectList where obj.ID == objectId select obj;
/*
for( int i = 0; i < objectList.Count; i++ )
{
if( objectList[i].ID == objectId )
{
return (T)((object)objectList[i]);
}
}
*/
}
return default(T);
}
[/CODE]

There are obviously a couple of alternate approaches I can use now, but this Find problem has become something of an academic question [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Capoeirista
0

Share this post


Link to post
Share on other sites
[quote name='Capoeirista' timestamp='1347913255' post='4981007']
I looked up how to use LINQ, and the following does work :
[/quote]

That method will always return default(T).

But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?

[CODE]
public T GetObject<T>(int objectId) where T : EngineObject
{
Dictionary<int, EngineObject> objects;
if (m_objectInstances.TryGetValue(typeof(T), out objects))
return (T)objects[objectId];
else
return default(T);
}
private Dictionary<Type, Dictionary<int, EngineObject>> m_objectInstances;
[/CODE]
1

Share this post


Link to post
Share on other sites
objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );

This also does not work?

[quote name='lwm' timestamp='1347916064' post='4981022']
[quote name='Capoeirista' timestamp='1347913255' post='4981007']
I looked up how to use LINQ, and the following does work :
[/quote]
That method will always return default(T).
But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?
[CODE]
public T GetObject<T>(int objectId) where T : EngineObject
{
Dictionary<int, EngineObject> objects;
if (m_objectInstances.TryGetValue(typeof(T), out objects))
return (T)objects[objectId];
else
return default(T);
}
private Dictionary<Type, Dictionary<int, EngineObject>> m_objectInstances;
[/CODE]
[/quote]
I think he knows that will only return Default(T), as he commented the section that was actually going to do something to test the part of the code where he's having issues. Edited by Arthur Souza
1

Share this post


Link to post
Share on other sites
[quote name='Arthur Souza' timestamp='1347916102' post='4981023']
objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );

This also does not work?
[/quote]
No that doesn't work - I get the same compile error as :

[CODE]
objectList.Find( i => i.ID == objectId );
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='lwm' timestamp='1347916064' post='4981022']
That method will always return default(T).

But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?
[/quote]
Hah hah, yeah - I was just trying to test out the compiling... that one won't make it through to a commit :)

I could use a dictionary for the object ids as well, but to start with I'd like to figure out why the Find operation is being so problematic.

Cheers!
0

Share this post


Link to post
Share on other sites
Soo... I tried pasting the code you have supplied in a new XNA project in Visual Studio 2010. Compiles fine. Then I did the same in VS2008. No errors there either. It seems like the errors is somewhere else in the code. What compiler are you using? If you don't mind, maybe you can zip the project and attach it to a post here so we can take a look [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
I'm glad you found a working solution. If you want to use the Linq extension methods on collections you need to have the "using System.Linq;" line. It got me a bunch of times because it doesn't give you very useful compile errors. Personally I'm a fan of FirstOrDefault<T>, it allows a predicate and also works for empty collections.
1

Share this post


Link to post
Share on other sites
Yes, what I meant with the "First" LINQ extension was actually the method CALLED First:


var y = myList.First(x => x.ID == id);

or

var y = myList.FirstOrDefault(x => x.ID == id);

This is getting weird... Have you done a clean solution and then a rebuild solution?
1

Share this post


Link to post
Share on other sites
[quote name='CyberRascal' timestamp='1347970380' post='4981221']
Yes, what I meant with the "First" LINQ extension was actually the method CALLED First:


var y = myList.First(x => x.ID == id);

or

var y = myList.FirstOrDefault(x => x.ID == id);

This is getting weird... Have you done a clean solution and then a rebuild solution?
[/quote]
This just gets stranger :

[CODE]
EngineObject elObjectO = objectList.First( x => x.ID == objectId );
[/CODE]

Works perfectly, as does :

[CODE]
EngineObject elObjectO = objectList.FirstOrDefault( x => x.ID == objectId );
[/CODE]

Just not :

[CODE]
EngineObject elObjectO = objectList.Find( x => x.ID == objectId );
[/CODE]

I thought it might be something to do with EngineObject being abstract... but no.

And yeah, I've cleaned my solution and rebuilt... I also reboot my PC [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Capoeirista
0

Share this post


Link to post
Share on other sites
[quote name='Mizu' timestamp='1347966259' post='4981201']
Soo... I tried pasting the code you have supplied in a new XNA project in Visual Studio 2010. Compiles fine. Then I did the same in VS2008. No errors there either. It seems like the errors is somewhere else in the code. What compiler are you using? If you don't mind, maybe you can zip the project and attach it to a post here so we can take a look [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
I'd be happy to link to upload my engine project, but don't have a premium membership. I'm using VS2010. Edited by Capoeirista
0

Share this post


Link to post
Share on other sites
The "Find" function is a member of the List<T> class, the other functions are LINQ statements. These have different signatures (Find takes a Predicate<T>, where the LINQ functions take Func<T, bool>) and are implemented in completely different libraries / namespaces. This must be the root of the problem, but I don't have a compiler handy to investigate. When in doubt and using LINQ, hover over the function to see which signature it is trying to use and then find examples to match that (for instance look to an OLDER, .NET 2.0 example for using Find before LINQ instead of a newer 3.5 or 4.0 example).

I'm also not sure if the problem will be in some assumption the Predicate version makes about your class "EngineObject" or if it will be a simple issue with the syntax having to be slightly different. Good Luck.
1

Share this post


Link to post
Share on other sites
[quote name='Xai' timestamp='1347975954' post='4981246']
The "Find" function is a member of the List<T> class, the other functions are LINQ statements. These have different signatures (Find takes a Predicate<T>, where the LINQ functions take Func<T, bool>) and are implemented in completely different libraries / namespaces. This must be the root of the problem, but I don't have a compiler handy to investigate. When in doubt and using LINQ, hover over the function to see which signature it is trying to use and then find examples to match that (for instance look to an OLDER, .NET 2.0 example for using Find before LINQ instead of a newer 3.5 or 4.0 example).

I'm also not sure if the problem will be in some assumption the Predicate version makes about your class "EngineObject" or if it will be a simple issue with the syntax having to be slightly different. Good Luck.
[/quote]
Thanks for the advice - I had a look at the Predicate that the Find operation, and sure enough it's

[CODE]
Predicate<EngineObject> match
[/CODE]

I'll keep looking
0

Share this post


Link to post
Share on other sites
Oops, sorry.. didn't know you had to have premium membership to do that :)

Anyway... Here's a random wild guess: what happens if you write it like this:
[source lang="csharp"]Predicate<EngineObject> pred = i => i.ID == objectId;

EngineObject obj = objectList.Find( pred );[/source]
Is the errors still with the Find() function, or is it with the predicate?


This surely is weird.. My last advice is to create a new project, isolate your ObjectManager and EngineObject, copy those to the new project and try compiling. If it works, add stuff until it stops working. If it still doesn't work it should be small enough to post the code in code tags here.
1

Share this post


Link to post
Share on other sites
[quote name='Mizu' timestamp='1347981027' post='4981273']
This surely is weird.. My last advice is to create a new project, isolate your ObjectManager and EngineObject, copy those to the new project and try compiling. If it works, add stuff until it stops working. If it still doesn't work it should be small enough to post the code in code tags here.
[/quote]

Just tried this out :

[CODE]
Predicate<EngineObject> pred = i => i.ID == objectId;
objectList.Find( pred );
[/CODE]

aaaand the same error code... this is getting ridiculous :)
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