Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: Denzil Quixode on Fri 04/09/2009 19:30:22

Title: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 04/09/2009 19:30:22
Edit: I decided to update my original post for the new version rather than trying to have several threads, or post updated versions throughout this one. Sorry if this confuses anyone, it just seemed like the best option.


Hello!

This is my first plugin. It is quite experimental, but I'm fairly pleased with the way it's gone so far. I've developed it against version 3.1.2SP1 (build 3.1.2.82) and haven't had a chance to try it with other versions, so I'm not sure how well it works with them.

Download it here:

>> http://duals.agser.me/Plugins/LuaForAGS_v4.rar (http://lua-for-ags.wikispaces.com/Downloads) <<

The purpose of the plugin is to allow you to write parts of your game's code in Lua, an alternate scripting language to AGS-Script. It is intended for people who are already fairly comfortable with scripting and are interested in trying something a bit different.

The files included in the package are five DLLs - AGS.Plugin.Lua.dll, AGSPlugin.SciLexer.dll, agslua.dll, lua51.dll and lua5.1.dll. Copy all of them into the AGS application home directory. Once you have these DLLs in place and you fire up AGS, there will be a new icon in the project tree called simply "Lua", and also a new entry in "Plugins" called "Lua Run-Time Component". Both of these are disabled by default, and should have no effect until you choose to activate them for a particular game. You need to have both of them active or inactive or you will probably get odd errors. (I'd recommend trying it first on a demo game rather than something important, just in case.)


To create your first Lua script, expand the "Lua" icon, right-click on the "Lua Scripts" icon inside it and select "New Lua script...". Choose a suitable name for it in the window that pops up, and click "Create".


Okay, you will now have to enter some Lua code. How to write Lua itself is not something I'll go into too deeply just yet, but to get you started:
The scripts are saved in a subfolder of your game project directory called "lscripts". You can right-click on the "Lua Scripts" icon and select "Refresh" to reflect changes to the lscripts directory made outside of the AGS Editor.

Once you have written a Lua script, you will want to run it as the game is starting up. If you don't do this, any functions you define inside the script will not be available later on. Open the GlobalScript.asc file and add Lua.RunScript("test.lua"); into the game_start() function (replace "test.lua" with the name of your script if you called it something different):

Code (Lua) Select
function game_start()
{
  Lua.RunScript("test.lua")
  // (... the rest of your initialisation code ...)
}


Okay. Now to test calling your function. Go to a room script and in the handler function for some activity (looking at a hotspot or something), add:

Code (Lua) Select
function hHotspot1_Look()
{
  LuaValue* result = Lua.Call("hello");
  player.Say(result.AsString);
}


What this does is to call the Lua global function "hello", convert the return value to a string, and pass it to player.Say(). It is the equivalent of player.Say(hello());, if hello() was an AGS function rather than a Lua one.

Okay! You are now ready to try compiling and running your game. Hopefully, your player character will say the string returned from the Lua function. (If not, please let me know!)

If you are wondering how using this plugin affects things:
I have put up a page explaining more about what you can do with the plugin here: http://lua-for-ags.wikispaces.com/UsingLuaForAGS (http://lua-for-ags.wikispaces.com/UsingLuaForAGS) Link not working
Title: Re: PLUGIN: Lua for AGS
Post by: Kweepa on Fri 04/09/2009 20:30:05
Wow, nice work!
How does the speed compare with AGS scripting?
I can see this being pretty useful for data management.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 04/09/2009 20:39:56
I have not yet had time to properly benchmark them against each other, but Lua is considered very fast for a dynamically-typed language. It probably is still not fast enough to do real-time graphics effects that need to do lots of pixel manipulation every frame or something, but still, it should be fast enough for you not to be able to tell the difference most of the time.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 06/09/2009 15:47:21
Edit: This post used to be some out-of-date information about calling Lua functions from AGS.
See this Wiki page for the most up-to-date info on that: http://lua-for-ags.wikispaces.com/LuaValueLists (http://lua-for-ags.wikispaces.com/LuaValueLists)
Title: Re: PLUGIN: Lua for AGS
Post by: Pumaman on Sun 06/09/2009 22:06:15
This is pretty neat work!

QuoteI realise that what I haven't demonstrated yet is any compelling reason why you would want to have the option of writing part of your game in Lua.

Yes, I think this would be useful :) 
If it allows things like resizable collections to be easily supported then that would be quite cool.
Title: Re: PLUGIN: Lua for AGS
Post by: Snarky on Mon 07/09/2009 01:14:22
Quote from: Denzil Quixode on Fri 04/09/2009 19:30:22I realise that what I haven't demonstrated yet is any compelling reason why you would want to have the option of writing part of your game in Lua. In time I hope to be able to do this, but for now you may have to just trust me on that.

Grim Fandango was scripted in Lua. Therefore, obviously, working in Lua will enable you to make games as good as Grim Fandango.

Done and done!
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 07/09/2009 15:59:03
Haha  ;D. It's true that Grim Fandango was scripted in Lua, and other games like Psychonauts, and I believe companies like Telltale and PopCap use it in the frameworks for their games. It's popular because it's very small and light, very cleanly coded so it compiles on almost anything, very permissively-licensed (you can use it freely in commercial and non-commercial things without paying for it or agreeing to release any of your own code) and very fast (again, for a dynamic scripting language, not compared to C++ or something).

Okay, this is the first in a short series of posts I'm gonna do about some of Lua's features. This is partly an introduction of how to use Lua, and partly explanation of what I think Lua is good at & valuable for. So, if you're interested in learning Lua, don't solely rely on these, as I might be skipping things that you'd like to know in favour of things I think are cool. All feedback is welcome! :)

Edit: Moved to http://lua-for-ags.wikispaces.com/DataDefinition (http://lua-for-ags.wikispaces.com/DataDefinition)
Title: Re: PLUGIN: Lua for AGS
Post by: SSH on Mon 07/09/2009 17:35:05
This is so great: it lets me make TWO terrible puns in one blog post!

http://ags-ssh.blogspot.com/2009/09/lua-of-temptress.html
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 07/09/2009 18:23:37
Hey, thanks, SSH! Much appreciated.

(Also, it's funny you should mention puns - Lua was created in Brazil, named after the Portuguese word they use for "moon". Apparently it was originally intended to replace a previous scripting language they were using called "Simple Object Language", or "SOL" - and "sol" is also the Portuguese word for "sun"...)

(...I know way too much about this.  :P)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 07/09/2009 19:33:54
Edit: Moved to http://lua-for-ags.wikispaces.com/LuaFunctions (http://lua-for-ags.wikispaces.com/LuaFunctions)
Title: Re: PLUGIN: Lua for AGS
Post by: Igor Hardy on Mon 07/09/2009 21:10:02
Somehow I always thought Lua was something made internally at LucasArts specifically for Grim Fandango.
Title: Re: PLUGIN: Lua for AGS
Post by: edmundito on Tue 08/09/2009 01:41:23
Actually, what happened was that the lead programmer for Grim Fandango found that Lua was just what they were looking for to replace the SCUMM language, and it's apparently recorded as the first game to use the language (for game scripting) (see: http://www.lua.org/history.html and word search for "grim fandango"). Now days it's pretty much becoming the adopted standard for professional game scripting* because it's easy to integrate into anything and it also it's lightweight enough to work with consoles like the Wii.

---

Very interesting work, Denzil! I always wondered myself what it would be like if AGS switched to a more standardized language, and something that's late-binding based. I have done Lua integration in the past, and there's a couple of things that bugged me overall that are huge "gotchas" when you learn:

1. Counting starts at 1, not 0. Oh my!
2. Unless you defined a variable local inside a function it will be global, like JavaScript.
3. There's no decent debugger implementation that can be plugged in. You have to write your own.

That being said, adding this sort of scripting could be the missing piece for a project that I was working on in AGS earlier... actually, can you distribute a game with open Lua scripts? As in I got my agsgame.exe file somewhere, and I let the player write their own script. Is that possible?

---

* Not to say that there are other scripting languages like UnrealScript, Lisp, or Python, but Lua is very widely adopted.... I mean, WoW uses it!
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 08/09/2009 02:08:50
Quote from: Edmundito on Tue 08/09/2009 01:41:23I have done Lua integration in the past, and there's a couple of things that bugged me overall that are huge "gotchas" when you learn:

1. Counting starts at 1, not 0. Oh my!
I agree, it's a bummer. There's not a lot you can do except get used to it and warn people as quickly as possible. Hopefully it not a deal-breaker for too many people. (Some people try to hack Lua to go from 0, but I'm not going down that route.)
Quote2. Unless you defined a variable local inside a function it will be global, like JavaScript.
Yeah, I was annoyed about this too, but it turns out that making variables local by default is an even worse choice, because of the way Lua does variable-scoping (which is really good, and honestly not worth sacrificing to avoid having to write "local" all over the place, IMHO). Maybe in some future version they will be clever enough to find a solution for it, but it really is a more complicated situation than just a bad design choice.
Quote3. There's no decent debugger implementation that can be plugged in. You have to write your own.

That being said, adding this sort of scripting could be the missing piece for a project that I was working on in AGS earlier... actually, can you distribute a game with open Lua scripts? As in I got my agsgame.exe file somewhere, and I let the player write their own script. Is that possible?
Sure - The standard Lua functions loadstring() and loadfile() are available to compile script strings/files into new functions.
Title: Re: PLUGIN: Lua for AGS
Post by: Joseph DiPerla on Tue 08/09/2009 17:58:34
Awesome work DQ!!!!! Probably one of the top 5 most useful plugins.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 08/09/2009 19:00:45
Thanks for the support  :) It's still quite early days though. What I really want to do is start making AGS functions available to the Lua scripts (I mean the in-built AGS functions, not the ones you define yourself in the global/room scripts - I don't think there is a way to call those from a plugin) and add objects to the Lua universe that represent AGS characters, inventory items etc. These objects would support the standard methods (for a character: Say, WalkTo, etc.) but also, allow you to add your own custom methods and fields.

For example, eventually you should be able to do this (Lua):

-- get an AGS character object from a special "ags" table managed by the plugin
local cEgo = ags.cEgo

-- add a new custom method to it (note the colon instead of a dot - this is Lua "method" syntax)
function cEgo:Greeting()
-- "self" inside a Lua method is like "this" in AGS-Script, Javascript etc. - like an invisible parameter
-- that refers to the parent object (in this case, cEgo, although we could reuse this function for different
-- characters)
self:Say("I feel... odd...")
self:Say("Lua... is... controlling me...")
end


...and then, you would also be able to call these Lua methods from AGS-Script, in a similar way to calling a global function using Lua.Call:


function room_AfterFadeIn()
{
   cEgo.LuaMethod("Greeting", null, null);        // Call cEgo:Greeting() with no parameters or return values
}


