TargetInvocationException on CreatePlayerAgent

Dec 11, 2012 at 10:04 PM
Edited Dec 11, 2012 at 10:04 PM

Good Evening

I wanted to create a player agent. I tried to use the same code as in the AoS source, but I keep getting a TargetInvocationException with InnerException: {"Collection is read-only."}.

What could be the reason for this?

I'm working on a pc-only-game and use a local session.

private void OnSessionStarted(object sender, EventArgs e)
	player = SessionManager.CurrentSession.CreatePlayerAgent<Player>(SessionManager.GetIdentifiedPlayer(LogicalPlayerIndex.One));
public PlayGameState(Application application) : base("Game", application) {
	SessionManager.CurrentSession.Started += OnSessionStarted;

Tell me if you need more code to say something about it, would just be a bit much to search everything session-related (I don't know where else I should look for the problem)...

Thanks in advance

Dec 12, 2012 at 8:57 AM

Hi Leo,

In order to help you out, I'd need to know where exactly the exception is raised as well as its StackTrace. From there I'd be able to see what is wrong.

Moreover, It could be helpful to see how you create your SessionManager Local session and which version of IGF you're using.



Dec 12, 2012 at 4:24 PM
Edited Dec 13, 2012 at 3:02 PM

Thanks for your fast answer!

I'm using IGF with Sunburn and of course XNA 4.0.

Well, I just found out what the problem is. The player identification never ends.
I used almost the same code as in the AoS source/tutorial, but I wasn't aware the player identification happens on the PressStartLayer.
I'm developing a windows game, so I don't want a "press start"-screen like this. I want the player to land immediately in the main menu.

Maybe I'm just too stupid and didn't understand how the sessions work in IGF, but is there a way to identify a player manually?


I think I got it, found "SessionManager.IdentifyPlayer()", but I have to try first when I'm at home...

Dec 13, 2012 at 5:24 PM

Hi Leo,

You are right, the SessionManager.IdentifyPlayer() is exactly the method you're looking for ;)

I made this method for such cases where you don't want to use a Start screen.

Note however that it is a best practice to start your game with such screen because it'll let you see if the player has other controls available.



Dec 14, 2012 at 7:16 AM
Edited Dec 14, 2012 at 7:17 AM
indiefreaks wrote:

Note however that it is a best practice to start your game with such screen because it'll let you see if the player has other controls available.

Ah, nice to know for my future projects.

The IdentifyPlayer worked for me, thanks!

The TargetInvocationException was btw not related to a session problem. It seems I just wasn't careful enough when I went through the tutorials, so in my player behavior, I typed something like "player.Component.Component" instead of "player.Component"...

Gonna play around with physics now. :D

Thanks and regards