Defdata within a function and returning variables

BlitzMax Forums/BlitzMax Beginners Area/Defdata within a function and returning variables

Xerra(Posted 2015) [#1]
I've just hacked together a conversion of some blitzplus code I found in the routines section here because it would work for something like a defining your own keys routine for a game.

First question being: Is it correct to have the data statements outside of the function or should they be within it, because it didn't seem to like it when I tried? I like to create reusable code so wrapping it all into a function I can include into the main program makes more sense to me.

Second question: Obviously it's not working at present. I've not got an example in there to test it as yet but I'd be calling the function by sending it a the scancode of whatever key I'm pressing which would be a byte/int whereas I'm returning a string when the function exits. Anyone think of a better way of doing this?

Function GetKeyName(ScanCode:Int)
Local TempString:String

RestoreData keynames
For I:Int = 0 To ScanCode
	ReadData TempString
Next
Return TempString
End Function

#keynames
DefData "ESCAPE KEY", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="
DefData "BACKSPACE", "TAB", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "ENTER"
DefData "LEFT CONTROL", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "TILDE & ACCENT GRAVE", "LEFT SHIFT", "\"
DefData "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "RIGHT SHIFT"
DefData "* (Numeric Keypad)", "LEFT ALT", "SPACE", "CAPITAL", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
DefData "F10", "PAUSE", "SCROLL LOCK", "7 (Numeric Keypad)", "8 (Numeric Keypad)", "9 (Numeric Keypad)"
DefData "- (Numeric Keypad)", "4 (Numeric Keypad)", "5 (Numeric Keypad)", "6 (Numeric Keypad)", "+ (Numeric Keypad)"
DefData "1 (Numeric Keypad)", "2 (Numeric Keypad)", "3 (Numeric Keypad)", "0 (Numeric Keypad)", ". (Numeric Keypad)"
DefData "", "", "OEM_102", "F11", "F12", "", "", "", "", "", "", "", "", "", "", "", "F13", "F14", "F15", "", "", "", "", "", "", "", "", ""
DefData "KANA", "", "", "ABNT_C1", "", "", "", "", "", "CONVERT", ""
DefData "NOCONVERT", "", "YEN", "ABNT_C2 Numpad", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
DefData "= (numeric keypad)", "", "", "PREVIOUS TRACK", "AT"
DefData ":", "UNDERLINE", "KANJI", "STOP", "AX Japan AX", "UNLABELED", ""
DefData "NEXT TRACK", "", "", "ENTER (Numeric Keypad)", "RIGHT CONTROL", "", "", "MUTE", "CALCULATOR", "PLAY/PAUSE", "", "MEDIA STOP", "", "", "", "", "", "", "", "", "", "VOLUME DOWN", ""
DefData "VOLUME UP", "", "WEB HOME", ", (Numeric Keypad)", "", "/ (Numeric Keypad)", "", "SYSREQ", "RIGHT ALT", "", "", "", "", "", "", "", "", "", "", "", "", "NUMLOCK", ""
DefData "HOME", "UP ARROW", "PAGE UP", "", "LEFT ARROW", ""
DefData "RIGHT ARROW", "", "END", "DOWN ARROW", "PAGE DOWN", "INSERT"
DefData "DELETE", "", "", "", "", "", "", "", "LEFT WINDOWS KEY", "RIGHT WINDOWS KEY", "APPS MENU KEY", "SYSTEM POWER", "SYSTEM SLEEP", "", "", "", "SYSTEM WAKE", ""
DefData "WEB SEARCH", "WEB FAVORITES", "WEB REFRESH", "WEB STOP", "WEB FORWARD", "WEB BACK", "MY COMPUTER"
DefData "MAIL", "MEDIA SELECT"



Yasha(Posted 2015) [#2]
DefData is more or less completely unnecessary in BlitzMax and is a relic of an earlier way of doing things. It doesn't gel well with neat scoping and reusability rules, because it predates the idea of bothering with code structure at all.

One "more native" way to do things might be to use an array:

Function GetKeyName:String(ScanCode:Int)
    Global keynames:String[] = [ ..
        "ESCAPE KEY", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", ..
        "BACKSPACE", "TAB", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "ENTER", ..
        "LEFT CONTROL", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "TILDE & ACCENT GRAVE", "LEFT SHIFT", "\", ..
        .. ' ...etc
        "MAIL", "MEDIA SELECT" ..
    ]
    Return keynames[ScanCode]
End Function