(This is speculative, it isn't implemented yet, but I think it should all be possible.)
Title: Re: PLUGIN: Lua for AGS
Post by: Joseph DiPerla on Tue 08/09/2009 21:07:28
Lots of hard work ahead of you. But if you get LUA to work with AGS like that, you will have made AGS the most powerful Windows Engine on the market. Now all thats needed is a port of the Run-time plugin so that it would work on the Linux and MAC versions when/If they catch up.

---

ERRRR. The freeware/swapware market that is.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 16/09/2009 14:01:38
I am hoping to get a new version out in the next couple of days. In the meantime I'm going to return to that question of why using Lua in AGS would ever be useful. One Lua feature that I haven't mentioned yet is coroutines. I'm not going to describe what they actually are here, or how to directly create and use them - instead, I'm going to try to outline something quite tricky to do in AGS that could be easier to do with Lua, and (for now) just say that coroutines are the magic that would let it work.




Say you're making a game where the PC is a member of crew on board a space ship. You want to give the impression in-game that the crew is a real community of people having independent lives. You don't want the NPCs to just stand/sit around, waiting for the PC to approach them before they do anything. They may move around, from room to room, start conversations of their own that you can eavesdrop on (or wander away from, if you're not interested). If you interrupt people in the middle of a conversation, they might carry it on where they left off, later. And so on.

An important part of being able to achieve that is being able to easily set up a chain of simple events that run like a cutscene, but unlike a cutscene, you don't want the game to block while it's going on - there might even be several of these things going on simultaneously in the same room, and the PC should be free to walk around and do stuff. Let's call them non-blocking cutscenes.

So, how would you implement that? Let's start with a fairly simple chain of events like: an NPC walks across the room to a particular point, pauses a second, runs an animation loop, walks across to another point, pauses again, faces the PC, says something, then walks out of the room. The best way I can see how to do it is to split this up into a sequence of steps, give them all a number, and do a big if-else-if block in the main script's repeatedly_execute() function, where each step waits for the previous one to finish before continuing. Something like this:

//(AGS Script)

// Counter to keep track of where we are in the sequence
int SEQUENCE_1_STEP = 0;
// Timer ID to use when waiting for NPC to finish pausing
#define PAUSE_TIMER  10
// Overlay for background speech
Overlay* bgSpeechOverlay;

function repeatedly_execute() {
// Set SEQUENCE_1_STEP to 1 to kick things off
       if (SEQUENCE_1_STEP == 0) {
               // do nothing
       }
else if (SEQUENCE_1_STEP == 1) {
// Walk over to a point...
cNPC.Walk(50, 100, eNoBlock);
SEQUENCE_1_STEP = 2;
}
else if (SEQUENCE_1_STEP == 2) {
if (!cNPC.Moving) {
// ... pause ...
SetTimer(PAUSE_TIMER, 80);
SEQUENCE_1_STEP = 3;
}
}
else if (SEQUENCE_1_STEP == 3) {
if (IsTimerExpired(PAUSE_TIMER)) {
// ... run an animation ...
cNPC.Animate(3, 1, eOnce, eNoBlock);
SEQUENCE_1_STEP = 4;
}
}
else if (SEQUENCE_1_STEP == 4) {
if (!cNPC.Animating) {
// ... walk across to another point ...
cNPC.Walk(240, 80);
SEQUENCE_1_STEP = 5;
}
}
else if (SEQUENCE_1_STEP == 5) {
if (!cNPC.Walking) {
// ... pause ...
SetTimer(PAUSE_TIMER, 80);
SEQUENCE_1_STEP = 6;
}
}
else if (SEQUENCE_1_STEP == 6) {
if (IsTimerExpired(PAUSE_TIMER)) {
// ... face the PC ...
cNPC.FaceCharacter(cEgo, eNoBlock);
SEQUENCE_1_STEP = 7;
}
}
else if (SEQUENCE_1_STEP == 7) {
if (!cNPC.Animating) {
// ... say something ...
bgSpeechOverlay = cNPC.SayBackground("...are you responsible for this?");
SEQUENCE_1_STEP = 8;
}
}
else if (SEQUENCE_1_STEP == 8) {
if (!bgSpeechOverlay.IsValid) {
bgSpeechOverlay = null;
// ... then walk out of the room.
cNPC.Walk(0, 150, eNoBlock);
SEQUENCE_1_STEP = 9;
}
}
else if (SEQUENCE_1_STEP == 9) {
if (!cNPC.Moving) {
cNPC.ChangeRoom(10);
                       SEQUENCE_1_STEP = 0;
}
}
}


So that's the first one done. In about sixty lines of code, with an int and an Overlay* to keep track of. We've also used up one of only 20 available timers. It is starting to look daunting to have dozens of these throughout the game. Now, obviously, you can start trying to make it a bit more managable by moving this code out of repeatedly_execute() into a new script, dedicated to these non-blocking cutscene things, and have repeatedly_execute() just call a handler function for each one in turn. You can try to squish each if-else-if step to be on a single line, to try and make it easier to see what's going on throughout the sequence at a glance, but that will probably end up making it even less readable. In order to get around that limited number of timers, you could try using another int variable, that you decrement every turn during the right SEQUENCE_x_STEP until it reaches zero. But it's starting to get annoying just naming all these variables.

It's also no fun tinkering with the sequences, since adding or removing steps means changing all of the SEQUENCE_x_STEP numbers after that point, which takes ages and it's easy to make a mistake. Then there's one sequence where you'd like to repeat part of it in the middle three or four times before continuing, but there's no way of doing it that feels right. You could just copy and paste the steps, then fix up all the SEQUENCE_x_STEP numbers (time-consuming, since there's quite a lot of steps involved - and also, what if you decide later you want to change the number of loops, or alter the steps inside the loop...?). Or, you could create another decrementing int variable, one that counts down the number of loops and decides whether to send SEQUENCE_x_STEP "back in time" to the beginning of the loop or not (which would hurt code-readability even more, as you have to read it back carefully to see where the loop starts and finishes - also, if you want to run the whole sequence multiple times, it's easy to forget to reset this loop variable, so the looped part only plays once the next time...)




Okay, enough hypothetical lecturing, I'm sure you get the picture - this is a case where you'd probably end up compromising quite heavily, to avoid making too much work for yourself, and to keep the code from getting unmanageable. So how could Lua help?

(First of all, this isn't yet possible with the currently-released version, because you can't call AGS system functions from Lua. But you will be able to in the next one.)

I would write a Lua script for handling non-blocking cutscenes, that adds:
I won't give you the code for this script now, but it really isn't that long or complex - I will include something like it as an example script in a later version of the plugin.

Only one new line in repeatedly_execute() would be necessary:


//(AGS Script)
function repeatedly_execute() {
Lua.Call("NonBlockingCutscenes_Update", null, null);
// (... rest of repeatedly_execute ...)
}


...and the implementation of a single cutscene using this system would be something like:


--(Lua)
function ags.cNPC:MyCutscene()

NonBlockingCutscene_Start(function()
ags.cNPC:NonBlockingCutscene_Walk(50, 100)
NonBlockingCutscene_Wait(80)
ags.cNPC:NonBlockingCutscene_Animate(3, 1)
ags.cNPC:NonBlockingCutscene_Walk(240, 80)
NonBlockingCutscene_Wait(80)
ags.cNPC:NonBlockingCutscene_FaceCharacter(ags.cEgo)
ags.cNPC:NonBlockingCutscene_Say("...are you responsible for this?")
ags.cNPC:NonBlockingCutscene_Walk(0, 150)
ags.cNPC:ChangeRoom(10)
end)

end


When you want to start this cutscene from AGS Script, you would call the "MyCutscene" Lua method on cNPC, like this:


//(AGS-Script)
cNPC.LuaMethod("MyCutscene", null, null);


Changing it to loop over part of the sequence a couple of times, using a for-loop:


--(Lua)
function ags.cNPC:MyCutscene()

NonBlockingCutscene_Start(function()
-- repeat this bit 3 times before continuing
for i = 1, 3 do
ags.cNPC:NonBlockingCutscene_Walk(50, 100)
NonBlockingCutscene_Wait(80)
ags.cNPC:NonBlockingCutscene_Animate(3, 1)
ags.cNPC:NonBlockingCutscene_Walk(240, 80)
NonBlockingCutscene_Wait(80)
end
ags.cNPC:NonBlockingCutscene_FaceCharacter(ags.cEgo)
ags.cNPC:NonBlockingCutscene_Say("...are you responsible for this?")
ags.cNPC:NonBlockingCutscene_Walk(0, 150)
ags.cNPC:ChangeRoom(10)
end)

end


Part of my goal for this plugin is to make it as unobtrusive as possible, so you can use it for just one section of the game (even just one NPC) and just ignore it the rest of the time, if you wish. I don't pretend that Lua will make it easier to write any part of a game, but I believe it does have the potential to make some very cool things much easier.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 16/09/2009 18:03:25
I just thought of another example. One thing that I think is quite common is you'll have a list of messages in response to some action, and you'd like to select one each time according to a pattern, like:
This is how you might implement the first one in AGS:

//(AGS-Script)

int MESSAGE_INC = 0;
#define LAST_MESSAGE 5
String NextMessage() {
if (MESSAGE_INC < LAST_MESSAGE) {
MESSAGE_INC = MESSAGE_INC + 1;
}
if (MESSAGE_INC == 1) {
return "I don't want to.";
}
if (MESSAGE_INC == 2) {
return "I really don't want to.";
}
if (MESSAGE_INC == 3) {
return "I really REALLY don't want to.";
}
if (MESSAGE_INC == 4) {
return "Okay. Oh, wait, no.";
}
if (MESSAGE_INC == 5) {
return "No.";
}
}

It's easily possible, but you do have to deal with numbers all the time, which feels sort of unnecessary. If you want to add or remove one early on in the list, you have to go through fixing the rest of the numbers, and remembering to set LAST_MESSAGE as well.

The second pattern is really just a minor change to the first one, so I won't bother with that. What about random order?

//(AGS-Script)
String RandomMessage() {
int rand = Random(4);
if (rand == 0) {
return "Boring.";
}
else if (rand == 1) {
return "Irrelevant.";
}
else if (rand == 2) {
return "Mind-numbing.";
}
else if (rand == 3) {
return "Yawn.";
}
       else if (rand == 4) {
               return "It's a picture of Gordon Brown.";
       }
}

Still dealing with a lot of numbers, although it's never a pain to add more in this case, since order doesn't matter.




With Lua, I would first write a script that adds "selector factories" - special reusable functions that do the grunt work of implementing these patterns for you - and then use them to create NextMessage and RandomMessage (which will be ordinary Lua functions that you can call with Lua.Call()):

--(Lua)

NextMessage = selector.oneshot({
"I don't want to.";
"I really don't want to.";
"I really REALLY don't want to.";
"Okay. Oh, wait, no.";
"No.";
})

RandomMessage = selector.random({
"Boring.";
"Irrelevant.";
"Mind-numbing.";
"Yawn.";
       "It's a picture of Gordon Brown.";
})


With the message lists written like this, you can very easily add, remove, and reorder the items, and also, change one selector pattern to another. Again - nothing that is impossible to do in AGS-Script - but you probably wouldn't bother.

(Like the NonBlockingCutscene stuff in the previous example, selector.oneshot and selector.random are not standard existing functions, I will include a script that implements them in the next version.)
Title: Re: PLUGIN: Lua for AGS
Post by: SSH on Wed 16/09/2009 19:38:10
Of course with this example you could also use my MultiResponse module ;)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 16/09/2009 20:28:46
Ah, oops  :-[. Sorry, SSH - I haven't actually tried a lot of modules or plugins myself before, so I'm not really aware what else is already out there. I should probably do that before I try to give more examples...
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 08/10/2009 19:42:36
OK! It's time to publicise the new version of Lua for AGS.
I've updated the first post a bit and set up this place as the official site for now: http://lua-for-ags.wikispaces.com/

There's more details there but here are some of the main changes:

IDE Features
Title: Re: PLUGIN: Lua for AGS
Post by: Joseph DiPerla on Fri 09/10/2009 03:38:10
Woah! Awesome. I can see some top notch games being made with this...
Title: Re: PLUGIN: Lua for AGS
Post by: ThreeOhFour on Fri 09/10/2009 13:12:49
A lot of this is a mystery to me, but from what I can understand there seems to be a lot of potential here.

Have you any plans to create something like a small demo game to help dull minded people like me understand what sort of capabilities this could provide?
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 09/10/2009 14:20:17
I do plan to, yeah, but I haven't been able to find the time yet.

It's sort of difficult making an effective demo. Unlike a plugin for 3D graphics or something, using Lua will not really make anything possible that used to be impossible - any single example of Lua being used to do something can always be replicated using AGS's standard scripting. The "demo" would really be in looking at how it was coded, not necessarily what it's like for the player.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 13/10/2009 19:48:06
Okay, I'm still working on a proper demo, but in the meantime here is a little "extra" demo to show how Lua could be used to help with debugging.

If you want to be able to do all sorts of little things (add specific inventory, teleport to a specific room, etc.) while debugging/playtesting your game, the usual thing is to set up a load of special key combinations, or a special GUI with a whole load of buttons, something like that. Either way, each new thing that you want to do in-game has to be specially coded, and if you realise you need a new one while playing you have to stop the game to code it in.

The alternative is to be able to type in and run Lua code while the game is running. This is just the standard "Demo Quest" that comes with AGS, with the addition of an interactive Lua prompt:

>> http://octospherics.com/toolbox/ags/DemoGameWithLua.zip (http://octospherics.com/toolbox/ags/DemoGameWithLua.zip) <<

The game loads and plays as normal, but press Ctrl-L to show a prompt at the bottom of the screen that accepts Lua commands. You can also use the Page Up/Page Down keys to find older commands you've typed and repeat them.

For example, try:
print("Hello World from " .. _VERSION .. "!")
(http://octospherics.com/toolbox/ags/demo1.png)
(http://octospherics.com/toolbox/ags/demo2.png)

The prompt tries to be clever, in that if you just enter a value it will try to display it. For example, entering this does the same thing as the previous one:
"Hello World from " .. _VERSION .. "!"

Here are some other commands you can try:
ags.Debug(1,0)
ags.GiveScore(728)
ags.Mouse.GetPosition()
1 + 2
ags.cCris:Say("Hi, I'm %s!", ags.cCris:Name());
ags.cCris:ChangeRoom(16,170,180);
ags.cCris:GiveInventory(ags.iKey)
ags.StopMusic()
obey = ags.Overlay.CreateTextual(10,30,100,2,36,"OBEY!!!!")

As well as directly calling "raw" AGS functions like this, you could also write your own functions in Lua that never get called by the game, but are only meant for being called from this interface.

By the way, this prompt isn't a "standard" feature of the plugin, it's custom-written for this demo but is pretty simple and should be quite easy to integrate into any game.

(Also I've just noticed the game stops with an error if you try to save. This is something to do with the plugin not being able to handle Text Window GUIs I think, I will try to get this fixed asap.)
Title: Re: PLUGIN: Lua for AGS
Post by: Pumaman on Wed 14/10/2009 22:23:59
Quote from: Denzil Quixode on Tue 13/10/2009 19:48:06
The alternative is to be able to type in and run Lua code while the game is running. This is just the standard "Demo Quest" that comes with AGS, with the addition of an interactive Lua prompt:

Here are some other commands you can try:
ags.Debug(1,0)
ags.GiveScore(728)
ags.Mouse.GetPosition()

That's really useful -- it's something I've been meaning to add natively to AGS for ages but never got around to it, and a lot of people have been asking for this run-time scripting ability.

Great work!
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 15/10/2009 00:03:18
Quote from: Pumaman on Wed 14/10/2009 22:23:59
That's really useful -- it's something I've been meaning to add natively to AGS for ages but never got around to it, and a lot of people have been asking for this run-time scripting ability.

Great work!
Thank you!

One noticeable that you can't do now is call your own script functions (as in, functions you write yourself in the main script). I know that the plugin docs say that calling script functions from a plugin function just isn't supported, but is that something that could ever be overcome?
Title: Re: PLUGIN: Lua for AGS
Post by: Pumaman on Thu 15/10/2009 20:44:36
The CallGameScriptFunction and QueueGameScriptFunction plugin API commands are provided for this purpose, though admittedly they are a bit restrictive with what they allow at the moment.

Title: Re: PLUGIN: Lua for AGS
Post by: monkey0506 on Fri 16/10/2009 22:52:24
Quote from: Pumaman on Wed 14/10/2009 22:23:59a lot of people have been asking for this run-time scripting ability.

Chris now you've got me trying to implement this directly within AGS...I've been toying around with the idea a bit. And by "toying around" I of course mean I have nearly a thousand lines of code (not including my Stack module which I'm using) and...the results are interesting (to me in any case). I've gotten loads of functions now running from my simple TextBox console.

It's notably much less versatile than the plugin of course, and there's a lot yet to be done. For example I have no idea how I could possibly convert a String containing the script name of a GUI or InventoryItem into the actual script object. For characters I was able to use the hidden scrname property. But for everything else I think manual registration of the variables would be required.

Also my console isn't equipped yet for multiple function calls per line and can therefore only accept literal parameters. It provides no type-checking and allows implicit type conversion. And the error handling is terrible.

But it's an interesting concept. Who knows, I might even release the code... :P
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Wed 08/12/2010 19:06:36
Bump.

Has anyone started using the plugin seriously?
Have you hit any serious issue?

Is it convenient to try controlling AGS from LUA (using that "ags" object DQ mentionned), or is the only working strategy to call LUA from AGS?
(and by the way it must be slow every time AGS calls a LUA function?)

For short : how mature is the plugin?
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 08/12/2010 20:56:34
Quote from: Monsieur OUXX on Wed 08/12/2010 19:06:36
Has anyone started using the plugin seriously?
(...)
For short : how mature is the plugin?
I am only aware of one person who has seriously tried to use the plugin, apart from me. I'd say it's therefore still very immature, because it does not have a big proof-of-concept game. (I was hoping to make one, but you know how it is...)

QuoteIs it convenient to try controlling AGS from LUA (using that "ags" object DQ mentionned), or is the only working strategy to call LUA from AGS?
It really depends how you want to use it, unfortunately (I think this is the big problem with the plugin, there is no definitive way to use it.)

Quote(and by the way it must be slow every time AGS calls a LUA function?)
Depends what you mean by slow. It's certainly slower than calling an AGS function but I think you should be able to make quite a lot of Lua calls every frame (assuming 40 fps) before it causes any slowdown.

(Also, this is a very picky thing but:- unlike "AGS", "Lua" doesn't stand for anything, it's just a name, so it's more correct to call it "Lua" rather than "LUA".)
Title: Re: PLUGIN: Lua for AGS
Post by: monkey0506 on Thu 09/12/2010 04:08:48
My hard-drive crash manifested whilst using the plugin. Probably not related..but for simplicity's sake..I've decided to just stick to AGS for now. ;P (Considering it took me several weeks to recover from that crash..due to my own lack of backups! :=)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 09/12/2010 10:33:40
Okay, nobody is using the plugin then :P Oh well. Very sorry to hear about your HD woes, monkey - I've been there...
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Thu 09/12/2010 12:19:11
Damn I want to use that plugin.
I want to bind AGS with Blender Game Engine :-)

I didn't understand your answer: "It really depends how you want to use it, unfortunately"
Is it unrealistic to hope controlling AGS from the Lua script exactly the same way you'd control your game from the AGS script? Have you bound all AGS objects in your Lua plugin? (That is: Can I iterate through objects, characters, rooms... Can I read the game's parameters... Can I call AGS functions such as "Changeroom".. etc?)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 09/12/2010 12:59:05
Quote from: Monsieur OUXX on Thu 09/12/2010 12:19:11
Is it unrealistic to hope controlling AGS from the Lua script exactly the same way you'd control your game from the AGS script? Have you bound all AGS objects in your Lua plugin? (That is: Can I iterate through objects, characters, rooms...
Can I read the game's parameters... Can I call AGS functions such as "Changeroom".. etc?)
Yes, you can call standard built-in functions and methods, and refer to built-in objects like GUIs, inventory items, etc. The main thing you can't do is directly call your own AGS-script functions, or get/set your own AGS-script variables - there simply isn't any way to do that through the AGS plugin system, currently. You also can't directly control other plugins.

See this page for more info about what you can do through the ags table: http://lua-for-ags.wikispaces.com/agsTable (http://lua-for-ags.wikispaces.com/agsTable) - there's a table that shows the Lua equivalent to things you do in AGS script.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Thu 09/12/2010 14:23:22
Quote from: Denzil Quixode on Thu 09/12/2010 12:59:05
Yes, you can call standard built-in functions and methods, and refer to built-in objects like GUIs, inventory items, etc.

I love you.

Quote from: Denzil Quixode on Thu 09/12/2010 12:59:05
The main thing you can't do is directly call your own AGS-script functions, or get/set your own AGS-script variables

That shouldn't be too much of a problem. Duplicating needed modules by translating them into Lua shouldn't be too much work.
And I don't need any other plugins, since I'll be using AGS only for Point-n-click events and character localization. All the rendering (graphics, sounds, physics) will be done by the Blender Game Engine.

By the way, for those who wonder what's my plan -- I want to do pretty much the same as the Character3D plugin, except that I want to break the barriers imposed by the custom mesh editor, the complex engine (Irrlicht), and the limited plugin/Irrlicht mapping
Title: Re: PLUGIN: Lua for AGS
Post by: monkey0506 on Fri 10/12/2010 05:46:36
The problems with not being able to use custom AGS functions/variables from Lua scripts can be overcome by simply writing them as Lua functions/variables instead. In order to make any use of them they have to be fired off from some AGS-based event anyway, so it's not really an issue.

I was actually doing precisely this..

Some of the AGS methods the plugin provides are a bit wonky to actually use, which I probably would have commented on if I were developing with it further.

But, I had almost fully converted my custom Verbcoin script into Lua..the only things in the AGS script files were calls to the Lua scripts..so it's very useful as-is.

Also, some of the Lua scripting is difficult to use and/or inconsistent with the way AGS handles things. For example, it's impossible in Lua to set the player's active inventory to nil (this crashes the game). Presumably this would be equivalent to setting player.ActiveInventory = null..and I couldn't think of any work-around for this scenario..short of just using AGScript.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Fri 10/12/2010 10:34:59
Quote from: monkey_05_06 on Fri 10/12/2010 05:46:36
The problems  (...)

Thanks fro the thourough answer!
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 10/12/2010 17:30:45
Quote from: monkey_05_06
Some of the AGS methods the plugin provides are a bit wonky to actually use, which I probably would have commented on if I were developing with it further.
Like the way you have to use attributes as if they were methods? It actually would have been possible not to do that... but at the cost of potentially making performance a  bit worse, because of the internal magics needed. I'm not sure I made the right decision.

Quote from: monkey_05_06
Also, some of the Lua scripting is difficult to use and/or inconsistent with the way AGS handles things. For example, it's impossible in Lua to set the player's active inventory to nil (this crashes the game). Presumably this would be equivalent to setting player.ActiveInventory = null..and I couldn't think of any work-around for this scenario..short of just using AGScript.
This sounds like an outright bug. It's something I simply forgot to try in my own testing,  I guess. I won't be able to sort it out until next week.

Also, reeading your other comments about having to convert your module made me realise that maybe what would be really useful here would be a special splitscreen utility for the plugin where you can copy and paste AGS script into the left hand side,  click a "Convert" button and it will generate equivalent Lua code on the right hand side. I will look into that too.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Tue 14/12/2010 09:42:12
Quote from: Denzil Quixode on Fri 10/12/2010 17:30:45
a "Convert" button and it will generate equivalent Lua code on the right hand side. I will look into that too.

I've tried several times to write a formal Grammar (and the Parser + Code Generator that goes with it) for AGS' scripting language, using a mainstream tool (lately, I was playing with ANTLR ( http://www.antlr.org/ (http://www.antlr.org/)). I never found the time and energy to finish them.

Is it the way you plan on writing your "converter"? I think that would be an extremely powerful artefact to put in the hands of AGS developers. It would initiate a whole lot of new wrappers, converters, Plugin "workarounds", etc.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 14/12/2010 17:06:46
Hmm, I hadn't even heard of ANTLR before... interesting. Actually I happen to already have most of a hand-written C# parser done already, which creates an object-based representation of AGS Script. I would be happy to extract it from the project I was making it for and fix it up into a reusable middleware DLL that people can use in their own plugins. (I'd release it as open source, too, why not.) I need a few days though - my main laptop is in for repairs at the moment and that's got all my plugin stuff on it...
Title: Re: PLUGIN: Lua for AGS
Post by: Joseph DiPerla on Tue 14/12/2010 20:53:05
Look up GOLD Parser Generator as well. That seems to be a good one. If you like in the General Forum, I posted a topic on it yesterday.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Thu 16/12/2010 13:14:06
Quote from: Denzil Quixode on Tue 14/12/2010 17:06:46
I happen to already have most of a parser done already. I would be happy to extract it from the project I was making it for and fix it up into a reusable middleware DLL.

DO IT!  :D :D :D

<3
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 18/12/2010 13:05:17
Quote from: Monsieur OUXX on Thu 16/12/2010 13:14:06
Quote from: Denzil Quixode on Tue 14/12/2010 17:06:46
I happen to already have most of a parser done already. I would be happy to extract it from the project I was making it for and fix it up into a reusable middleware DLL.

DO IT!  :D :D :D

<3
OK, I've started a project at http://spags.googlecode.com/ (http://spags.googlecode.com/) (SPAGS: Script Parser for AGS :P). If you can use SVN you should be able to get it. It includes a project for a test plugin SPAGSTest that creates a file called scripts_dump.txt in the game's project directory (select the SPAGS > Dump Scripts menu item) which isn't very useful on its own but hopefully demonstrates what the parser has understood. Also the source code for that plugin should hopefully be instructive for how to use SPAGS itself - though things might change a bit in the future, it's still very much a work in progress. Also currently it only handles global scripts, not yet room scripts or dialog scripts - but it should be able to handle any global script that would compile normally.

When it's a bit more mature I'll make a thread about it in the technical forum, but I'm still tinkering with it for now.
Title: Re: PLUGIN: Lua for AGS
Post by: monkey0506 on Sat 18/12/2010 16:01:26
You should seriously consider renaming it "SPAGHETTI!"..the YTPers (http://www.youtube.com/watch?v=UTuQAsM3NAk) would love you.
Title: Re: PLUGIN: Lua for AGS
Post by: Clarvalon on Sat 18/12/2010 20:21:40
I've had a peek at the code and it's not too dissimilar to how the conversion works in XAGE, i.e. Tokenising the AGS scripts and then spitting it out in a new format.  I have no experience with LUA so I'm unfamiliar with the format, but it's interesting to see the corresponding structure in scripts_dump.txt.  Excellent work as usual, Mr. Quixode :)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 18/12/2010 21:03:33
Thanks! The scripts_dump.txt output isn't anything related to Lua, by the way, it's just an arbitrary format for demonstrative purposes but doesn't really have any use as it is.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Mon 05/09/2011 15:32:25
Quote from: Denzil Quixode on Sat 18/12/2010 13:05:17
SPAGS (http://spags.googlecode.com/) (SPAGS: Script Parser for AGS :P).

Bump.

@Denzil: Is this going forward?
In other words:
- did you run tests on many arbitrary scripts (i.e. from random modules from the forum) to see if they get parsed properly?
- do you get many "false positive" tests (regarding alleged syntax errors in the scripts) ?

Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 05/09/2011 16:04:56
I ran it on as many Module scripts as I could find (from the appropriate forum), hoping that they would cover complex and exotic uses of AGS script. That is the best source of freely-available non-trivial source code that I was able to find.

There may well be cases where it parses a script successfully where the real parser would fail (for example, I think assigning to a property of a struct* returned by a function probably works, where the real parser chokes) - but at as far as I know it should parse all correct scripts correctly. I have not looked at it in a while, though.

If you have some examples of scripts that you think I should look at, let me know.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Mon 05/09/2011 16:22:57
Quote from: Denzil Quixode on Mon 05/09/2011 16:04:56
If you have some examples of scripts that you think I should look at, let me know.

That wasn't the goal of my post, but come to think of it, I did some extensive use of macros in AGSH (see the link in my signature), and I remember I found onr or 2 glitches in the original parser at the time. I haven't checked if the download links in the AGSH thread are broken or not.

Anyway it doesn't really matter. Your parser seems to be solid by now. COOL.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Mon 12/03/2012 15:57:08
I've finally had the chance to use this plugin in my new project and, boy, it's awesome! Goodbye, restrictions of AGS scripts! Welcome, content that can be modified while running the game and reloaded with the press of a button (requires custom code, but still)! Thanks so much for the plugin, Denzil!

I don't know if you're still willing to maintain it after all these years but here are some things I've noticed:

EDITOR
- Renaming scripts results in duplicate files. (At least inside the editor.)

ENGINE
- Why aren't the game.variables available to the scripts?
- ags.getplayer() doesn't seem to work properly. I tried to get the current room number by calling ags.getplayer():Room() but it returned a table. Calling ags.cEgo:Room() worked just fine.


--edit--

And now suddenly, I keep getting an error that goes something like this: "[Lua] attempting to concatenate a table value". No line number, nothing. And it seems like the error happens after a successful function call from AGS to Lua and before the next call. Maybe it's a bug in the plugin? I'm out of ideas.

If it helps, I've created many different classes (table, metatable, etc.) before this but now that I'm trying to make an Inventory class, it just keeps crashing. Doesn't matter if it's a class or a plain table but sometimes I can define some functions into it and sometimes I can't. Sometimes the error goes away for a while after I comment out some class method call. Very weird.

--edit2--

Maybe it has something to do with the serialization of the Lua state? The crash has always occurred after game_start but before any other events that call the scripts. That's when the default restart point is saved, right?

--edit3--

Probably the same thing that happens sometimes with the editor. I try to compile the game and it says it can't write the exe since it's in use, even though it isn't. After one or more tries it works just fine. Just a moment ago I started the game and it crashed with the error (see above). Then I tried to start the game again. I didn't modify anything but now it started ok. So yeah, what the hell...

--edit4--

Worked around the crash by delaying script loading to the first on_event call. Saving the game produces the crash sometimes, so it's still a very big problem.

And one other thing.. How can I set ActiveInventory to null? ags.cEgo:ActiveInventory( nil / false ) crashes the game because the value isn't an InventoryItem.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 20/03/2012 01:27:06
Quote from: AJA on Mon 12/03/2012 15:57:08
I've finally had the chance to use this plugin in my new project and, boy, it's awesome! Goodbye, restrictions of AGS scripts! Welcome, content that can be modified while running the game and reloaded with the press of a button (requires custom code, but still)! Thanks so much for the plugin, Denzil!
You're welcome! :)
QuoteI don't know if you're still willing to maintain it after all these years but here are some things I've noticed:
I'm willing to maintain it! I just kinda... haven't been  :-\

Some of the bugs that you describe I think - not certain - but I think they are already fixed in the development version, but it just never got turned into a release version. Either way I will try to sort out a new proper release version soon (and feel free to yell at me if I take too long again :)).

QuoteWhy aren't the game.variables available to the scripts?

This might be a restriction of the plugin API. Accessing struct fields is a different thing internally to accessing properties (which are really just getter/setter methods).

QuoteThe crash has always occurred after game_start but before any other events that call the scripts. That's when the default restart point is saved, right?
Yeah, I think so.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Tue 20/03/2012 02:00:02
Quote from: Denzil Quixode on Tue 20/03/2012 01:27:06
I'm willing to maintain it! I just kinda... haven't been  :-\

Understandably. :)


Quote from: Denzil Quixode on Tue 20/03/2012 01:27:06
Some of the bugs that you describe I think - not certain - but I think they are already fixed in the development version, but it just never got turned into a release version. Either way I will try to sort out a new proper release version soon (and feel free to yell at me if I take too long again :)).

I tried saving the game with the dev version you sent me and it hasn't crashed so far! Great! Thanks! :D

The getplayer function is still acting oddly, though. Calling ags.getplayer():Room() to get the room number actually calls my Lua defined table called Room. :-\ And the editor bug is still there too.


Quote
QuoteWhy aren't the game.variables available to the scripts?

This might be a restriction of the plugin API. Accessing struct fields is a different thing internally to accessing properties (which are really just getter/setter methods).

Good point.


I'm thinking of releasing the framework I've written in Lua once I've finished my game. Maybe that'll get more people interested in the plugin. The framework pretty much removes any need for scripting in AGS; you don't even have to link events to their handlers, just the AGS objects to their Lua counterparts.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 29/03/2012 14:42:18
I like this idea.. I will give it a spin.

Is there anything that you would like to be added to the plugin API in order to improve the functionality of the plugin?
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 30/03/2012 10:51:30
For the editor plugin, one problem I remember is there is no way to investigate audio clips through the IGame interface. (There's some other things that are missing from IGame, but I needed them for a custom exporter plugin, not the Lua plugin: which character is the player character, global messages, text parser information, property schema, lip sync information.)

For the runtime plugin, there are things that work but it would be great if they could be more official/documented: the way you have to pass "float" arguments/return values, for example. And getting the address of global variables using the function for getting the address of functions.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Sat 07/04/2012 15:32:26
It would be nice if we could od simple Lua to AGS script function calls using the CallGameScriptFunction and QueueGameScriptFunction plugin API functions. Even though it's limited you can at least pass the proper parameters by reading them from Lua inside the AGS script function. Right?

And what about the undocumented bool Character.on property? That would be useful to easily hide the player character.


-edit-
Oh, Lua + AGS, how have I ever been able to live without you?

Anybody ever wanted to do something like this?


player.Say( "Who's that behind the door? Hello?" );
dude.Say( "It's me. The dude." );

cutToRoom( ROOM_ON_THE_OTHER_SIDE_OF_THE_DOOR );

// Now we're in the other room and the script continues running...
dude.Say( "I'm right here, can't you see me?" );
player.Say( "Oh, there you are." );

cutToRoom( ORIGINAL_ROOM );

// And we're back...
player.Say( "Well, that was fun." );


Well, you can! I'll share all the neat stuff I've hacked up in my AGS-Lua framework sometime in June, once I'm finished with my game. Just don't expect everything to be consistent and pretty. :P
Title: Re: PLUGIN: Lua for AGS
Post by: ShadowStalker on Sun 08/04/2012 07:38:41
I can't use Run without debugger it gives me an error:
---------------------------
Illegal exception
---------------------------
An exception 0xC0000005 occurred in ACWIN.EXE at EIP = 0x0281E51E ; program pointer is -23, ACI version 3.21.1115, gtags (0,0)

AGS cannot continue, this exception was fatal. Please note down the numbers above, remember what you were doing at the time and post the details on the AGS Technical Forum.



Most versions of Windows allow you to press Ctrl+C now to copy this entire message to the clipboard for easy reporting.

An error file CrashInfo.dmp has been created. You may be asked to upload this file when reporting this problem on the AGS Forums. (code 0)
---------------------------
OK   
---------------------------
this only happens if i have the Project tree node activated and the run-time plugin activated :(
EDIT:i use version 3.21
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 09/04/2012 20:34:40
Quote from: AJA on Sat 07/04/2012 15:32:26
It would be nice if we could od simple Lua to AGS script function calls using the CallGameScriptFunction and QueueGameScriptFunction plugin API functions. Even though it's limited you can at least pass the proper parameters by reading them from Lua inside the AGS script function. Right?
I did attempt something like this, but it looks like I commented it out. You would call Lua.RegisterGlobalScriptFunction("myfuncname", <number of parameters>); in AGS script and it would add ags.myfuncname(...) to the Lua side, but there must have been some problem with it.
QuoteAnd what about the undocumented bool Character.on property? That would be useful to easily hide the player character.
That is another struct field, rather than a property with getter/setter methods. I am very unsure about getting/setting fields seeing as it's getting to the point of poking into arbitrary memory.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Wed 02/05/2012 00:33:39
Any chance there'll be a fix for the nil/null problem I mentioned earlier, Denzil?

I ran into another case where it was a bit troublesome: You can't actually cancel FollowCharacter calls since you can't call ags.cEgo:FollowCharacter(nil). That just gives an error that a Character was expected for arg #1.

Luckily in my case I only wanted to undo following because I hit AGS's limit of following characters. So, it was easy to fix by changing the limit in the engine. But if I needed to cancel following, I'd be in big trouble right about now. :P
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 02/05/2012 16:26:54
Here is a bleeding edge dev version that hopefully fixes the nil/null problem:

http://dl.dropbox.com/u/29133560/agslua-dev-2012-05-02.zip
http://dl.dropbox.com/u/29133560/agslua-dev-2012-05-02_2.zip (EDIT: Replaced with a version with a fixed compat.lua)

...however, there is also a new feature which breaks compatibility with existing scripts, but I have included a script called "compat.lua" - run this script straight away before any other Lua script (probably in game_start()) and the compatibility problems should be sorted.

If you don't run this script, then there is different ways of accessing the player object and properties of AGS objects:
ags.player instead of ags.getplayer()
ags.player.ActiveInventory = nil instead of ags.getplayer():ActiveInventory(nil)
ags.Game.SpriteHeight[5] instead of ags.Game.SpriteHeight(5)
...and so on. This way is better, but I know it's a pain when you already have a lot of code that uses the old way, which is why I provide the compat script.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Wed 02/05/2012 17:05:08
Nice, thanks! Seems to be working fine so far. :)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 03/05/2012 13:22:45
Excellent! Sorry for the delay in getting to this stuff  :-[
Title: Re: PLUGIN: Lua for AGS
Post by: statelessrich on Sat 12/05/2012 18:01:02
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!

Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 12/05/2012 19:07:52
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():

f = io.open('test.dat', 'wb')

...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.
Title: Re: PLUGIN: Lua for AGS
Post by: statelessrich on Sat 12/05/2012 19:15:18
Thanks for the reply, Denzil... but I receive this error even when I have zero Lua scripts in my project...
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 12/05/2012 19:20:46
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?
Title: Re: PLUGIN: Lua for AGS
Post by: statelessrich on Sat 12/05/2012 19:41:46
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 12/05/2012 19:49:08
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!)
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Sat 12/05/2012 21:14:53
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 14/05/2012 22:09:20
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 (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?
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Mon 14/05/2012 22:54:42
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)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 15/05/2012 03:23:10
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: AJAIs 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.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Tue 15/05/2012 04:01:40
Works great! Thanks! :D

And yeah, the error message thing was a bit of a long shot.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 26/07/2012 22:59:29
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 26/07/2012 23:33:57
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 26/07/2012 23:41:16
Quote from: Calin Leafshade on Thu 26/07/2012 22:59:29
I assume this is fully portable?
I don't believe the run-time plugin uses anything Windows-specific.

Quote from: Calin Leafshade on Thu 26/07/2012 22:59:29
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...
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Fri 27/07/2012 17:06:47
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) Select

    requestHook("repeatedly_execute_always", "nameOfLuaFunction")


requestHook would look something like

Code (lua) Select

function requestHook(hook, callback)
    index = storeHookAndCallBackInAnArrayOrSomething(hook, callback) -- return a table index
    ags.QueueScriptFunction("LuaRequestHook",index)
end


and the ags would look something like this:

Code (ags) Select

void LuaRequestHook(int index)
{
    String funcName = Lua.CallScript("GetHookFromIndex", Lua.IntValue(index)); // or whatever, i dont remember the syntax
    String hook = GetTheHookToo(index) //whatevr
    StoreHookAndCallBack(funcName, hook); // just store it in an array

}

void repeatedly_execute_always()
{
    IterateThroughHooksAndFireIfAppropriate();
   
}


I dont think the code was necessary to explain my point... whatever
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 27/07/2012 17:43:44
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) Select

-- callbacks.lua

local callbacksByEventName = {}

function addCallback(eventName, callback)
  -- get a list of callbacks for this event
  local callbacks = callbacksByEventName[eventName]
  -- if the list doesn't exist yet, create it
  if callbacks == nil then
    callbacks = {}
    callbacksByEventName[eventName] = callbacks
  end
  -- add the provided callback function to the end of the list
  callbacks[#callbacks+1] = callback
end

function runCallbacks(eventName, ...)
  -- iterate through all callbacks registered to this event name
  for i, cb in ipairs(callbacksByEventName[eventName] or {}) do
    -- call the function, passing through any extra parameters
    cb(...)
  end
end


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

Code (AGS) Select
function repeatedly_execute_always()
{
  Lua.Call("runCallbacks", Lua.StringValue("repeatedly_execute_always"));
}


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

Code (Lua) Select
function DoThisEveryFrame()
  -- (...whatever you like...!)
end

addCallback('repeatedly_execute_always', DoThisEveryFrame)


...or, alternatively, even:

Code (Lua) Select

addCallback('repeatedly_execute_always', function()
  -- (...whatever you like...!)
end)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Fri 27/07/2012 17:54:19
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Sat 28/07/2012 16:44:02
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.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sat 28/07/2012 17:02:34
Yes, that is precisely what it does.

Lua is not a language i personally like that much but it is *far* more flexible than AGSScript and since the plugin provides access to everything that AGSScript does there is really no reason to use standard ags script anymore. I am of the belief that AGSScript is quite literally obsolete at this point.

Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Sat 28/07/2012 17:12:49
But we would still have to have event callbacks in the AGSScript, like game_start, repeatedly_execute, object interactions?
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sat 28/07/2012 17:25:39
Yes, but as Denzil said higher up, it's easy to make a system whereby a lua script can subscribe to events from the main script.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 28/07/2012 19:12:33
Quote from: Calin Leafshade on Sat 28/07/2012 17:02:34
since the plugin provides access to everything that AGSScript does
Not quite true - you cannot access object fields (as opposed to properties, which are really getter/setter functions in disguise). There's very few of those around now, mostly you access everything through properties, but I think there might still be a few odd things you can only do with fields.
Title: Re: PLUGIN: Lua for AGS
Post by: AJA on Sat 28/07/2012 20:25:50
Plus you can't call modules or plugins from Lua.

Glad to see someone else finally seeing the light, Calin. :)

Btw, I'm not sure whether it's AGS or this plugin but it takes quite a while to save a game in Barely Floating (see GiP thread for game info). If AGS saves a screenshot regardless of whatever the game settings are, then it might be AGS. But just as a heads up, if the plugin saving is in any way a slow operation, you might want to add some AGS system polling calls in there because the sound stutters whenever I save a game.

And what exactly is the problem with fields? Can't you just write wrapper functions to access them? Or is there no way to access them through the plugin API? Why do I feel like I've asked this question before... :P
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 28/07/2012 21:34:46
Quote from: AJA on Sat 28/07/2012 20:25:50
Plus you can't call modules or plugins from Lua.
Oh, yes, that too. It should at least be possible to convert any script module to Lua, and I did start work on a tool to automatically convert any piece of AGS script to equivalent Lua code to help with that.

For plugins though... well, it migbt be possible to one day achieve this, but it would take quite a bit of work:
Quote from: AJA on Sat 28/07/2012 20:25:50
Btw, I'm not sure whether it's AGS or this plugin but it takes quite a while to save a game in Barely Floating (see GiP thread for game info). If AGS saves a screenshot regardless of whatever the game settings are, then it might be AGS. But just as a heads up, if the plugin saving is in any way a slow operation, you might want to add some AGS system polling calls in there because the sound stutters whenever I save a game.

There is certainly a bit of processing going on when it serializes the Lua state. Part of the problem is I've never had a realistically large real-world test case project myself to experiment with, so it's quite possible you're seeing it under more strain than I ever have. I will certainly look into good places to put some PollSystem() calls, anyway.

Quote from: AJA on Sat 28/07/2012 20:25:50
And what exactly is the problem with fields? Can't you just write wrapper functions to access them? Or is there no way to access them through the plugin API?
While checking up on the documentation in reply to this I just noticed the GetGameOptions() function in the plugin API that I somehow missed before, so for game options it seems like it should be possible to write those wrapper functions! Sorry for not spotting that before. I'll put that on the todo list.

(In general though, there's no documented way to access them. Admittedly, I am already working with undocumented things in the plugin API, like using "GetScriptFunctionAddress" to get the addresses of variables rather than functions, and the odd way that "float" values are passed around internally as function parameters/return values in AGS. For both of those I was using information I found posted on the technical forum, so it was already kind of unofficially established, but I couldn't find anything about struct fields.)
Title: Re: PLUGIN: Lua for AGS
Post by: Dualnames on Sun 29/07/2012 13:08:38
Further discussion has been moved to  HERE  (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=46493.0)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 29/07/2012 16:21:51
A new version (edit: another new version) of the plugin is now available from http://lua-for-ags.wikispaces.com/Downloads

The main changes:
Title: Re: PLUGIN: Lua for AGS
Post by: JJS on Mon 30/07/2012 10:43:38
I have a quick question on compiling the source: Is the version of Lua used (5.1.4) critical or would the plugin also work with Lua 5.2.x?
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 30/07/2012 10:51:43
Quote from: JJS on Mon 30/07/2012 10:43:38
I have a quick question on compiling the source: Is the version of Lua used (5.1.4) critical or would the plugin also work with Lua 5.2.x?
Unfortunately the Pluto serialization library that this plugin's save/load system relies on is extensively tied to the internals of 5.1. If there is a 5.2-compatible version of Pluto I'm not aware of it.
Title: Re: PLUGIN: Lua for AGS
Post by: JJS on Mon 30/07/2012 11:06:26
Ok, thank you! It is not a problem to use 5.1.4, I just wanted to know what version I should use as a base because they need different patches.
Title: Re: PLUGIN: Lua for AGS
Post by: Joseph DiPerla on Tue 31/07/2012 19:06:24
Awesome work guys!
Title: Re: PLUGIN: Lua for AGS
Post by: JJS on Tue 31/07/2012 20:05:41
I got the plugin running on Android. The changes to the plugin source are in here: https://github.com/jjsat/Lua-for-AGS

Changes are mostly for making GCC happy and enclosing the windows.h include and the MessageBox code in #ifdefs. Also I commented out the unused mutex as it doesn't seem like it is used anywhere.

Can you review the code and maybe merge it into your repo if it doesn't break anything?
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Tue 31/07/2012 21:30:16
Great work JJS! Thanks! I've merged the changes, it all looks reasonable and it compiles without a hitch. (I can't remember why I ever had that mutex...)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Wed 01/08/2012 20:26:55
A request:

Line numbers in the editor please.

Edit: Also a way to enumerate the scripts in the store would be nice.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 01/08/2012 21:15:55
I've added the line numbers to git.

Here's a built version (just the editor plugin): download (http://lua-for-ags.wikispaces.com/file/view/AGS.Plugin.Lua+-+Dev+Version+2012-08-01.zip/355936784/AGS.Plugin.Lua%20-%20Dev%20Version%202012-08-01.zip)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Wed 01/08/2012 21:19:23
Thanks,

with regards to the script store all would really be needed is an array of the filenames and an int somewhere for the count.

Code (ags) Select

int i;
while (i < Lua.ScriptCount)
{
    Lua.RunScript(Lua.ScriptNames[i]);
    i++;
}
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 01/08/2012 21:28:50
It's slightly complicated by the fact that the scripts can be put in folders (and really are stored in this folder hierarchy internally, not an array).
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Wed 01/08/2012 21:31:34
Hmm well the filename could just return the relative folder structure.

So a file at lscripts/folder1/lua1.lua would return "folder1/lua1.lua"

I don't think having a special tree structure in this case is that necessary. If the user wants to rearrange the data at runtime they can do so.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 02/08/2012 00:50:19
I've added Lua.ScriptCount and Lua.ScriptNames[]. Here is the dev version of the runtime plugin with this change: download (http://lua-for-ags.wikispaces.com/file/view/agslua-dev-2012-08-02.zip/355962546/agslua-dev-2012-08-02.zip)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 02/08/2012 00:52:36
Exquisite.

Have yourself an extra slice of cake on my say so.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sat 08/09/2012 18:57:28
A request:

The AGS plugin API provides a way to replace font renderers with custom ones.

Could this be exposed to the lua interface so that one could write replacement font renderers from within lua?

You could create a table with all the functions required by a font renderer and then pass that to a function in the ags table and the lua runtime plugin just calls the lua functions in the script.

Possible?

Also, as an aside, since its quite common to directly write data to file as a lua script, it would be useful if the IsRunningUnderDebugger() function of the API(or whatever its called) were exposed so you could know if it was safe to write to lscripts/
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sat 22/12/2012 00:45:13
Another request, apologies for the triple post but it seemed permissible in this instance.

Could you enable some kind of Lua debugger for remote debugging?
Something like this: http://luaedit.sourceforge.net/support.html#faq-002


Scrap that.

I've tried to implement Mobdebug in my game but of course when Pluto tries to serialise the game it tries to serialise the c functions in socket which is not allowed.

Do you have any suggestions on how to get around this?

Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 22/12/2012 13:45:47
One thing you could try is removing the C functions just before the game gets saved, and re-load them just after the game is restored.

I haven't had a chance to test this myself, but if you add something like this to the end of socket.lua:

Code (Lua) Select
-----------------------------------------------------------------------------
-- Avoid having any C functions around while the game is being saved
-----------------------------------------------------------------------------

function base.ags_presave_socket()
    socket = nil
    base.package.loaded['socket.core'] = nil
end

function base.ags_postrestore_socket()
    socket = require 'socket.core'
end


...then make sure these functions are called on the pre-save and post-restore events, hopefully that will make the C functions unreachable during serialization but still there when you need them.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sat 22/12/2012 14:33:43
Hmm that works but AGS's framerate drops from 60 to about 4. Presumably because of all the polling.

Have you given any thought to any kind of debugger for the lua plugin? It's something it lacks i think at the moment and allowing for variable inspection at runtime will tip Lua over the edge as a defacto replacement for AGSScript. At least for me.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 22/12/2012 18:02:32
I've thought about it, but I feel like it would only really be worth doing if both AGS APIs were more deliberately tailored to supporting custom third-party scripting languages in general, and then had custom hooks for seamlessly dealing with breakpoints etc. for those languages at run-time in particular.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 30/12/2012 19:15:55
This is a bit of a rambling post about potential future features of the plugin. I'd love to hear feedback, particularly from people who can see potential possibilities here, or would at least be interested in experimenting with them.




One idea that I had early on but never quite got around to sorting out is compile-time scripts.

The idea here is that, when you hit compile, the editor itself sets up a Lua state and runs a special script.

Any changes made to the Lua state by this script would be preserved and loaded when the game is run, in the same way that the Lua state is saved and loaded when save games are used.

As a trivial example, if the compile-time script was this:

Code (Lua) Select
compiled_on = os.date('Compiled on %Y-%m-%d')

...then a string with the date that the game was compiled would be accessible as the global variable compiled_on in run-time scripts.

Again, that's a trivial example, I know that if having the compile date was such a useful thing it would just be added to AGS. But the more interesting possibilities are to do some heavyweight pre-processing - if there is a lot of set-up involved right at the start, you could do it at compile time rather than run-time, so the start-up time could be reduced, and you would not need to include a whole bunch of code that would only ever be run at the start, reducing the amount of Lua code left around bulking up the game data and save-game files.

You could even access files in the project source folder. For example, to get the total number of times the game has been compiled, you could store this number in a project file and update it each time. I'm sure there are lots of other possibilities to accessing files in the project folder from a compile-time script.




But maybe it shouldn't always be necessary to do that, and we can do better. Say that when the compile-time script gets run, there is a special project table available. This table is only available to compile-time scripts, not run-time scripts, and it is special in that its contents get preserved between compilations. So, another trivial example of a compile-time script:

Code (Lua) Select
if type(project.compiled_count) == 'number' then
  project.compiled_count = project.compiled_count + 1
else
  project.compiled_count = 1
end

compiled_count = project.compiled_count


Now, the run-time scripts can access compiled_count.

Note that compiled_count is copied to a global variable, because the project table itself won't be available.

The reason for that is that, in real-world examples, I imagine the project table would mostly be used for storing cached partial precalculations and other things that don't need to be in the final game data.




Maybe it should be an option though, so if you do something like this:

Code (Lua) Select
project.runtime_access = true

...the project table (or a copy of it made at compile-time, technically) will available at run-time.




Taking the idea of Lua scripts running in the Editor even further - how about using Lua to script lightweight editor plugins?

The idea is to have a new collection of Lua scripts, called something like editor action scripts. These are not project-specific but would be stored in a subfolder of the AGS editor home directory. (Actually there could be project-specific ones as well, if that's useful.) Unlike the compile-time scripts, what these scripts do to the Lua state is not preserved for the run-time scripts. They might be able to access the project table described earlier, though.

These scripts can be run from within the editor whenever you like, by right-clicking and selecting "Run" from the dropdown menu. (Possibly they could be optionally bound to hotkeys, too.)

These editor action scripts would have access to a special global object called editor. This is an interface to the state of the editor, able to read and change things in the current project in the same way a full-fledged .NET editor plugin can. It would also have a few special dialogs, so you can pop up a message box with custom text, ask a Yes/No question, get the user to enter a string, or get the user to select a certain game item from the current project (sprite, character, etc.) with a selector dialog.




There is a project called LuaInterface (http://code.google.com/p/luainterface/) that allows arbitrary .NET objects to be scripted by Lua, e.g.:

Code (Lua) Select
luanet.System.Windows.Forms.MessageBox.Show("hi")

The editor action scripts could use this too.
Title: Re: PLUGIN: Lua for AGS
Post by: Billbis on Tue 01/01/2013 10:17:43
Quote from: Denzil Quixode on Sun 30/12/2012 19:15:55
One idea that I had early on but never quite got around to sorting out is compile-time scripts.

I totally support this. It would be very useful for game optimization and intensive calculation processes. I can use this for determination of Hotspots coordinates. (http://www.adventuregamestudio.co.uk/forums/index.php?topic=47195.0) ;)
(Even if I do not know how to code a "for each room in project" instructions.)
Also, it will be even better, if possible, that this possibility works with AGS script, and not just Lua code. Maybe by doing a independent - but compatible with Lua - plugin ?
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Tue 01/01/2013 11:24:41
At first I couldn't see the utility. But if the plugin allowed access to all rooms and stuff at compile time then it would actually be very useful for data processing.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Wed 02/01/2013 10:02:52
...This entire thread brilliantly demonstrates why a "real" language is a must in AGS: As Denzil explained in his previous long post, Lua can then be used for no less than 3 different things :
1) Scripts in-game, that can use the billions of scripts already developed in Lua, out there in the real world.
2) Scripts run only once, when the game gets compiled (imagine for example if you pre-processed some Tweens once and for all at compile time, to make the game faster later on -- that's just an example)
3) Make the Editor customizable (a bit the same way as you can run scripts in Photoshop, 3DS or Blender). That would be priceless for people who make large projects and are tired of repeating the same imports and stuff.


I'm planning on packaging a version of AGS that embeds Lua by default, along with other "advanced" tools (SpeechCenter, Tween module, etc.). Is there a default Lua game template?

Title: Re: PLUGIN: Lua for AGS
Post by: Sslaxx on Wed 02/01/2013 11:24:04
Quote from: Monsieur OUXX on Wed 02/01/2013 10:02:52I'm planning on packaging a version of AGS that embeds Lua by default, along with other "advanced" tools (SpeechCenter, Tween module, etc.). Is there a default Lua game template?
Dunno. Might want to check with the module creators they'd be OK with you packaging their stuff up first.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 02/01/2013 12:21:41
Quote from: Monsieur OUXX on Wed 02/01/2013 10:02:52
Is there a default Lua game template?
Not any official one, no. The thing is that it could be used in completely different ways, and so far I've kind of stood back and let people decide how they want to use it rather than push an officially "correct" way. (That's probably part of the reason it took three years for anything to get done with it, but anyway...)

Also, is it possible for templates to include weird arbitrary plugin-related files, like Lua scripts? I haven't tried, I don't remember there being anything special that the editor plugin is able to do to handle templates.
Title: Windows 7 Lua Crashing
Post by: pietakio on Wed 16/01/2013 04:17:52
Hello!

Thanks for all the hard work with the Lua pluggin -- it looks like it can make scripting certain things in AGS a lot easier.

Now, I've been trying to get this Lua plugin to work but it's been crashing no matter what I do. The issue seems to go right back to the plugin itself as even if I create a new default game, enable the Lua plugin (both the "lua scripts" and "lua runtime" components), and then try to run the game with no new Lua scripts or anything, I get an immediate crash (see below)...Obviously the same thing happens if I have a developed game and write some Lua scripts and try to call them in AGS...when I disable the pluggin the game will run again as before...

I had downloaded what I thought to be the latest version of the Lua plugin from http://lua-for-ags.wikispaces.com/ and am running AGS 3.2.1 on a Windows 7 64-bit machine...

After downloading the zipped Lua plugin folder I simply unzipped it and copied the dlls into the AGS editor main program folder as were the instructions...

...the "lua scripts" blue node and "lue code converter" show up in AGS when I did this and I can create new lua scripts in AGS without issue. 

Does anyone have any idea what's going on? I'd really like to be able to use this great piece of work...

Thanks!

Crash:


---------------------------
Illegal exception
---------------------------
An exception 0xC0000005 occurred in ACWIN.EXE at EIP = 0x066DE51E ; program pointer is -23, ACI version 3.21.1115, gtags (0,0)

AGS cannot continue, this exception was fatal. Please note down the numbers above, remember what you were doing at the time and post the details on the AGS Technical Forum.



Most versions of Windows allow you to press Ctrl+C now to copy this entire message to the clipboard for easy reporting.

An error file CrashInfo.dmp has been created. You may be asked to upload this file when reporting this problem on the AGS Forums. (code 0)
---------------------------
OK   
---------------------------




So... 64-bit may be the issue.

"Dependency Walker" reports "agslua.dll" and dependency "lua5.1.dll" (bundled with the latest plugin download) as having been compiled for 32-bit systems, declaring: "Error: Modules with different CPU types were found." That said, it seems a bit doubtful that I could be the only poster in this thread running a 64-bit system.

So... 64-bit may not be the issue. Consider me flummoxed. :tongue:

Incidentally, on merely copying all requisite plugin DLLs into the AGS folder of a Windows XP 64 system I have handy, I receive the following error on every AGS startup:

---------------------------
Adventure Game Studio
---------------------------
There was an error loading plugin 'ags.plugin.lua.dll'.System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'lua5.1.dll': This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)   at AGS.Plugin.Lua.InvokeLua.luaL_newstate()   at AGS.Plugin.Lua.LuaForAGSEditorComponent..ctor(IAGSEditor editor)   at AGS.Plugin.Lua.LuaPlugin..ctor(IAGSEditor editor)   --- End of inner exception stack trace ---   at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)   at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)   at AGS.Editor.EditorPlugin..ctor(String fileName, AGSEditorController pluginEditorController)   at AGS.Editor.Components.PluginsComponent.LoadEditorPluginIntoMemory(String fileName)
---------------------------
OK   
---------------------------


