1

Hi everyone,
so before the forum had its little episode, Cae Jones posted a package with a new keyconfig system. Problem is, the code is very confusing. Can someone provide an example of how to use this script? Thanks

Ivan

Thumbs up

2

[email protected] config;
void init_config(string filename) {
if (file_exists(filename)) {
keyconfig temp(filename);
@config = temp;
}
else {
// I usually just write a default config file as a string, and load that.
string txt = "Move right:right\nMove left:left\nJump:up\nAttack:space\n";
// load expects a line count:
txt = string_split(txt, "\n", true).length() + "\n" + txt;
keyconfig temp;
temp.load(txt);
@config=temp;
}
}

//There seems to be something causing an extra index, so let's start at 1:
const int RIGHT = 1, LEFT = 2, JUMP = 3, ATTACK = 4;


void press_key(int k) {
int command = config.press(k);
if (command ==RIGHT) press_right();
else if (command == LEFT) press_left();
else if(command == JUMP) press_jump();
else if(command==ATTACK) press_attack();
}
void release_key(int k) {
int command = config.release(k);
if(command == RIGHT) release_right();
else if(command == LEFT) release_left();
// etc
}

If the int constants are too much trouble, you can just check config.names. Just be sure to check for -1, since press and release return -1 for invalid keys.
ex, if(config.names[command] == "Jummp") press_jump();
The names method is good if you want to protect against players rearranging the config file, but it will be much slower than using the index.
The update method is included if you'd rather do it in a more polling-like manner, but I do not recommend it.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up