473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Looping Through CurrentJoystickState

I am writing a wrapper class for xna to use generic gamepads and joysticks through directinput. My question is not pertaining to dx or xna but rather how to get property names as a string and their corresponding value from an object. The CurrentJoystickState object has int properties for 24 different axes and various int[] procedures for other information. The axis properties have a ToString() function, but this only returns the int value as a string. How can I procedurally loop through int properties and get the property names and values, excluding the int array procedures?
Oct 19 '08 #1
5 1706
Nevermind, I made an enum of all 24 joystick axes and manaually checked each one on each poll. It's a lot of coding, but it works and pretty fast. If anyone is interested in the code for this class drop me a line and I will be glad to give it to you. I am trying to incorporate every property in joystickstate, including all pov hats, axes, buttons, and sliders, so any hid controller or joystick should work. I have already tested it with xbox and ps2 controllers.
Oct 20 '08 #2
I forgot to mention my GamePad class has events for every property checked, including buttonDown,buttonPressed,stickMoved, sliderMoved, etc... It runs in a separate thread so apps run smoothly.
Oct 20 '08 #3
mldisibio
190 Expert 100+
(You posted your workaround while I was typing this response...)

The System.Type class has a GetProperties method, which in turn takes a BindingFlags enumeration which allows you to specify if you want only public properties or protected, and if you want instance properties or static.

This returns an array of PropertyInfo objects, which belong to the System.Reflection namespace. The PropertyInfo object in turn has a GetValue method which returns the value of a current instance property as an object.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Reflection;
  3. class test {
  4.  static void Main() {
  5.   CurrentJoystickState joystick = new CurrentJoystickState();
  6.   Type JoystickType = typeof(CurrentJoystickState);
  7.   // Get the public properties.
  8.   PropertyInfo[] myPropertyInfo = JoystickType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  9.  
  10.   // Display information for all properties.
  11.   for (int i = 0; i < myPropertyInfo.Length; i++) {
  12.     PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
  13.     if (myPropInfo.PropertyType == typeof(int)) {
  14.       Console.WriteLine("The property name is {0}.", myPropInfo.Name);
  15.       Console.WriteLine("The property value for provided instance is {0}.",  (int)myPropInfo.GetValue(joystick, null));
  16.     } // end if property is int
  17.    } // end property loop
  18.  } // end Main
  19. }
  20.  
The fact that you use GetProperties and not GetMethods will eliminate the procedures which return int[].

This is just to get you started. Notice that GetValue will box your 24 ints as objects. If performance is an issue, you might want to look into Reflection.Emit to generate "getters" that return strongly typed values...but that is not necessary to start, and is an entirely new topic.
Oct 20 '08 #4
Plater
7,872 Expert 4TB
All I found was this:
http://msdn.microsoft.com/en-us/libr...04(VS.85).aspx
Oct 20 '08 #5
Thanks mldisibio for the info. I will definately look back into that as although my class works very fast in a test console app, using it in an XNA game tends to be very irregular and slow at times. It works better actually just checking the states of the selected axes and buttons versus using events to notify the game. When I get this perfected I will post the code for anyone else interested.
Oct 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
2
by: ensnare | last post by:
Hi all, I'm using a database session handler and am looking to loop through data residing in the sessions table to make a 'Users online' array. I've found that using urldecode on the data...
2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
2
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.