🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Loop trough object instances in Lua

Started by
19 comments, last by Jedive 20 years, 1 month ago
I want to implement Lua into my game engine, and I want it to be event driven. For it, I want to export to Lua an "entity" class (i think i wil use ToLua++ or LuaBind to do that). Every entity will represent an element in the scene. This elements will have an ''OnUpdate'' method, which will be called by the engien every frame. So, when you create a new entity in the script, you must code its functionality on its OnUpdate method. When a frame occurs, the host application has to loop though all the entity instances created by the script, and execute its ''OnUpdate'' function. Does anybody know how can I do that?
Advertisement
Why don''t you keep the entities in the host, and provide functions so LUA can access them? I think it''s much better (flexible and faster) that way.
How can I create new entities from the scripts then?

I don''t know much about LUA, but I would do it this way:
In the host, have a function like create_entity()
Lua will call that function and pass the right parameters whenever you want to create an entity. Then let the host take care of stuff, and call a lua script passing the entity ID and whatever outher flags you want.
Dump all the entities in a table and iterate through it each frame.
Anybody knows where I can find a good example on how to implement Lua in a game engine?
My I ask why you want to use LUA?
In fact, I would prefer to use a more C-like scripting language, but Lua is a very widely used scripting language these days, which tells me that it is not a bad choice. The problem is that I find the Lua documentation very disappointing.

I pretend to make a game engine which is completely scriptable, so with the same engine i can make two very different type of games. I was thinking about something like:

A Game object with some methods like: OnInit(), OnUpdate(), OnFinish(). Game.OnInit() will be called by the engine at initialization, so basic stuff is done. Then, every frame, the Game.OnUpdate() function will be called. There would be a Game.SetSate(state) function that would allow you to set the state to different values (without a predefined meaning, this is up to the user). On Game.OnUpdate(), you would check the current state and act consequently (for example, is state is GS_MENU, it would call the function which creates the menu, if its GS_LOADMAP, it loads the next game map, if its GS_DEFAULT, doesn''t do anything, or if it''s GS_END, it calls Game.Finish() to exit the game).

You should be able to create entities to place in your map. There would be a function like CreateEntity(type), where ''type'' is a string containing the type of entity. If type is ''Player'', for example, every frame, after calling Game.OnUpdate(), the OnUpdate() method of the player will be called.
Don''t go with LUA just because others use it as well. If you want a C like language (no objects tho) go with Small
It''s very very easy to bind with your application (both calling C functions from small or Small functions from C).
I started to use it recently, and it''s a pleasure to use it.
Of course, if you want OOP, then LUA is a better choice.
Thanks, I am seriously considering the Small option, because I am startign to feel disappointed by Lua. Anyway, I''ve heard that Small is only capable of executing bytecode scripts... can''t you load source scripts directly, and let the virtual mchine compile them when loading?

This topic is closed to new replies.

Advertisement