lua question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

lua question

Post 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.
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: lua question

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: lua question

Post 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?
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: lua question

Post 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.
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: lua question

Post 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.
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: lua question

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: lua question

Post 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.
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: lua question

Post 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
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: lua question

Post 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.
I will live forever or die trying.

"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
lelandbdean
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Thu Mar 28, 2013 10:37 pm
Favorite Gaming Platforms: PC, Gameboy, PSP, SNES, NES
Programming Language of Choice: C/++ usually

Re: lua question

Post 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!
Post Reply