Thanks much, guys.

Combined double post. ^_^
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 16/01/2013 11:48:13
I'm really sorry you're having these problems pietakio. I have 64-bit Windows 7 myself, and just tried a fresh install of AGS 3.2.1 with the latest version of the plugin, and can't reproduce the problem. I also don't have a 64-bit version of Windows XP handy unfortunately. I doubt 64-bit can be the issue because the application and DLLs themselves are all really 32-bit, they run on WoW64 (http://en.wikipedia.org/wiki/WOW64). (Unless there is a 64-bit version of AGS itself that I am not aware of...?) I will try to look into the "modules with different CPU types" thing. Sorry again.
Title: Re: PLUGIN: Lua for AGS
Post by: pietakio on Wed 16/01/2013 14:12:20
Hey Denzil,

Well, thanks for letting me know about the 64-bit issue not being the issue and getting back so quickly. The Windows 7 computer is my main one so no worries about the 64-bit XP side of things -- it was just weird that it didn't work there either.

I wonder if I'm doing something stupid?! To get things going the process is really just:
1. copy the five "LuaForAGS_v6_b" dll's into the AGS editor game folder (alongside ags.exe, etc), then
2. start up AGS, activate the blue Lua node to get the scripts and then activate the green puzzle-piece going under the plugins node

?

I had also tried copying the agslua.dll and the lua51.dll + lua5.1.dll into the game folder but no luck.

Hmmm...hopefully I've just done something dumb as this looks like just the thing to take AGS to the next level!

Thanks again!
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Wed 16/01/2013 16:12:31
Quote from: pietakio on Wed 16/01/2013 14:12:20
I wonder if I'm doing something stupid?! To get things going the process is really just:
1. copy the five "LuaForAGS_v6_b" dll's into the AGS editor game folder (alongside ags.exe, etc), then
2. start up AGS, activate the blue Lua node to get the scripts and then activate the green puzzle-piece going under the plugins node
No, you are doing everything right.

Hmm... I wonder if it is possible that you have another, incompatible version of lua5.1.dll installed in a system folder somewhere that is somehow overriding the one in the immediate application folder. It doesn't seem very likely though...
Title: Re: PLUGIN: Lua for AGS
Post by: pietakio on Wed 16/01/2013 21:27:59
Hmmm...well...thanks for the suggestion but there isn't another copy of the Lua5.5.dll around on my system...

So far have tried *everything* I can think of but no luck...

The only other possible hint is that when a game.exe compiled with the lua plugin is executed (I tried runnin Calin's "Night Thorn" game), there's an error:

Error:[Lua] lscripts.dat signature mismatch

I can't believe it won't work on either of my computers :(

Anyways, if there are any ideas about dependencies and so on please let me know...

Thank you!   
Title: Re: PLUGIN: Lua for AGS
Post by: pietakio on Thu 17/01/2013 00:40:27
Hello again! Sorry for all the posts but since I can't even *PLAY* lua-ags compiled games (i.e. Night Thorn) on either of my two machines at home I thought it might be a kind of important thing to try and suss out what's going on in case there are more folks like me who won't be able to use the lua plugin OR play games made with it....

So, a summary of what's going on with my two machines.


XP-64 bit machine:

Here, if the 5 lua plugin dlls are in the AGS editor directory on startup AGS calls an error ("unable to load ags.plugin.lua.dll") and bunks out.

I then went to the "Administrative Tools Event Viewer" in Windows control panel and found the following message related to the ags crash:

"Resolve Partial Assembly failed for Microsoft.VC80.CRT. Reference error message: The referenced assembly is not installed on your system."

Scanning the lua plugin dlls with the program "dependency walker" for the agslua.dll I get:

Error: The Side-by-Side configuration information for "c:\program files (x86)\adventure game studio 3.2.1\LUA5.1.DLL" contains errors. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem (14001).

It also reports that devmgr.dll is missing, however, it does that for other things that work so I take it with a grain of salt...

Then, when I then tried to PLAY the compiled game Night Thorn (a lua-ags hybrid) I get the event viewer message:

Generate Activation Context failed for C:\Documents and Settings\Administrator\My Documents\GameMaker\NightThorn\NightThorn\Compiled\lua5.1.dll. Reference error message: The referenced assembly is not installed on your system.

And yes lua5.1.dll is right there in the folder that the application is looking for each time (all the required dlls are) -- so this is really kind of funky!

I also immediately checked and can play regular compiled AGS games (5 days a stranger, etc) without issue.

ON the Windows 7 64 bit machine:

Here I put the 5 lua-plugin dll files into the AGS editor folder and fire up AGS without issue. I can activate both components of the plugin and make and write lua scripts and stuff. If there is any call to a lua script in AGS then upon compiling the game (f5 or ctl-f5) I get a message like "LuaValueList is not a public member of 'Lua'. Are you sure you spelt it correctly" and this goes on for all lua-related script. If I remove all reference to lua in the game script and just have the lua plugin activated then it calls a fatal error (ambiguous splash screen mentioned in my first post yesterday). If the lua plugin is disabled, everything is normal and runs fine again. This same kind of stuff happens for games I'm working on, the default game, the empty game, everything.

Running the "dependency walker" on the lua dlls in this system says GPSVC.dll and IESHIMS.dll are missing, though again I see 'em in the System-32 folder so don't understand or know if this is a red herring or not. Scanning AGS dlls gives similar errors and again it works fine.   

I'm not a developer and suspect there is some dependency (visual C++ runtime library?) that you all have on your systems and I don't? Maybe?

Anyways it would be great if the lua plugin could work on at least one of these two computers and probably a good thing to sort out what's happening in case a bunch of folks can't play the lua-AGS hybrid games that get made...

Thanks very much again :)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 17/01/2013 00:56:37
Yea, I think you might need the cpp re-distributable package.

http://www.microsoft.com/en-gb/download/details.aspx?id=14632

That is the 2010 version. You may need the 2008 (or maybe 2005) version depending on what version the lua plugin (and lua) were compiled against.

Title: Re: PLUGIN: Lua for AGS
Post by: pietakio on Thu 17/01/2013 01:35:50
Definitely worth a try but just did the c++ runtime library installs (2010, 2008, 2005 for 64 and 32 bit,  hopefully this isn't really dumb to get all of the ones I didn't have...!) and everything is the same as before :(

Hmmmm.....
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Thu 17/01/2013 20:41:28
Quote from: pietakio on Thu 17/01/2013 00:40:27
in case there are more folks like me who won't be able to use the lua plugin OR play games made with it....
Sure, I understand the potential severity of this... it does seem odd that you're the first person who's had this many problems with it, unless there are others who never bothered to post in the thread...?

Quote from: pietakio on Thu 17/01/2013 00:40:27
"Resolve Partial Assembly failed for Microsoft.VC80.CRT. Reference error message: The referenced assembly is not installed on your system."
As I understand it, this should be taken care of by the msvcr80.dll/Microsoft.VC80.CRT.manifest files that should also be found in the AGS editor folder after a normal installation. I'm no expert, though... hmm, does copying the msvcr*/msvcrm*/*.manifest files to the game project's "compiled" and "debug" folders help anything?

Quote from: pietakio on Thu 17/01/2013 00:40:27
"LuaValueList is not a public member of 'Lua'. Are you sure you spelt it correctly"
That's very weird, like the lscripts.dat error... it almost sounds like the script header is getting corrupt somehow.

Quote from: pietakio on Thu 17/01/2013 00:40:27
Running the "dependency walker" on the lua dlls in this system says GPSVC.dll and IESHIMS.dll are missing
Hmm, for me, dependency walker only mentions KERNEL32.dll and MSVCRT80.dll. It sounds like maybe you have a problem with the Microsoft Visual C Runtime (MSVCR) on both machines, but unfortunately, again, I'm no expert on it...
Title: Re: PLUGIN: Lua for AGS
Post by: pietakio on Thu 17/01/2013 23:00:43
Hey thanks again for your help, and yes, it is probably only some weird windows issue with my machines. 

A great idea and job with the lua plugin though, and I'm sad that I can't use it, but AGS-script will probably be fine in the end.

Thanks!
 
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Fri 18/01/2013 13:41:43
I do hate to turn away an enthusiastic potential user :/ Thank you so much for documenting the errors you encountered though.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Wed 05/06/2013 21:24:41
It seems that loadscript doesnt return an error message. Is there anyway to see if a file exists in the script store? Currently it seems impossible to tell whether loadscript failed because the file does not exist or because there was a compilation error.
Title: Re: PLUGIN: Lua for AGS
Post by: bslinger on Thu 13/06/2013 19:00:02
I just started using the Lua plugin for a game prototype I'm working on after getting frustrated with the limits of AGS Script - it's working great so far, except when I compiled the game and sent it to one of our writers to check out, he got the "lscripts.dat signature mismatch" error mentioned by pietakio. Any ideas on what could be causing that, do we have to distribute some extra .dlls with the game or something? I was glad to find a solution to my AGS problems but if it's not going to reliably work on a variety of machines I'm not sure it's going to be viable. Hopefully we can find a solution, I really can't be bothered writing an adventure game engine in scratch in Unity! :)
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 13/06/2013 21:03:08
Try doing a rebuild all files and be sure to send him the files from the Compiled folder.

If you run the game again in debug I think it invalidates the stuff in the compiled folder because the lua plugin writes a new version of the lscripts.dat.
Title: Re: PLUGIN: Lua for AGS
Post by: bslinger on Fri 14/06/2013 06:25:34
Quote from: Calin Leafshade on Thu 13/06/2013 21:03:08
Try doing a rebuild all files and be sure to send him the files from the Compiled folder.

If you run the game again in debug I think it invalidates the stuff in the compiled folder because the lua plugin writes a new version of the lscripts.dat.

Thanks, looks like that was the problem!
Title: Re: PLUGIN: Lua for AGS
Post by: bslinger on Fri 14/06/2013 19:56:21
Is the documentation on the wiki still up to date? I'm having trouble getting return values from my Lua functions called from AGS Script.

I initially tried this, based on the example here: http://lua-for-ags.wikispaces.com/IntroducingLuaValueLists

Code (AGS) Select

LuaValueList* results = Lua.Call("getQueueLength");
int queueLength = Lua.AsInt(results);


But AsInt doesn't seem to exist as a function of the Lua object.

I then tried to use the examples from here: http://lua-for-ags.wikispaces.com/UsingLuaForAGS

Code (AGS) Select

LuaValueList* results = Lua.NewValueList();
LuaCallResult status = Lua.Call("getQueueLength", null,  results);
Display(status);
Display(results.AsInts[1]);


but that gives me "Error (line 183): Type mismatch: cannot convert 'LuaValueList*' to 'LuaCallMode'" because it seems like the function signature doesn't line up with the documentation.

Any advice?
Title: Re: PLUGIN: Lua for AGS
Post by: dbuske on Wed 10/07/2013 16:13:33
If another scripting language was used in AGS, would it be implemented in the same way?
Meaning it being tied to the editor in the same way.
AGS scriting is far easier then other scripting.
Title: Re: PLUGIN: Lua for AGS
Post by: Scavenger on Sat 30/11/2013 19:13:25
Hey, I've been using the Lua plugin for a couple of days now, and I'm loving it's speed - this is the best thing for complex scripting. AGS Script has nothing on it.

However, I've been having a few problems with it.

Firstly, if you rename a script, it will duplicate the entry and deleting one of the entries will crash AGS and remove "both" scripts.

Secondly, I've been having some problems with the project getting corrupted if AGS doesn't crash cleanly. In 3.3.0, at least, if the engine stalls and needs to be shut down manually (prompting the "The AGS engine did not shut down properly" error in AGS), any Lua calls will crash a few loops in with:

QuoteAdventure Game Studio
---------------------------
An internal error has occurred. Please note down the following information.
If the problem persists, post the details on the AGS Technical Forum.
(ACI version 3.3.0.1148)

Error: [Lua] [string "local select = select;local error = error;l..."]:1: bad argument #2 to 'setter' (number expected, got nil)

I've included a copy of the ruined project here: https://dl.dropboxusercontent.com/u/50882197/Game/paltest_ruined.zip

Other than that, it's brilliant, and I love it! I want to continue using Lua for engine-intensive code.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sat 30/11/2013 21:08:12
Yes, the renaming bug is an embarrassingly old one - I need to fix that, sorry!! :-[

I will have a look at the corrupted project though, I haven't seen that happen before...
Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Sat 30/11/2013 21:21:49
This might as well be AGS 3.3.0 error.
Uh, sorry, I had to read the Scavenger's post more carefully. The crash might be the AGS 3.3.0 error, but the corruption is something I have no idea about.
Title: Re: PLUGIN: Lua for AGS
Post by: Scavenger on Sat 30/11/2013 21:51:50
Additional suggestion: Can we get the 8 bit palette arrays accessable in Lua? I'm currently working on a module that relies on the palette and Lua doesn't have access to it (I tried ags.palette.r etc and it came up with nothing).

Also, LuaValueList.ToArray() seems to be missing from the copy of the plugin on the Downloads (http://lua-for-ags.wikispaces.com/Downloads) page? It doesn't show up in Autocomplete, and it doesn't compile, but it IS in the documentation.

Lua.Evaluate is also missing.
Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Sun 01/12/2013 12:57:00
@Denzil Quixode, your lua-for-ags wiki states (http://lua-for-ags.wikispaces.com/RunTimeComponent):
Quote
The run-time component is a DLL called agslua.dll. This DLL needs to be present in the same directory as your game when it runs, as well as lua51.dll and lua5.1.dll, which agslua.dll depends on. (These should hopefully all be copied in automatically.)
However lua51.dll is never copied to Compiled folder, and game seem to run without it as well.


Another thing I should mention just in case: the game will crash badly if the script .dat file is missing (AGS 3.2.1).
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 01/12/2013 18:24:00
Quote from: Crimson Wizard on Sun 01/12/2013 12:57:00
@Denzil Quixode, your lua-for-ags wiki states (http://lua-for-ags.wikispaces.com/RunTimeComponent):
Quote
The run-time component is a DLL called agslua.dll. This DLL needs to be present in the same directory as your game when it runs, as well as lua51.dll and lua5.1.dll, which agslua.dll depends on. (These should hopefully all be copied in automatically.)
However lua51.dll is never copied to Compiled folder, and game seem to run without it as well.
Ah, oops, thank you, I've changed it. (This was originally true, there WERE two DLLs, because of some confusing issue where some things want to link against "lua51" rather than "lua5.1" -- but once I realised it shouldn't affect this plugin, I removed the extra DLL copy, which only acted as a proxy to the other DLL anyway.)

Quote from: Crimson Wizard on Sun 01/12/2013 12:57:00
Another thing I should mention just in case: the game will crash badly if the script .dat file is missing (AGS 3.2.1).
Thanks for the report, I'll start adding these things to the github tracker.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 01/12/2013 22:47:24
Quote from: Scavenger on Sat 30/11/2013 19:13:25
I've been having some problems with the project getting corrupted if AGS doesn't crash cleanly. In 3.3.0, at least, if the engine stalls and needs to be shut down manually (prompting the "The AGS engine did not shut down properly" error in AGS), any Lua calls will crash a few loops in with:

QuoteAdventure Game Studio
---------------------------
An internal error has occurred. Please note down the following information.
If the problem persists, post the details on the AGS Technical Forum.
(ACI version 3.3.0.1148)

Error: [Lua] [string "local select = select;local error = error;l..."]:1: bad argument #2 to 'setter' (number expected, got nil)

I've included a copy of the ruined project here:
Thank you very much for the test case!

So, I have to apologise for that error message. I know there's no way you would be able to guess this, but the actual problem here is that you end up assigning nil to OSSurface.DrawingColor in your ProcessTranslucency() function.

I will have to do something about that error message, so it actually shows your script/line number (instead of blaming the internal script that handles the "DrawingColor" getter/setter)

For now, you can do a temporary workaround get better error messages here by changing lines like this:

Code (lua) Select
OSSurface.DrawingColor = <VALUE>

...to this:

Code (lua) Select
OSSurface.DrawingColor = assert(  <VALUE>  ,'DrawingColor cannot be nil')

Digging in a bit, it looks like BGSurface:GetPixel(x,y) is returning -1 sometimes, and this leads to it looking up clut[ i ][ j ] for a negative j, which is where the nil value comes from. I'm afraid I have no idea whether GetPixel returning -1 is something that should ever happen or why it would be related to project corruption or 3.3.0, etc.

Quote from: Scavenger on Sat 30/11/2013 21:51:50
Also, LuaValueList.ToArray() seems to be missing from the copy of the plugin on the Downloads (http://lua-for-ags.wikispaces.com/Downloads) page? It doesn't show up in Autocomplete, and it doesn't compile, but it IS in the documentation.

Lua.Evaluate is also missing.
I must apologise for this too - I changed my mind several times on how the whole LuaValueList/LuaValue API stuff should work, and was inconsistent about both implementing and documenting features in it, as things got rewritten and moved around... I plan to move all the documentation and stuff over to GitHub soon, and as part of that I will double-check everything in the docs as I do it, so there's no more nasty surprises like this.
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Sun 01/12/2013 22:57:31
GetPixel returns -1 because COLOR_TRANSPARENT is -1 so it just refers to a mask pixel.
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Sun 01/12/2013 23:17:36
Quote from: Calin Leafshade on Sun 01/12/2013 22:57:31
GetPixel returns -1 because COLOR_TRANSPARENT is -1 so it just refers to a mask pixel.
That makes sense, but seems odd since we're talking about what I assume is a room background surface -- do room backgrounds have transparency masks?

(That's probably something to discuss further in the Advanced Technical Forum thread about this, which I've only just noticed)
Title: Re: PLUGIN: Lua for AGS
Post by: Denzil Quixode on Mon 02/12/2013 13:40:03
Quote from: Scavenger on Sat 30/11/2013 21:51:50
Additional suggestion: Can we get the 8 bit palette arrays accessable in Lua? I'm currently working on a module that relies on the palette and Lua doesn't have access to it (I tried ags.palette.r etc and it came up with nothing).
I've made changes to the code (there'll be a new build soon) to allow ags.palette[ i ].r etc. to work, and also a (probably faster) alternative form that uses Lua's multiple-return value feature:
Code (Lua) Select
local r,g,b = ags.palette.getRGB(3)
ags.palette.setRGB(5, 0,63,0)

-- copying a colour from one slot to another
ags.palette.setRGB(30, ags.palette.getRGB(11))
Title: Re: PLUGIN: Lua for AGS
Post by: dbuske on Thu 11/09/2014 14:04:14
Ok, I converted the intro room script to Lua.
Do I put the Lua code in place of the original ags room code.
Or is their more?
Title: Re: PLUGIN: Lua for AGS
Post by: Calin Leafshade on Thu 11/09/2014 15:05:17
There is significantly more.

The AGS-Lua link is purely manual. If you want to trigger AGS events in lua then you need to wire that up yourself.

You need a fairly good grasp of programming before you'll be able to understand how this is done.
Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Thu 11/09/2014 15:28:39
Quote from: dbuske on Thu 11/09/2014 14:04:14
Ok, I converted the intro room script to Lua.
Do I put the Lua code in place of the original ags room code.
Or is their more?
There's an explanation with script examples in the very first post of this very thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=38765.msg510099#msg510099).

In short, you are to put "Lua.RunScript("whatever_name_i_have.lua");" in game_start, and then put "Lua.Call("my_function_name")" whenever you want to call a function from lua script.
Title: Re: PLUGIN: Lua for AGS
Post by: dbuske on Thu 11/09/2014 21:41:04
Thanks for the replies.  Looks like I won't be able to use this.
Title: Re: PLUGIN: Lua for AGS
Post by: ChamberOfFear on Fri 21/11/2014 08:34:14
Just for information. When I go to the download page it says "Subscription Expired". No downloads available.
Title: Re: PLUGIN: Lua for AGS
Post by: Dualnames on Sun 01/03/2015 05:06:15
http://duals.agser.me/Plugins/LuaForAGS_v4.rar

Found an old cd, and it had a version of certain plugins and one of them happened to be lua.
Title: Re: PLUGIN: Lua for AGS
Post by: TheNaiker on Wed 11/05/2016 08:02:21
Hi,
sorry for writing in this old topic, but I have a problem with this plugin.
I have installed it correctly and it works fine with simple lua scripts. But now, in order to write my real code, I need another lua module (luasql.sqlite, exactly).
In this case, what should I do? I am not expert about how Lua Works and I haven't found any dll for that module; and If I am not wrong the modules I got must be installed directly into lua.

Can you give me any hint?

Thanks.
Title: Re: PLUGIN: Lua for AGS
Post by: Monsieur OUXX on Wed 11/05/2016 09:30:08
Quote from: TheNaiker on Wed 11/05/2016 08:02:21
Hi,
sorry for writing in this old topic, but I have a problem with this plugin.
I have installed it correctly and it works fine with simple lua scripts. But now, in order to write my real code, I need another lua module (luasql.sqlite, exactly).
In this case, what should I do? I am not expert about how Lua Works and I haven't found any dll for that module; and If I am not wrong the modules I got must be installed directly into lua.

Can you give me any hint?

Thanks.
You'd be probably better off asking on the Lua forums, but I'm sure Calin Leafshade will help if he sees your update (did you try PM'ing him?)
Title: Re: PLUGIN: Lua for AGS
Post by: TheNaiker on Wed 11/05/2016 09:41:18
Quote from: Monsieur OUXX on Wed 11/05/2016 09:30:08
Quote from: TheNaiker on Wed 11/05/2016 08:02:21
Hi,
sorry for writing in this old topic, but I have a problem with this plugin.
I have installed it correctly and it works fine with simple lua scripts. But now, in order to write my real code, I need another lua module (luasql.sqlite, exactly).
In this case, what should I do? I am not expert about how Lua Works and I haven't found any dll for that module; and If I am not wrong the modules I got must be installed directly into lua.

Can you give me any hint?

Thanks.
You'd be probably better off asking on the Lua forums, but I'm sure Calin Leafshade will help if he sees your update (did you try PM'ing him?)

I've done that some day ago(with another question, to be honest), but I've got no answer.
You know, I found a "sqlite3.dll" file, but I fear it's not enough - I'd should add something which allows comunication between lua and sqlite, shouldn't I? :P Before asking to official lua forums (where I am not sure they know about this old plugin) I wanted to ask here, I'll be waiting some time. :)
Title: Re: PLUGIN: Lua for AGS
Post by: Snarky on Wed 11/05/2016 17:51:01
I would start by writing a proof-of-concept test just in Lua (without AGS) to access SqlLite. This seems to be how to load a DLL: https://www.lua.org/pil/8.2.html, or you could try this: http://alien.luaforge.net/

If you can't get that to work, you need Lua-specific help. If you can, it should also work in AGS, and if it doesn't there might be something you need to do with the plugin or AGS build.
Title: Re: PLUGIN: Lua for AGS
Post by: TheNaiker on Wed 11/05/2016 19:38:55
Thanks, your second hint looks to be good. I typed this
Code (lua) Select
function readDb (ID)
assert (package.loadlib("sqlite3.dll", "luaopen_luasql_sqlite3"))() 
env = luasql.sqlite3()
con = env:connect("test.db")
res = con:execute("create table person( id integer primary key,name text not null, email text, phone text)")
con:close()
env:close()
a = 12
return a
end

And after compiling and running the "test.db" file has been created without error messages. (I used the A variable to see if Ags runs the script)
I will make further tests, but I am satisfied with this. Thanks again.
Title: Re: PLUGIN: Lua for AGS
Post by: Snarky on Wed 11/05/2016 20:02:21
Sweet! I don't know Lua, but that looks good so far. And thanks for posting your sample code, I'm sure that could be helpful for others.

Hope it works out.
Title: Re: PLUGIN: Lua for AGS
Post by: eri0o on Mon 22/01/2018 21:10:05
Hey! What's the status of this plugin? Is there an update? Is source code available somewhere? The first page link points to an expired wiki.

I played a little bit with love2d this weekend and would like to test using lua to use shaders :]
Title: Re: PLUGIN: Lua for AGS
Post by: Crimson Wizard on Mon 02/07/2018 12:51:17
Quote from: eri0o on Mon 22/01/2018 21:10:05
Hey! What's the status of this plugin? Is there an update? Is source code available somewhere? The first page link points to an expired wiki.

We have this plugin as submodule in our repository. Actual repository is https://github.com/duncanc/Lua-for-AGS/commits/master

The status of plugin is unknown. It seems to be made in such way that requires continious sync with the engine to have full support. Yet it was last updated in 2013, which means it cannot work with the any recently added engine API.
Also the reason I believe we should drop supporting this plugin when developing AGS 4 branch, because plugin is strictly tied to the engine's data layout in memory. Unless there would be a maintainer who rewrites it in a different way.
Title: Re: PLUGIN: Lua for AGS
Post by: eri0o on Wed 28/08/2019 17:18:11
Answering to myself after an year, I just started taking a look into this plug-in to see how the hell it works.

Apparently a part of the code is generated by a script. I also didn't find that script yet.

Unfortunately it doesn't use the proper Engine plug-in API and instead does weird things as CW suggests.

About my own questions on Shaders, which is unrelated to it, but I was too dumm at the time to know, it's a different issue. It would require AGS to expose the texture where screen is drawn or something similar for the plug-in interface, which right now only has the "screen bitmap", which only exists using Software Render and is only exposed on the plug-in interface of Windows build of the engine. Shaders are doable on a plug-in side if it's possible to access the texture that's going to be presented to the screen. It would probably be an OpenGL only feature.