Player input discussion

Posted By: Lion_Ts

Player input discussion - 09/22/05 10:20

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...
Posted By: Lion_Ts

Re: Player input discussion - 09/23/05 18:21

yeah, no discussion...
i'm sorry
Posted By: eleroux

Re: Player input discussion - 10/05/06 05:26

I wrote a script to manage Actors, which can be human or not. So I wanted to separate the user input from it.

What I do is to use a skill for something called 'ActorCommand', used to pass input commands to an actor. This way, if the actor is driven by some AI code, it will also pass 'commands' to the Actor movement script.

Commands are for instance: "COMMAND_JUMP".

and there is a value skill - ActorCommand_Val

for instance the command may be "COMMAND_TURN" and ActorCommand_Val be "-5" or "8"

So far I have managed to keep the keyboard and mouse Input completely out of my Actor pseudo-physics (movement) and from my animation codes.
© 2023 lite-C Forums