Page 1 of 1

lua question

Posted: Wed Oct 08, 2008 11:35 pm
by jesterguy
ive been working on a tbs game based in space with a friend and id like to implement lua into it to allow all ships and ship components like weapons, engines, shields and so on to be made with lua scripts so i dont havta code them all directly into the game. so i was wondering if you had any good advice or know of any good lua tutorials that deal with something like this, also do you think it would be worth it to look into that luabind thing for this?

screenshot ingame
Image

friend does all the graphics and interface stuff, ive nearly all the map and unit stuff.

the game is currently done in c++ and sdl, but were currently working on implementing opengl to do all the video stuff plus it'll allow us to do new things like particle effects on weapons and stuff.

Re: lua question

Posted: Thu Oct 09, 2008 7:06 pm
by trufun202
Yeah, you could definitely use Lua for that. Similar to how Falco explains in his videos, basically each Weapon, Engine, etc would have its own script with a "Use()" method or whatever.

Then, when you want to use a Weapon:

Code: Select all

luaL_dofile(L, "WeaponXYZ.lua");
lua_getglobal(L, "Use");
lua_call(L, 0, 0);
Using LuaBind is up to you, but I'm a big fan of it. It allows you to expose existing objects and pointers to your scripts. So you won't have to write a bunch of accessor methods specifically for Lua, and you'll be using less memory because you can pass objects by reference.

Re: lua question

Posted: Fri Oct 10, 2008 2:50 am
by jesterguy
yea Ive spent the day experimenting with lua and it seems to be fairly easy to use lua and c++ but combining the two together is where it starts to get complicated.

I can create something like a use function in lua, but i need to be able to pass it the target unit and the unit using it and I'm not sure how to do that.

Also I setup LuaBind and I'm currently looking at examples of how to use it. hopefully this will make things a little easier but do you know of any good example code i can look at doing something similar to what i need?

Re: lua question

Posted: Fri Oct 10, 2008 9:07 am
by Falco Girgis
Without LuaBind, we had to keep track of the entity that we were currently updating and use it as an index for lua calls.

Something like this would call Lua:

Code: Select all

for(currentNPC = 0; currentNPC < totalNPCs; ++currentNPC) {
    //Call respective lua scripts to update.
}
Say that lua calls things like this:

Code: Select all

if GetNPCSomething() == number then SetNPCSomething(number) end
Then the C/++ functions "GetNPCSomething()" and "SetNPCSomething()" use the currentNPC variable in the NPC system to know which one to update:

Code: Select all

int GetNPCSomething(lua_State *state) {
    lua_pushnumber(state, NPCs[currentNPC].something);
    return 1;
}

int SetNPCSomething(lua_State *state) {
    NPCs[currentNPC].something = lua_tonumber(state, -1);
    return 1;
}
Of course, LuaBind would make everything much, much, much easier and prettier. Just make it retrieve a reference to the current NPC. I'm not actually using Luabind yet, so if you have any specific questions, I'm not qualified to answer them.

Re: lua question

Posted: Fri Oct 10, 2008 2:33 pm
by jesterguy
That would seem to work for something like using a potion in an rpg but running something like a weapons firing script would have the possibility of needing access to both the target unit and the unit firing.
I was just thinking about it and I think I know how to do it, but it would defiantly not be the nicest looking or easiest to understand kinda code. I think Ill read up some more on luaBind and see if that's any easier to use.

Thanks for the example, I think example code is the best way to understand something or at least it is for me.

Re: lua question

Posted: Fri Oct 10, 2008 3:31 pm
by MarauderIIC
I don't see why you wouldn't be able to pass a Lua function the target of your weapon.

Couldn't you do something like-
C++:

Code: Select all

Weapon::Fire(Target& target /*or even int targetID*/) {
    //Call weapon_fire lua script, passing it targetID
}
?

I don't know enough about LuaBind, or Lua in general, to be able to tell you specifics, but wouldn't not being able to use it like that make it, oh, mostly useless? Maybe Falco can help.

Re: lua question

Posted: Sat Oct 11, 2008 6:20 am
by jesterguy
MarauderIIC wrote:I don't see why you wouldn't be able to pass a Lua function the target of your weapon.

Couldn't you do something like-
C++:

Code: Select all

Weapon::Fire(Target& target /*or even int targetID*/) {
    //Call weapon_fire lua script, passing it targetID
}
?

I don't know enough about LuaBind, or Lua in general, to be able to tell you specifics, but wouldn't not being able to use it like that make it, oh, mostly useless? Maybe Falco can help.
yea using luaBind I'm pretty sure you can pass pointers to existing objects i just don't know how yet.



So I spent some time playing with luaBind and I defiantly like the part about not having to remake set and get function specifically for lua use, also being able to bind entire classes to lua is awesome.
Now I've been reading all the luaBind stuff google brings up but I cant seem to find a simple example of passing a pointer to an already existing object to lua.
Also is there a way to register something like an already existing array or vector of objects so you can do something like

Code: Select all

function shoot(target)
    unit[target].Dmg(10)
end
or just do a pointer to an object

Code: Select all

function shoot(Unit* target) --Probably wrong
    target.Dmg(10)
end
Another example of needing access to the unit array would be writing a cloaking components script.
In c++ Id just run a for loop checking each enemy ships sensor to see if it can see cloaked ships and if so does its sensor range collide with the cloaked ship,
but to do that in lua Id need access to the unit array.



SideNote I was able to make a very simplified version of that lua console thing you have, and its awsome to be able to type in lua commands in game.Question about your adventure log thing though,
is that log writing everything out to a txt file and storing it all there or do you have some kind of array holding all the strings? I'm considering trying to expand to a full blown console deal like yours but I've never done anything like that so I wasn't sure how to store the logs or whatever.

Re: lua question

Posted: Sat Oct 11, 2008 10:13 am
by trufun202
Here's the Pointers in LuaBind example I sent to Falco:

Code: Select all

//In the Engine 

Player* GetPlayer() 
{ 
return &player; 
} 

luabind::module(L) [ 
luabind::class_<Player>("Player") 
.def(luabind::constructor<std::string>()) 
.def("Init", &Player::Init) 
.def("Print", &Player::Print) 
.def("GetName", &Player::GetName) 
.property("HP", &Player::GetHP, &Player::SetHP) 
.property("MP", &Player::GetMP, &Player::SetMP) 
]; 

luabind::module(L) [ 
luabind::def("GetPlayer", GetPlayer) 
]; 

//In Lua 
function BuffPlayer() 
player2 = GetPlayer() 
player2.HP = 6000 
print ("Lua says that " .. player2:GetName() .. "'s HP is " .. player2.HP) 
end 

//In the Engine 
lua_getglobal(L, "BuffPlayer"); 
lua_call(L, 0, 0); 
printf("C++ says that %s's HP is %d\n", player.GetName().c_str(), player.GetHP()); 

//Output 
Lua says that Fred's HP is 6000 
C++ says that Fred's HP is 6000

Re: lua question

Posted: Sat Oct 11, 2008 8:28 pm
by jesterguy
Sweet! that was exactly what i needed, now i have full control of everything i needed.
That example was perfect, many thanks for the assistance.

You sir are made of win and topped with awesome.

Re: lua question

Posted: Wed Jul 06, 2016 11:27 pm
by lelandbdean
(necro'd--sorry. Not sure if I'm breaking the rules.)

This thread answered a some of my Lua questions. I was worried that accessing entities via ID numbers from Lua was a bad system.

So now my Lua entity access looks like this:

(C99)
int lua_entity_WhateverTheFuck(lua_State *LuaState) {
  int EntityID = -1;
  if (! lua_isnoneornil(LuaState, -1)) EntityID = lua_tointeger(LuaState, -1);
  entity_WhateverTheFuck( &State.Entities[ (EntityID < 0)? State.CurrentEntity : EntityID] );
  return 0;
}
(Lua)
function DopeScript.OnWhatever() {
  Entity.WhateverTheFuck(); -- will affect the current entity.
  Entity.WhateverTheFuck(SomeID); -- will affect the entity with SomeID.
}
To refine this, scripts will be able to do lookups of entities by name (used sparingly!), or tags, whatever.

Something I still need to implement is per-entity per-script attributes, ideally just using Lua tables.
There isn't really a need for the engine to access script-logic-exclusive data...

Anyway, thanks!