Monday, February 4, 2008

AI scripting with LUA

This is probably the coolest feature and it's not even my idea! So far, the AI for NPCs was really primitive. All I had was some flag telling the code what can and can't be done like walk=true, changezone=false and so on.

So to add a bit of depth to it, Maple suggested to add scripting. After some research, it ended that it's quite easy to do and doesn't require much changes in the way NPC are handled.

After checking JScript/VBScript, I spent some time checking LUAInterface. Since LUA seems to be so popular these days (used in Metaplace for example), I decided to go with this.

The initial use for this will be for GMs and admin but this possibility fits perfectly with the game idea on working on while testing and finishing the framework. Players should have access to this to program their own NPCs (or just copy/paste scripts from others).

Here's an example that took me about 30 seconds to code just to test LUA with .NET:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objLUA As New Lua

'-- Give LUA access to the .NET function HelloWorld
objLUA.RegisterFunction("HelloWorld", Me, Me.GetType().GetMethod("HelloWorld"))

'-- Declare global LUA variable
objLUA("myTest") = ""

'-- Inject dynamic code
objLUA.DoString("myTest = HelloWorld();")

'-- Read back the LUA varialbe value (now that's cool)
Me.txtResult.Text = objLUA("myTest")
End Sub

Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class