Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Dynamic KeyBinding via script?  (Read 5318 times)

0 Members and 1 Guest are viewing this topic.

dogmaan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Dynamic KeyBinding via script?
« on: January 10, 2010, 12:21:14 pm »
Hi Everyone,

I'm currently writing a 2d game , i'm using SFML, C++ and Lua + Luabind for scripting.

I can load and draw a level + entities and set resolution, screen depth etc, via Lua script, but I am having trouble thinking of an efficient way of dynamically binding different keys via a script

i am going to use a lua table that will look something like this

Code: [Select]

keys =
{
    ["right"] = "Right Arrow";
    ["left"] = "Left Arrow";
    ["jump"] = "z"
    etc....etc
};


I'm having trouble thinking of a way to compare the above to the SFML enum types sf::key::somekey, so that Right Arrow == sf::Key::Right  dynamically

any ideas?

thanks

Manux

  • Newbie
  • *
  • Posts: 23
    • View Profile
Dynamic KeyBinding via script?
« Reply #1 on: January 17, 2010, 10:47:53 pm »
The reverse should be done?

i.e. :

Code: [Select]

keys =
{
    "LeftKey" : "left"
    "Z" : "jump"
    //...
};

dogmaan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Dynamic KeyBinding via script?
« Reply #2 on: January 21, 2010, 12:10:37 am »
Okay I solved the problem

first I created these maps and functions and exported them with luabind, in my game they reside in an input class

Code: [Select]

map <string, char> keyMapChar;
map <string, int>    keyMapInt;

void setCharKeys_lua (string name, string id)
{
const char * buf = id.c_str();

keyMapChar[name] = *buf;
}

void setIntKeys_lua (string name, int id)
{
keyMapInt[name] = id;
}


then these lua functions are called

Code: [Select]

keytable =
{
[1] = {"left", 291 };
[2] = {"right", 292 };
}


function setKeys (inputPtr)
numberofkeys = (table.maxn(keytable))
for key = 1, numberofkeys do

if (type(keytable[key][2]) == ("number"))
then
inputPtr:setIntKeys_lua  ((keytable[key][1]), (keytable[key][2]))
else
if (type(keytable[key][2]) == ("string"))
then
inputPtr:setCharKeys_lua ((keytable[key][1]), (keytable[key][2]))
else
-- insert error function here
end
end
end
keytable = (nil)
end


then I use this ridiculously inefficient way to find if the key is down, I'll optimize it later

Code: [Select]

bool isKeyDown (string key)
{
if (keyMapChar.find(key) != keyMapChar.end())
{
if (App->GetInput().IsKeyDown(sf::Key::Code (keyMapChar[key])))
   {
return true;
   }
else
{
return false;
}
}
else
if (keyMapInt.find(key) != keyMapInt.end())
{
if (App->GetInput().IsKeyDown(sf::Key::Code (keyMapInt[key])))
{
return true;
   }
else
{
return false;
}
}
else
{
cout << "Error, key: " << key << " not found" << endl;
return false;
}

}


if anyone would like a better explanation of this please let me know

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Dynamic KeyBinding via script?
« Reply #3 on: January 21, 2010, 05:06:45 pm »
Thanks for sharing.
I plan to use Lua as well
Pluma - Plug-in Management Framework

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
Dynamic KeyBinding via script?
« Reply #4 on: January 22, 2010, 10:41:29 am »
how long did it take you to be able to use lua with C++/SFML?
John Carmack can Divide by zer0.

dogmaan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Dynamic KeyBinding via script?
« Reply #5 on: January 22, 2010, 10:47:16 pm »
Quote from: "WitchD0ctor"
how long did it take you to be able to use lua with C++/SFML?


it took me ages (about 2 weeks to get started), considering I started with no knowledge of C, C++, Lua or Luabind.

the need was kind of born out of necessity, as I realised I was hard coding a game engine to only really do one thing, I didn't really fancy my levels and entity data and other variables controls, gravity, friction etc to be hard coded, Lua seemed to be the quickest scripting language that i could use to instantiate objects at runtime using an object factory class.

