Game authors and players, please read this thread!

Author Topic: PLUGIN: Lua for AGS  (Read 10758 times)  Share 

Re: PLUGIN: Lua for AGS
« Reply #60 on: 02 May 2012, 17:05 »
Nice, thanks! Seems to be working fine so far. :)

Re: PLUGIN: Lua for AGS
« Reply #61 on: 03 May 2012, 13:22 »
Excellent! Sorry for the delay in getting to this stuff  :-[

Re: PLUGIN: Lua for AGS
« Reply #62 on: 12 May 2012, 18:01 »
Hi All,

I am new to the forum and AGS, and I am trying to make use of the Lua plugin in my first game.
After I enable the plugin and run my game, the game crashes and I get the following error: "Error: [Lua] Type not literally persistable by default." Here's a screenshot: http://oi47.tinypic.com/34eqhcm.jpg

Can anyone offer some advice?
Thanks!


Re: PLUGIN: Lua for AGS
« Reply #63 on: 12 May 2012, 19:07 »
That's an indication that there exists a value somewhere in the Lua "universe" that it does not know how to save in a restoreable format. It knows how to do this for every standard Lua object, but not non-standard custom objects (except ones that have had special handling specifically written for them in the plugin).

One example is an open file-handle object. If you do this in a Lua script and run it from game_start():

[code]f = io.open('test.dat', 'wb')[/code]

...this opens a file for writing, and saves the open file-handle object in global variable 'f'. When the time comes for the game to be saved (which is immediately - AGS needs to create a restart point as soon as the game begins, which works by creating a save-game) there is no meaningful way for the variable 'f' to be saved, so you will get the "not literally persistable" error. (So make sure, if you do want to read/write files from Lua, that you do not keep a reference to the file handle around!)

Another example would be objects created by Lua C libraries that come as a DLL (for example, LuaSocket sockets or LPEG patterns). Are you doing anything like that? If not, maybe there is a bug in the plugin and one of these unsaveable values is being created accidentally by the plugin itself. It's not a bug I remember ever encountering myself, though.
« Last Edit: 12 May 2012, 19:13 by Denzil Quixode »

Re: PLUGIN: Lua for AGS
« Reply #64 on: 12 May 2012, 19:15 »
Thanks for the reply, Denzil... but I receive this error even when I have zero Lua scripts in my project...

Re: PLUGIN: Lua for AGS
« Reply #65 on: 12 May 2012, 19:20 »
Okay, so it must be a bug in the plugin. Thanks for the report! Is this an empty/new project, or does it have lots of existing rooms etc.? If it's an existing project, does the same thing happen if you try with a new one?

Re: PLUGIN: Lua for AGS
« Reply #66 on: 12 May 2012, 19:41 »
When I create a new project using "empty" or "default," I don't receive the error.
I only receive it when I create a new project using a saved template.

Re: PLUGIN: Lua for AGS
« Reply #67 on: 12 May 2012, 19:49 »
Aha, interesting - can I get a copy of the template? If it's a private one, but you don't mind just me seeing it, send a PM - I promise to keep it confidential.

(Sorry about this!)

Re: PLUGIN: Lua for AGS
« Reply #68 on: 12 May 2012, 21:14 »
Hmm, here's a weird bug. When I restart the game or restore a saved game, something (the plugin?) seems to override the compat.lua script. When I normally run the game, everything works fine but when I restart, the game crashes with the first call to set a character's x coordinate (because now it's a number value). I load all the scripts in game_start.

Re: PLUGIN: Lua for AGS
« Reply #69 on: 14 May 2012, 22:09 »
Oops. Here is a new dev version with a fix for that AJA:

http://dl.dropbox.com/u/29133560/agslua-dev-2012-05-14.zip

statelessrich, I think this should work for your problem too (I tried building a new game with your template and it works) - could you try it with this version and see if it works for you too?

Re: PLUGIN: Lua for AGS
« Reply #70 on: 14 May 2012, 22:54 »
Still doesn't quite work. :( Different error, though, so that's progress. It seems ags.object[id].Clickable is nil after a restore/restart. Visible and Baseline are also nil, didn't try any more of them. If I comment out the instantly crashing part of code, everything seems to work fine until I a script tries to access an object.

Is there any way to make the error message clearer in this kind of a case where stuff is read from a saved game? Since now it's just: Error: [Lua] [string ""]:0: attempt to call method 'Clickable' (a nil value)

Re: PLUGIN: Lua for AGS
« Reply #71 on: 15 May 2012, 03:23 »
We're getting there! We're getting there!  :X

http://dl.dropbox.com/u/29133560/agslua-dev-2012-05-15.zip

Try that one.

Quote from: AJA
Is there any way to make the error message clearer in this kind of a case where stuff is read from a saved game?

Mm, no, I don't see a way of doing it, sorry :( The error isn't actually occurring while the saved-game is being loaded, and once it's loaded there's no easy way of telling that an error happened because of "something that was just loaded" as opposed to any other kind of error.

Re: PLUGIN: Lua for AGS
« Reply #72 on: 15 May 2012, 04:01 »
Works great! Thanks! :D

And yeah, the error message thing was a bit of a long shot.

Calin Leafshade

  • AGS Project Admins
  • Long live King Cat!
    • I can help with AGS tutoring
    •  
    • I can help with voice acting
    •  
  • Calin Leafshade worked on a game that was nominated for an AGS Award!Calin Leafshade worked on a game that won an AGS Award!
Re: PLUGIN: Lua for AGS
« Reply #73 on: 26 Jul 2012, 22:59 »
Ok, so I've spent the day putzing around with this plugin and frankly I see literally *no reason* why someone would not want to use it. There seems to be no disadvantages (unless you consider lua itself to be a disadvantage). This plugin is literally the best thing ever.

@Denzil

I assume this is fully portable? Do you plan to release the source so that JJS can have a go at recompiling it for other platforms?

Also, if your code is fairly general i reckon we could implement angelscript in a similar fashion.
« Last Edit: 26 Jul 2012, 23:31 by Calin Leafshade »

Leon: You need the sword first before you can get the monkey.

Re: PLUGIN: Lua for AGS
« Reply #74 on: 26 Jul 2012, 23:33 »
Thanks very much! The source code is up on GitHub:

https://github.com/duncanc/Lua-for-AGS

...the code is extremely messy, partly auto-generated, and difficult to compile (I know the VS project files refer to specific paths on my hard drive and there are some static lib dependencies that don't seem easy to find or compile, and so on :P) but it's there. I've always lived in hope of cleaning all this up, and I'd been putting off releasing the code for a while because of that, but decided that was silly. "Release early, release often, fix eventually" as I believe the saying goes.

Re: PLUGIN: Lua for AGS
« Reply #75 on: 26 Jul 2012, 23:41 »
I assume this is fully portable?
I don't believe the run-time plugin uses anything Windows-specific.

Also, if your code is fairly general i reckon we could implement angelscript in a similar fashion.
It isn't very general, I'm afraid, pretty specific (and convoluted). I'm sure you're right that something very similar could be done with other scripting languages, Lua is just one that I happen to know very well.

What I would really love to do is use the incredibly (and increasingly) high-powered version of Lua, LuaJIT2. It really is ridiculously fast, beating even state-of-the-art accelerated JavaScript JIT compilers on some benchmarks. But the big problem is that the serialization functionality that Lua For AGS needs for the save/load functionality is not compatible with LuaJIT2...

Calin Leafshade

  • AGS Project Admins
  • Long live King Cat!
    • I can help with AGS tutoring
    •  
    • I can help with voice acting
    •  
  • Calin Leafshade worked on a game that was nominated for an AGS Award!Calin Leafshade worked on a game that won an AGS Award!
Re: PLUGIN: Lua for AGS
« Reply #76 on: 27 Jul 2012, 17:06 »
Are there any plans to implement QueueGameScriptFunction from the plugin API?

This would allow users to call their own scripts.

My reasoning as to why this would be useful is that a lua script could request a hook from the game script if there were a LuaControl module or something.
like this:

Code: Lua
  1.     requestHook("repeatedly_execute_always", "nameOfLuaFunction")
  2.  

requestHook would look something like

Code: Lua
  1. function requestHook(hook, callback)
  2.     index = storeHookAndCallBackInAnArrayOrSomething(hook, callback) -- return a table index
  3.     ags.QueueScriptFunction("LuaRequestHook",index)
  4. end
  5.  

and the ags would look something like this:

Code: Adventure Game Studio
  1. void LuaRequestHook(int index)
  2. {
  3.     String funcName = Lua.CallScript("GetHookFromIndex", Lua.IntValue(index)); // or whatever, i dont remember the syntax
  4.     String hook = GetTheHookToo(index) //whatevr
  5.     StoreHookAndCallBack(funcName, hook); // just store it in an array
  6.  
  7. }
  8.  
  9. void repeatedly_execute_always()
  10. {
  11.     IterateThroughHooksAndFireIfAppropriate();
  12.    
  13. }
  14.  

I dont think the code was necessary to explain my point... whatever

Leon: You need the sword first before you can get the monkey.

Re: PLUGIN: Lua for AGS
« Reply #77 on: 27 Jul 2012, 17:43 »
The issue of QueueGameScriptFunction/CallGameScriptFunction is one that I do want to return to at some point. I made a start at it but I think I hit a problem (that I helpfully can't remember now) and left it for later...

But having said that, it looks like you shouldn't need it in this particular example, because in Lua you can put functions themselves into arrays - there's no need to burden AGS with the task of storing the hook and callback.

For example, say you have a script like this that you run in game_start():

Code: Lua
  1. -- callbacks.lua
  2.  
  3. local callbacksByEventName = {}
  4.  
  5. function addCallback(eventName, callback)
  6.   -- get a list of callbacks for this event
  7.   local callbacks = callbacksByEventName[eventName]
  8.   -- if the list doesn't exist yet, create it
  9.   if callbacks == nil then
  10.     callbacks = {}
  11.     callbacksByEventName[eventName] = callbacks
  12.   end
  13.   -- add the provided callback function to the end of the list
  14.   callbacks[#callbacks+1] = callback
  15. end
  16.  
  17. function runCallbacks(eventName, ...)
  18.   -- iterate through all callbacks registered to this event name
  19.   for i, cb in ipairs(callbacksByEventName[eventName] or {}) do
  20.     -- call the function, passing through any extra parameters
  21.     cb(...)
  22.   end
  23. end
  24.  

Then, in AGS, set up runCallbacks("repeatedly_execute_always") to be called every frame:

Code: Adventure Game Studio
  1. function repeatedly_execute_always()
  2. {
  3.   Lua.Call("runCallbacks", Lua.StringValue("repeatedly_execute_always"));
  4. }

Now, to add a function to be called on, you would do:

Code: Lua
  1. function DoThisEveryFrame()
  2.   -- (...whatever you like...!)
  3. end
  4.  
  5. addCallback('repeatedly_execute_always', DoThisEveryFrame)

...or, alternatively, even:

Code: Lua
  1. addCallback('repeatedly_execute_always', function()
  2.   -- (...whatever you like...!)
  3. end)
« Last Edit: 27 Jul 2012, 17:46 by Denzil Quixode »

Calin Leafshade

  • AGS Project Admins
  • Long live King Cat!
    • I can help with AGS tutoring
    •  
    • I can help with voice acting
    •  
  • Calin Leafshade worked on a game that was nominated for an AGS Award!Calin Leafshade worked on a game that won an AGS Award!
Re: PLUGIN: Lua for AGS
« Reply #78 on: 27 Jul 2012, 17:54 »
You make a good case. For some reason I didnt think of having lua store the callbacks which is, in hindsight, a far better idea.

Leon: You need the sword first before you can get the monkey.

Crimson Wizard

  • AGS Project Admins
  • not et suppotreD
    • I can help with translating
    •  
Re: PLUGIN: Lua for AGS
« Reply #79 on: 28 Jul 2012, 16:44 »
Do I understand correct that the plugin basically adds a lua-scripts interpreter to the engine?
Sorry, that's probably obvious, just wanted to make it fully clear for myself.