Skip to main content
added 34 characters in body
Source Link
user26550
user26550

Suppose I have a code like this in C++

// Game.cpp
Entity* hero;
Entity* witch;
... // entity initialization

if(/player near witch/) {
    if(hero->get<HealthComponent>()->getHealth() < 100) {
        say("Let me help you");
        witch->get<GraphicsComponent>()->setAnimation("witch.heal");
        hero->get<HealthComponent>()->setHealth(100); 
    }
    else {
        say("Go away!");
    }
}

Suppose I want to put this code into Lua script, so I make an "interact" function in C++

void interact(Entity* first, Entity* second);

And call it like this:

interact(witch, hero);

Then I can call lua script, which gets first and second entity IDs and does something like this:

-- Script.lua
function interact(firstId, secondId)
    if(getHealth(secondId) < 100) then 
        say("Let me help you")
        setAnimation(firstId, "witch.heal")
        setHealth(secondId, 100)
    else 
        say("Go away")
    end
end

And then I can define wrapper functions in C++ like this

void setAnimation(int firstId, const std::string& animationName) {
    Entity* e = entities[firstId];
    e->get<GraphicsComponent>()->setAnimation(animationName);
}
// etc.

and call it from Lua. But this gets quite troublesome as I need to write lots of binding and wrapping(like "setAnimation") functions. Is there any other methods to control entitity behaviour using Lua?

Suppose I have a code like this in C++

// Game.cpp
Entity* hero;
Entity* witch;
... // entity initialization

if(/player near witch/) {
    if(hero->get<HealthComponent>()->getHealth() < 100) {
        say("Let me help you");
        witch->get<GraphicsComponent>()->setAnimation("witch.heal");
        hero->get<HealthComponent>()->setHealth(100); 
    }
    else {
        say("Go away!");
    }
}

Suppose I want to put this code into Lua script, so I make an "interact" function in C++

void interact(Entity* first, Entity* second);

And call it like this:

interact(witch, hero);

Then I can call lua script, which gets first and second entity IDs and does something like this:

-- Script.lua
function interact(firstId, secondId)
    if(getHealth(secondId) < 100) then 
        say("Let me help you")
        setAnimation(firstId, "witch.heal")
        setHealth(secondId, 100)
    else 
        say("Go away")
    end
end

And then I can define wrapper functions in C++ like this

void setAnimation(int firstId, const std::string& animationName) {
    Entity* e = entities[firstId];
    e->get<GraphicsComponent>()->setAnimation(animationName);
}
// etc.

and call it from Lua. But this gets quite troublesome as I need to write lots of binding functions. Is there any other methods to control entitity behaviour using Lua?

Suppose I have a code like this in C++

// Game.cpp
Entity* hero;
Entity* witch;
... // entity initialization

if(/player near witch/) {
    if(hero->get<HealthComponent>()->getHealth() < 100) {
        say("Let me help you");
        witch->get<GraphicsComponent>()->setAnimation("witch.heal");
        hero->get<HealthComponent>()->setHealth(100); 
    }
    else {
        say("Go away!");
    }
}

Suppose I want to put this code into Lua script, so I make an "interact" function in C++

void interact(Entity* first, Entity* second);

And call it like this:

interact(witch, hero);

Then I can call lua script, which gets first and second entity IDs and does something like this:

-- Script.lua
function interact(firstId, secondId)
    if(getHealth(secondId) < 100) then 
        say("Let me help you")
        setAnimation(firstId, "witch.heal")
        setHealth(secondId, 100)
    else 
        say("Go away")
    end
end

And then I can define wrapper functions in C++ like this

void setAnimation(int firstId, const std::string& animationName) {
    Entity* e = entities[firstId];
    e->get<GraphicsComponent>()->setAnimation(animationName);
}
// etc.

and call it from Lua. But this gets quite troublesome as I need to write lots of binding and wrapping(like "setAnimation") functions. Is there any other methods to control entitity behaviour using Lua?

Tweeted twitter.com/#!/StackGameDev/status/483973464509120513
Source Link
user26550
user26550

How can I manage entities(in entity component system) using Lua scripts?

Suppose I have a code like this in C++

// Game.cpp
Entity* hero;
Entity* witch;
... // entity initialization

if(/player near witch/) {
    if(hero->get<HealthComponent>()->getHealth() < 100) {
        say("Let me help you");
        witch->get<GraphicsComponent>()->setAnimation("witch.heal");
        hero->get<HealthComponent>()->setHealth(100); 
    }
    else {
        say("Go away!");
    }
}

Suppose I want to put this code into Lua script, so I make an "interact" function in C++

void interact(Entity* first, Entity* second);

And call it like this:

interact(witch, hero);

Then I can call lua script, which gets first and second entity IDs and does something like this:

-- Script.lua
function interact(firstId, secondId)
    if(getHealth(secondId) < 100) then 
        say("Let me help you")
        setAnimation(firstId, "witch.heal")
        setHealth(secondId, 100)
    else 
        say("Go away")
    end
end

And then I can define wrapper functions in C++ like this

void setAnimation(int firstId, const std::string& animationName) {
    Entity* e = entities[firstId];
    e->get<GraphicsComponent>()->setAnimation(animationName);
}
// etc.

and call it from Lua. But this gets quite troublesome as I need to write lots of binding functions. Is there any other methods to control entitity behaviour using Lua?