once you get used to luabind it becomes quite easy to export class functions to Lua, the main problem is using STL containers, i opted to have all level and entity data passed serially, rather than trying to convert a table to a vector or map

i.e.

Code: [Select]


lua:

-- Sprite sheet table
spritesheets =
{
     [1] = "whatever.png";
     [2] = "something.png";
};

-- how many sprite sheets are there?
numOfSpriteSheets = (table.maxn(spritesheets));


function setSprites (entityPtr)

    -- for each sprite sheet export to C class function
    for iterator = 1, numOfSpriteSheets do
         entityPtr:setSpriteSheet_Lua (spritesheets[iterator])  --C++ exported class function
    end
end

c++

class entity
{
    public:
    void setSpriteSheet_Lua (string);

    private:
    vector<string> spritesheets;
};

void entity::setSpriteSheet_Lua (string spritesheet)
{
    spritesheets.push_back (spritesheet);
}




understand though, if you do decide to integrate a scripting language it will probably take you 10x longer than if you where hard coding a game

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Dynamic KeyBinding via script?
« Reply #6 on: January 22, 2010, 11:55:49 pm »
I didn't started with Lua yet, but I will soon.
I have read some stuff about it, so I have just a some notions of it.

The first direction that I got thinking is to export a lot of handy functions, and Lua code is mainly made of calls to getter and setter functions.
So dealing with containers and complex structures should be made like you pointed, functions like:
addCharacter, getEnergy, removeObject, etc. I see scripting as a way to take control over properties and objects of a game, even when it's about defining a new behaviour, it is based on characters and environment properties, it can change their properties, and perhaps ask a factory to create/remove objects.
However I'm thinking on ways to give some extra control, like defining new properties on Lua. That can be made by derivation of C++ classes. I don't know how hard/easy is defining new classes on Lua.. An alternative can be to provide an array on C++ code, unused on C++, but to be used freely on Lua, as a property of a class (accessed with getters and setters). Thus that restricts the new properties to values of a specific type (integers in my firstthoughts).

I don't want to use luabind, it uses boost (and for some reason I don't want to use it). I see that there are other templates and automatic binders, do you suggest something? I have to read more about it...



Quote
understand though, if you do decide to integrate a scripting language it will probably take you 10x longer than if you where hard coding a game

Yeah, but it will be like 10x faster to customize and extend  :D
Pluma - Plug-in Management Framework

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Dynamic KeyBinding via script?
« Reply #7 on: January 23, 2010, 11:40:25 am »
Quote
I don't want to use luabind, it uses boost (and for some reason I don't want to use it)

:D

By the way, why aren't you following the extended rather than the embedded approach? That means running your program entirely (well, almost) in Lua (or another scripting language) except for the time critical parts which go into a compiled library.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Dynamic KeyBinding via script?
« Reply #8 on: January 23, 2010, 02:39:45 pm »
Maybe because I'm not familiar enough with scripting yet :roll:

It's a beat'em up engine, so scripting is for in game triggers, AI, objects and characters behaviour and interaction, special effects... These are the most important ones that are called every frame or e very couple of frames (like AI I suppose). I wonder how can it affect performance, but I think I can avoid calling Lua functions every frame and do it only by triggering (key press, hit, animation ended...)
The rest is to define screens like menus, cutscenes, etc. Other things are defined with graphical editors (animations, character basis, layers, some fixed behaviour properties, etc).
C++ code provides the engine for physics, graphics, audio, network, etc, abstract classes for the main structure of beat'em up games, factories and managers, objects in a general way, with their mandatory properties, these are basic/abstract characters and objects to be extended with Lua. And finally the way the game runs (where the Lua updates are called, etc)
Pluma - Plug-in Management Framework

dogmaan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Dynamic KeyBinding via script?
« Reply #9 on: January 23, 2010, 10:49:07 pm »
i'm planning on the game engine only running lua snippets for AI etc, for custom entities, i.e. all my own ingame entities AI will be controlled in C++, but if somebody wants to modify or create their own enemy etc, they can do that by defining a custom lua file to go with their entity

that should only cause a slight performance hit for custom entities, and allow external modders to access an ingame console and also create their own content, without having to worry about delving into the bowels of my C code

 

anything