Minimal GS - my template for new Game Scripts

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Minimal GS - my template for new Game Scripts

Post by Zuu »

If anyone needs a boilerplate to get started with Game Scripts, I decided to upload my Minimal GS game script. I use it as starting point for new experiments and perhaps someone else might find it useful too.

Files included:
  • info.nut - declarations of the GameScript - change these to your own. Most importantly change the ShortName to a 4-letter name of your own.
  • version.nut - here the GameScript version is defined
  • main.nut - the main class of the GameScript
  • license.txt - the license which will be displayed in-game if user press License button
  • readme.txt - readme file which will be displayed in-game if user press Readme button
  • changelog.txt - changelog which will be displayed in-game if user press Changelog button
  • make_tar.py - a python script for packing a tar with all nut files and translations and stamp it with the current version
Main class
The main class have a basic structure for save/load, main loop and simple event handling.

SuperLib import included, but can be removed
It also come with a few lines to import SuperLib - my collection of common helpers over the years which also contains some helpers contributed by other fellow script developers. However, those lines can easily be removed if you don't want to use SuperLib. Remember then to remove the few cases where SuperLib helpers are used to simplify Minimal GS. If you use GPLv2 you can inline that SuperLib code in your GS but will of course then not as easily get bug fixes included in future SuperLib releases. See the readme for more info.

Get started
Unpack the attached zip in Documents\OpenTTD\game or equivalent directory on your system. When developing, OpenTTD will find your game script as long as it compiles. Only for distribution, you need to pack it as a tar using for example the attached make_tar.py (if you add sub directories for your additional code files, the script needs to be extended to handle that).

Download
Minimal GS-v2.zip
(13.46 KiB) Downloaded 1550 times
Version history
1. Initial release (downloaded 283 times)
2. Version 2
Last edited by Zuu on 05 Nov 2013 20:41, edited 1 time in total.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
rknetwork
Engineer
Engineer
Posts: 8
Joined: 31 Jan 2013 13:44

Re: Minimal GS - my template for new Game Scripts

Post by rknetwork »

It also come with a few lines to include SuperLib, but those lines are easy to remove if you don't want to use SuperLib.
I have commented out the following lines to get rid of SuperLib:

Code: Select all

/*
import("util.superlib", "SuperLib", 24);
Result <- SuperLib.Result;
Log <- SuperLib.Log;
Helper <- SuperLib.Helper;
ScoreList <- SuperLib.ScoreList;
Tile <- SuperLib.Tile;
Direction <- SuperLib.Direction;
Town <- SuperLib.Town;
Industry <- SuperLib.Industry;
*/
But the script is failing to start:

Code: Select all

dbg: [script] [18] [S] Your script made an error: the index 'Helper' does not exist
dbg: [script] [18] [S]
dbg: [script] [18] [S] *FUNCTION [Start()] /root/.openttd/game/gs/main.nut line [62]
dbg: [script] [18] [S]
dbg: [script] [18] [S] [ticks_used] 0
dbg: [script] [18] [S] [loop_start_tick] 2
dbg: [script] [18] [S] [HUMAN_COMPANY] 0
dbg: [script] [18] [S] [this] INSTANCE
dbg: [script] The script died unexpectedly.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Minimal GS - my template for new Game Scripts

Post by Zuu »

You also need to change those lines that make use of the library. It is not used for anything central.

Change "Helper.Max" to "max" to use the builtin max function instead which works fine for integers. I admit this is a silly example use of the library.

Code: Select all

 		this.Sleep(Helper.Max(1, 5 * 74 - ticks_used));
There are also some calls to Log.Info that you have to change so that they use GSLog.Info instead (remove the last parameter about log level).

This should be all you need to do unless I forgot any other usages of the library. That should however show as an error message as you have seen.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Minimal GS - my template for new Game Scripts

Post by Zuu »

I've updated Minimal GS with a second version. It fixes two bugs that I've fixed in my scripts since Minimal GS was released. 1) Detecting if used OpenTTD version has the World Gen Bug and in that case delay initialization to after world gen. 2) If Save() is called before the GS has finished loading, don't attempt to use the usual save methods, instead save the raw table that we did just load from a save game.

In addition to that I've added hooks to execute monthly and yearly tasks more easily. I show that you can display the welcome message in the Story Book on new nightly versions and fall back to GSGoal.Question on older versions. (for the actual code on how to do that, you have to refer to SuperLib which is used here)

Full changelog
  • Add: Provide EnfOfMonth and EndOfYear methods that get called from
    main loop every month/year
  • Add: Greet players using SuperLib.Story.ShowMessage which will
    use the Story Book in recent enough trunk versions but fall back
    to the blue query window in older trunk and 1.2-1.3.
  • Add: Detect if the OpenTTD version used has the World Gen Bug and
    apply counter measures so that it doesn't affect the script.
    If World Gen bug is detected, it will delay script initialization
    to after world gen.
  • Add: readme.txt and changelog.txt
  • Change: Move greeting of new players to event handler (works also
    in multiplayer)
  • Change: Replace Helper.Max() with built in max() method
  • Change: Make it more clear that you may insert your own name as
    author in info.nut
  • Fix: If parsing loaded data did not complete when Save() is called,
    save in-memory copy of loaded data

I've attached the new download to the initial post.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Minimal GS - my template for new Game Scripts

Post by krinn »

I get a look at it, i suggest (not critical) it would be better if function NewStoryPage(company, title, ...) return an array of [page_id, pe, pe, pe...]
With page_id user may use the id to re-add other elements in the page, but your function is there to add multi-elements in a row, so re-use of page_id is not really as useful as editing existing element id.

And as it's then a need to return an array for all "pe"_ids it's then logic to add page_id as 0 index while you're at it, and user get all ids he may need to edit elements or add new element to a page.

As i said, really not critical, as it's an example and it already do its task, as the usage is clear even right now.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Minimal GS - my template for new Game Scripts

Post by Zuu »

Note that this topic is about Minimal GS and not SuperLib. Your comment seems to be about a SuperLib method which is not even used in Minimal GS. I will therefore quote and reply to your question in the correct thread.

Edit: Link to reply in SuperLib thread
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
alc
Engineer
Engineer
Posts: 24
Joined: 04 Sep 2019 18:42

Re: Minimal GS - my template for new Game Scripts

Post by alc »

I've created a GS based on Minimal GS (Thanks Zuu :bow: ) but when I rename it, I can't see it anymore in the game. I edited the info.nut file function GetName() { return "AssistantGS"; } and changed the name of the directory.
On windows, It's located at .../OpenTTD/game/Assistant GS

What else needs to be changed so OTTD can see it?

Thanks,
Alberto
Wormnest
Engineer
Engineer
Posts: 117
Joined: 14 Jul 2013 12:33
Location: Netherlands

Re: Minimal GS - my template for new Game Scripts

Post by Wormnest »

You should also change the four-letter ShortName or however it's called (doing this from memory), this is the real code that has to be unique.
alc
Engineer
Engineer
Posts: 24
Joined: 04 Sep 2019 18:42

Re: Minimal GS - my template for new Game Scripts

Post by alc »

The problem was on the version. It only accepts integers.
Thanls,
Alberto
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 4 guests