[Question] Lua/C++ Balance, sequencing and Lua questions.
Posted: Tue May 10, 2011 12:25 am
Hello everyone,
It's been a while since I've made a topic in here, I hope everyone is doing well.
I'm currently working Lua into my Engine, and I'm stuck on exactly how much should be done in Lua, and how much should be hardcoded into C++.
I guess I should show kind of what I'm doing with my Lua and if I'm doing something stupidly, let me know.
When my Engine initializes with SDL and OpenGL, it moves on to initializing Lua and I use a LuaController class which holds a lua_State pointer so I can handle Lua from any class needed through a static controller. I'm trying to implement a way to call Lua functions and a file easier for things like handling key presses through a lua_push[type] call.
So, basically I have a main.lua file that looks like this:
I'm not passing types through to Lua, instead I'm passing back integers that stand for the ID of the element in a vector.
I create overloaded functions for Lua that call the normal functions so I can reference IDs instead of objects.
Currently maps are still hard coded, but those will get Lua overloads as well.
NPCs look like this:
What I want to do is have a single function for displaying messages, like window:showMessage("Greetings, Hero!") and then have a way to wait until ENTER or something is pressed before continuing on with the rest of the function.
For example:
Would I need to handle coroutines in Lua in order to pause the Lua script from executing everything at once?
How should I handle waiting until a key is pressed before returning to the Lua script without halting other things going on in the game?
And for entity talking for example, should I handle the talk event in C++ or Lua. It's currently in C++ that when you're in front of an entity you trigger it's talk() function, is this okay?
Please let me know if you see anything that is just stupid, or I should improve upon.
Thanks.
It's been a while since I've made a topic in here, I hope everyone is doing well.
I'm currently working Lua into my Engine, and I'm stuck on exactly how much should be done in Lua, and how much should be hardcoded into C++.
I guess I should show kind of what I'm doing with my Lua and if I'm doing something stupidly, let me know.
When my Engine initializes with SDL and OpenGL, it moves on to initializing Lua and I use a LuaController class which holds a lua_State pointer so I can handle Lua from any class needed through a static controller. I'm trying to implement a way to call Lua functions and a file easier for things like handling key presses through a lua_push[type] call.
So, basically I have a main.lua file that looks like this:
Code: Select all
-- Blade Brothers Engine
-- main.lua
dofile("define.lua")
-- create instances of the classes we need
entity = Entity:new()
player = Player:new()
window = Window:new()
-- called when the engine is initialized
function init()
-- print a lua initializing message
print("[LUA] initializing ...\n")
-- create our player
hero = player:addPlayer("player")
-- create the debug_window
debug_window = window:addWindow()
-- set the position and size
window:setPosition(debug_window, 5, 5)
window:setSize(debug_window, 165, 45)
window:setMessage(debug_window, "^c5Blade Brothers Engine^n^c0Debug Window")
end
-- called on each game loop
function loop()
-- set the debug window's text
-- window:setMessage(debug_window, "^c5Blade Brothers Engine^n^c0Debug Window")
end
-- called whenever a key is pressed
function key_press(key)
if key == KEY_SPACE then
print("[LUA] The SPACE key was pressed!")
end
end
I create overloaded functions for Lua that call the normal functions so I can reference IDs instead of objects.
Currently maps are still hard coded, but those will get Lua overloads as well.
NPCs look like this:
Code: Select all
-- on initialize
function init()
end
-- this function is called whenever you talk to the entity
function talk()
-- create our window and return the window index
mywindow = window:addWindow()
-- now we can set our parameters
window:setPosition(mywindow, 180, 350)
window:setSize(mywindow, 200, 60)
window:setMessage(mywindow, "^c3Veteran^c0^n\"Greetings, Hero!\"")
end
For example:
Code: Select all
function init()
-- create this NPC
old_man = entity:addEntity("old_man")
end
function talk()
-- disable player logic
player:setLogic(hero, false)
-- face this NPC to player
entity:faceEntity(old_man, hero)
-- converse with this old man
window:showMessage("Why, hello there!")
window:showMessage("What brings you to these parts?")
window:showMessage("Oh! It's you isn't it?! The hero!")
window:showMessage("Please, take this!")
-- give you a potion
item:giveItem(Item:getItemId("potion"))
-- one more message
window:showMessage("Be safe oh brave hero!")
-- re-enable logic
player:setLogic(true)
end
How should I handle waiting until a key is pressed before returning to the Lua script without halting other things going on in the game?
And for entity talking for example, should I handle the talk event in C++ or Lua. It's currently in C++ that when you're in front of an entity you trigger it's talk() function, is this okay?
Please let me know if you see anything that is just stupid, or I should improve upon.
Thanks.