Use Lua scripts in mods?

Use Lua scripts in mods?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Use Lua scripts in mods?

Post by hughperkins »

The lua scripts in single player games look awesome. Really impressed by what is possible to do using them.

It could be really fun to make lua scripts available to mods. How easy would this be to do?

Provisionally, one way that this could work is to create a directory within the mod sd7 called "lua", containing an arbitrary number of lua scripts, or perhaps just one if that is easier to implement.

If a lua script is found in this directory, then that script is loaded, and the Update function is called on that script each frame.

*is this something that could be interesting to integrate into Spring core?
*how could I go about adding this in? which files/classes are key?
*is there some way of adding this in without needing to recompile Spring itself, eg create some kindof dll plugin?
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

Assuming the lua code uses the regular VFS functions, just make a startscripts directory in the mod and in game it will appear as if the mod startscripts are in the 'real'startscripts folder. This way it should already be possible to ship lua startscripts (aka single player missions) with mods.

Or do you mean the lua scripts trepan created for GUI modification/overlay?
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Ok, awesome! Do you have some examples? Tried dropping missiontest.lua into a mod, but didnt seem to do anything.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Ok, saw your post in mod forum stating that this wont work for lobby-based games.

Do you mean that it can work for multiplayer games that are not started from the lobby? If I wanted to play one AI vs another in a mod that incorporates a lua script is this possible?

Started to look through the source. Looks like there is a maximum of one script running per spring instance, as set by ScriptHandler::Instance->chosenScript.

Perhaps we could add a file script.tdf to a mod, in the gamedata directory, which can specify a single lua script to autorun?

Code: Select all

[SCRIPT]
{
	autorun=missiontest.lua;
}
Where would be an appropriate location to add code to search for this file, load it if it is found, and instruct ScriptHandler to load this script?
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Looking through the source, it looks like:

The mods are primarily loaded by various dedicated classes which are instantiated in the constructor of CGame.

However, the script will already have been loaded at this point by CPreGame, which in the case of a lobby game will be the "Commanders" script, which presumably creates one commander on each team.

In order to use a custom script, one could do one of the following:
*modify the script.txt to have an (optional) value to specify the game script, defaults to Commanders
**this would imply modifying the lobby to be able to specify a custom game script
*add a new class to parse script.tdf, if available, and return the name of the startup script
**we call this class in PreGame, instead of using the hard-coded Commanders, ie in line 64, 79, 226

The first option, modify start script, sounds like a fairly clean option. It allows a single script to be used across multiple mods and maps, which might be fun.

The second option has the advantage of working with the current lobby. Its sufficient for Use Map Settings type games.

Thoughts?
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Here's a patchfile for the first option: new optional value available in script.txt:

Code: Select all

[GAME]
{
   scriptname=Commanders;
   ...
}
Defaults to Commanders if value not found.

This allows usage via a manually created/edited script.txt file. In the future it could be possible to extend the lobby to allow gui selection of the start script.

http://manageddreams.com/csai/startscript.diff

What do you think?
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Recompiled Spring to test this patch. It works - the script loads and you can move around - but there are messages saying "delayed response" at the start, and it runs quite slowly. What could be the issue here?

startfile:

http://manageddreams.com/csai/script.txt

missionfile:

http://manageddreams.com/csai/lobbymissiontest.lua
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Perhaps it is because the script needs to initialize the ai?

From Commanders.cpp

Code: Select all

				if(!gameSetup->aiDlls[a].empty()){
					if (gu->myPlayerNum == gs->Team (a)->leader)
						globalAI->CreateGlobalAI(a,gameSetup->aiDlls[a].c_str());
				}
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

The major problem here will be that the Commanders script is the only script which supports more then two players. I'm not sure which class actually handles that, it's either CCommanderScript or CGameSetup. (You'll also note that the (C++) commander script basically has two totally different implementations based on whether gameSetup is used.)

Also, mod isn't exactly "loaded" in CGame::CGame. CPreGame always maps the mod and map archives to the virtual file system, CGame::CGame loads the maps and some global mod stuff. Units etc. are loaded on the fly though (somewhere below CGame::SimFrame I think).

Anyway, I think the entire structure needs some overhaul so that startscripts can actually specify number of players needed/allowed and so they can all easily work with gameSetup.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Turns out it works fine. The issue was that Spring was built in Debug. When it's built in Release it runs fine.

Could you patch the patch into the SVN please? Link to patch: http://manageddreams.com/csai/startscript.diff
Post Reply

Return to “Lua Scripts”