Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Setting up arrays for variable names

Author
Message
jaccar09
13
Years of Service
User Offline
Joined: 27th Apr 2011
Location:
Posted: 27th Apr 2011 19:26
i'm not sure of the title myself...

anyway i'm trying to make a 3d virtual piano program. i already have the code and i'm sure i can do finish it in the way i'm working but i'm wondering if it could be done with less code. here's a sample code for the first key of the piano:



what it basically does is if A is pressed on the keyboard, it will rotate and move the first key of the piano down. if not, nothing will happen. what i did was copy this piece of code and just change the keystate scancode and the object numbers. here are the others:




after figuring out a way to make the sound play right (with the third code), i knew i was well on the way to finishing this, but i wonder if there is a way to lessen the amount of work that i would need to do if i pushed through with copy-pasting and changing numbers because there is a lot to be changed in the third code.

i guess an array could help but i don't know how that would look like, so does anybody have an idea? i know you can change numbers with a for-next loop but not numbers in variable names. i also don't want to use the same variable for all keys (such as press to indicate a key is pressed) because it should be possible to play multiple keys at once.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 27th Apr 2011 23:01 Edited at: 28th Apr 2011 19:27
I would recommend making a UDT (a User Defined Type) that'll allow you greater flexibility in your keys. You could not only have sounds associated with the array but any state like a key lock that holds down a key without physically holding down a key.

You use the arrays element number as the actual KEYSTATE number so all you do is add sound numbers to the desired keys and a switch to turn on/off the pressed status.

It'd be like this (attached are some sounds):


The amount of keys you can press at the same time depends on the users keyboard. Some allow only 4 keys at a time while others go up to 7. I only know of one keyboard that can detect 26 keys at a time.

The Ultimate Keyboard:
http://www.amazon.com/Microsoft-JQD-00001-SideWinder-X4-Keyboard/dp/B002ZV51DI

An article about keyboards:
http://www.microsoft.com/appliedsciences/content/projects/AntiGhostingExplained.aspx

Attachments

Login to view attachments
jaccar09
13
Years of Service
User Offline
Joined: 27th Apr 2011
Location:
Posted: 28th Apr 2011 09:54
wow, thanks grog! i think i've read something about UDTs but i didn't think i understood it back then haha

i finished it with copy-paste then number tweaking here and there and it ended up in 619 lines. with your code i was able to squeeze it into 158 (with a few for-next loops for recurring lines of code all over)! haha, it was a bit hard to understand at first but i finally got what it did so i also included the code to make the 3d keys to show that they're pressed.

thanks again!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 28th Apr 2011 19:42
Quote: "with your code i was able to squeeze it into 158"


Np, I'm glad to help you reduce code.

Quote: "it was a bit hard to understand at first but i finally got what it did"


Yeah, sorry about that. I usually explain in the message a bit more about what the code is actually doing but I was in a rush. I'm happy that you understood without a lengthy explanation.

Login to post a reply

Server time is: 2024-11-22 23:52:32
Your offset time is: 2024-11-22 23:52:32