Phun with Loo-ah

Published May 30, 2008
Advertisement
I've made the leap into Lua scripting (finally). This has been the biggest hole in the project for a long time. So, here's what I have now:



That guy stuck in the ground is an NPC(!). The exclamation mark is because he's the first ever NPC in the engine. He's at position [0,0,0] because I haven't implemented the Character.position property in Lua yet. He might be participating in collision detection, so I'm really not sure at the moment why he's in the ground.

Here's the Lua script that makes this happen:

print("Hello world!", _VERSION)coroutine.yield()print("this is the second frame")local Fred = Character:new("character/male-1/male.char");coroutine.wait("false");print("we should never get here")Quit()


Anyway its really beautiful outside, so I'm leaving to enjoy it!
Previous Entry Combating macro pollution
Next Entry Median filtering
0 likes 6 comments

Comments

MGB
Nice!
I recently integrated Lua into my game; looks like it'll open up a whole world of modding fun (and abuse :))
Wondering what approach you're taking to game save - especially with Lua in mind..?
June 10, 2008 03:46 PM
dcosborn
Hey, thanks for bringing this up. Honestly, I hadn't thought about how Lua would interact with saved games yet. But I think I'll serialize the state of all running scripts (coroutines), in the manner described here. I'll have a separate mechanism for serializing the C++ game state. And the third thing is to store the mapping of the Lua objects to their C++ counterparts so that we can recreate the userdata at load time. This is because, in the registry, I have a table that maps each Lua object to its userdata, which stores a C++ object pointer and a deleter, with a __gc metamethod.

Do you have any other ideas how to approach this?
June 10, 2008 09:35 PM
MGB
Hmm yeah that sounds fine as long as your Lua calls don't 'block' (i.e. execution doesn't stay in a lua routine for more than one frame).
See e.g. this thread.
Are you going the 'non-blocking' route? I feel a lot of the power & flexibility of scripting is lost when taking that route :-/
June 11, 2008 04:39 PM
dcosborn
Quote:Original post by Aph3x
Hmm yeah that sounds fine as long as your Lua calls don't 'block' (i.e. execution doesn't stay in a lua routine for more than one frame).
See e.g. this thread.

I feel like the only problem here (in the link) is how to serialize the execution context (such as the instruction pointer). However, this can undoubtedly be done by wading through the lua source code and finding enough information to allow you to properly serialize lua_State.

Quote:Original post by Aph3xAre you going the 'non-blocking' route? I feel a lot of the power & flexibility of scripting is lost when taking that route :-/

Well... that's a long story.

Its always been my intention to have multiple scripts running at once, which means each script is a coroutine. The scripts run until they reach the end, so execution does stay in the script across multiple frames, at least for a given execution context.

But blocking is a separate issue...

I was originally going to make all operations block. Then I would have a fork function for handling asynchronous operations, such as walking to a target while saying things and making hand gestures. I think that this would require too much forking too often, which could make the code hard to follow.

My current strategy is to avoid blocking operations, but provide functions for checking goal completion. You may have noticed I have a coroutine.wait function. The idea is that you initiate various non-blocking operations and call coroutine.wait with a string containing whatever conditions need to be met before continuing, such as arriving at the current destination or finishing speaking the current sentence. I'm still keeping the fork function though.
player.walk(target)
coroutine.wait('player.condition.arrived()')
Taking this a bit further, you could have each operation return a function that can be queried to see if the operation has completed. Then, if coroutine.wait receives a function, it should poll it until it evaluates true. So you could write something like this:
coroutine.wait(player.walk(target))
But what about the logical combiners, and and or? Well... I considered replacing the condition functions with userdata and overriding the logical operators in the metatable, but you can't override the logical operators in Lua [sad]. So another solution is something like this:
coroutine.wait(condition.and(player.walk(target), player.say('Hello there!')))
But that's pretty verbose. Another solution is this:
coroutine.wait(player.walk(target), condition.and, player.say('Hello there!'))
...where condition.and is a special sentry constant. This is (exactly) as verbose as the previous solution, but with less bracket nesting. *But* it doesn't allow for explicit operation ordering via brackets. Unless you go too far and implement this:
coroutine.wait({player.walk(target), condition.or, player.say('Hello there!')}, condition.and, player.jump())
LOL. Its convoluted, but I think I could live with syntax like that.
June 12, 2008 09:09 PM
MGB
Interesting approach there. As always, Lua seems to rise to the challenge, with the only limit being the programmer's imagination :)

I've pretty much resigned myself to taking the non-blocking route (using LuaPlus).

Ah found it: was looking for this link for ages - might be useful to you...
Pluto - lua state serialisation.
June 13, 2008 02:18 AM
dcosborn
Ah, thanks for that link!
June 13, 2008 01:43 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Advertisement