AlfonsAberg

I need some help and explaining of PlayerIndex and how to use it.

this is an example of what I want to do:

"for (int i = 0; i < Players.Length; i++)
{
if (XInputHelper.GamePads[i\].APressed)
{
GamePad.SetVibration(i, 1.0f, 1.0f);
}
}"
Players is an Array of all the player objects. I want to cycle all my players and start to rumble that specific controller, but I don't know how to easily transform the int i = 0 too PlayerIndex.One
(i\ should be only i in brackets but this forum displays a light bulb if i write that)

Cheers Alfons


Re: XNA Game Studio Express PlayerIndex?

Joel Martinez

Depending on the actual value assigned to the enums, you could just cast to it from int ... so something like:

PlayerIndex p = (PlayerIndex)0; //should give you PlayerIndex.One





Re: XNA Game Studio Express PlayerIndex?

Johnnylightbulb

You can use the Enum class to do some parsing of those values, or you could just do:

PlayerIndex rumblePlayer = (PlayerIndex)i;

Which casts the integer i to a member of the enumeration. Be careful to figure out which members of PlayerIndex relate to which numbers - Check help or experiment with the Enum class to figure that out.





Re: XNA Game Studio Express PlayerIndex?

Johnnylightbulb

Yeah, what Joel said....



Re: XNA Game Studio Express PlayerIndex?

andyfraser

Hi,

How about :

foreach( PlayerIndex thePlayer in Players )

{

if ( XInputHelper.GamePads[ thePlayer ].APressed )

{

GamePad.SetVibration(thePlayer, 1.0f, 1.0f);

}

}

Andy





Re: XNA Game Studio Express PlayerIndex?

AlfonsAberg

Thanks alot!

I used "PlayerIndex)0; //should give you PlayerIndex.One" and it works like a charm.

Andy, Benjamin Nitschke mentions that we should avoid foreach loops
on his blog. This is what he has to say about it:

"
Don't use foreach loops, especially not in tight render loops. This may sound a little crazy since it does not matter on a PC game and todays CPUs are fast enough to handle the creation and deletion of many thousand objects each frame, which most games don't even need. But on the compact framework you will spam the memory even with things like foreach loops because a new enumerator instance is created everytime you start a foreach loop. After a while there will be a lot of dead objects, which have to be collected. This can take some time and slow your game down immensely. The PC version might run at over 200 frames, but your Xbox 360 version is stuck at something like 30-40 frames. Avoiding creation of new data each frame and avoiding foreach loops (just replace them with normal for loops, it is mostly just 1 line of extra code) can improve your performance by a factor of 100% or more."