Hi!
One job in programming can be done with many ways. And all be right, but one is simpler, second is smaller, third is faster...
Let's talk about another way to control yout game. Conitec in 3dgs give to you some usefull variables: key_force, mouse_force, joy_force. These vars gives smoothed and calibrated user input.
They are great for simplifying your code and you can pick up this technique for your needs. Technique, I'm talking about separating user input functions and you 'reactor' functions, not about using these vars in any project.
Imagine your game:
You have a player on a board, he can move in four directions step by step. He can select and use something from inventory and can view the map.
Yes, you can:
Code:
on_ctrl=use_item;
on_alt=select_item;
...
Ok, second try:
Code:
...
while (lives > 0){ // as long as I'm alive
while (key_any == 0){ // as long as I haven't walked
...
wait (1);
}
if (key_cuu == 1){ // if we press the "up" arrow
...
}
if (key_cud == 1){
...
}
...
Better, but not perfect. Try this (not perfect too):
Code:
var move_forw_scan = 72; //CursorUp
var move_back_scan = 80; //CursorDown
var move_left_scan = 75; //CursorLeft
var move_right_scan = 77; //CursorRight
var select_scan = 56; //Alt
var use_scan = 29; //Ctrl
var map_scan = 15; //Tab
var player_input_mask=0; //bitmask for player movement
define player_move_none, 0;
define player_move_forw, 1;
define player_move_back, 2;
define player_move_left, 4;
define player_move_right, 8;
define player_move_any, 15;
define player_select, 16;
define player_use, 32;
define player_map, 64;
function update_player_input(){ //react for keypressing
while(player==null){wait(1);}
while(player!=null){
player_input_mask=player_move_none;
if(enable_key==on){
// check for keyboard/mouse_button/joy_button input
if(Key_Pressed(move_forw_scan)==on&&
Key_Pressed(move_back_scan)==off&&
Key_Pressed(move_left_scan)==off&&
Key_Pressed(move_right_scan)==off){
player_input_mask=player_move_forw;
}else{
if(Key_Pressed(move_forw_scan)==off&&
Key_Pressed(move_back_scan)==on&&
Key_Pressed(move_left_scan)==off&&
Key_Pressed(move_right_scan)==off){
player_input_mask=player_move_back;
}else{
if(Key_Pressed(move_forw_scan)==off&&
Key_Pressed(move_back_scan)==off&&
Key_Pressed(move_left_scan)==on&&
Key_Pressed(move_right_scan)==off){
player_input_mask=player_move_left;
}else{
if(Key_Pressed(move_forw_scan)==off&&
Key_Pressed(move_back_scan)==off&&
Key_Pressed(move_left_scan)==off&&
Key_Pressed(move_right_scan)==on){
player_input_mask=player_move_right;
}}}}
if(Key_Pressed(select_scan)==on&&
Key_Pressed(use_scan)==off){
player_input_mask|=player_select;
}else{
if(Key_Pressed(select_scan)==off&&
Key_Pressed(use_scan)==on){
player_input_mask|=player_use;
}}
if(Key_Pressed(map_scan)==on){
player_input_mask|=player_map;
}
}
wait(1);
}
}
Somewhere in your code:
Code:
action player{
update_player_input();
player = me;
...
if ((player_input_mask & player_move_any) > 0 ){
if ((player_input_mask & player_move_forw) > 0){ //forward
...
}
if ((player_input_mask & player_move_back) > 0){ //backward
...
}
if ((player_input_mask & player_move_right) > 0){ //right
...
}
if ((player_input_mask & player_move_left) > 0){ //left
...
}
...
}
...
if ((player_input_mask & player_use)!=0){ //use
use_inventory();
}else{
if ((player_input_mask & player_select)!=0){ //select
select_inventory();
}
}
if ((player_input_mask & player_map)>0){ //toggle map
toggle_map_camera();
}
wait(1);
}
Yes this is only a pattern, you have to change this for your needs.
What we can get with this ?
Player input served in one easy managable function, we can reassign input keys with user menu and can easily implement 'key 2 logic' technique.
That's all, may be this usefull for noobs like me...