Transport Tycoon Forums

The place to talk about Transport Tycoon
It is currently Tue May 22, 2018 3:51 pm

All times are UTC




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Aug 15, 2012 9:39 pm 
Offline
OpenTTD Developer
OpenTTD Developer
User avatar

Joined: Mon Jun 09, 2003 6:21 pm
Posts: 4535
Location: /home/sweden
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
Attachment:
Minimal GS-v2.zip [13.46 KiB]
Downloaded 605 times


Version history
1. Initial release (downloaded 283 times)
2. Version 2

_________________
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)


Last edited by Zuu on Tue Nov 05, 2013 8:41 pm, edited 1 time in total.

Top
   
PostPosted: Thu Jan 31, 2013 1:46 pm 
Offline
Engineer
Engineer

Joined: Thu Jan 31, 2013 1:44 pm
Posts: 5
Quote:
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:
/*
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:
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.


Top
   
PostPosted: Thu Jan 31, 2013 9:11 pm 
Offline
OpenTTD Developer
OpenTTD Developer
User avatar

Joined: Mon Jun 09, 2003 6:21 pm
Posts: 4535
Location: /home/sweden
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:
       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)


Top
   
PostPosted: Tue Nov 05, 2013 8:49 pm 
Offline
OpenTTD Developer
OpenTTD Developer
User avatar

Joined: Mon Jun 09, 2003 6:21 pm
Posts: 4535
Location: /home/sweden
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)


Top
   
PostPosted: Wed Nov 06, 2013 11:51 pm 
Offline
Transport Coordinator
Transport Coordinator

Joined: Wed Dec 29, 2010 7:36 pm
Posts: 336
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.


Top
   
PostPosted: Thu Nov 07, 2013 8:42 am 
Offline
OpenTTD Developer
OpenTTD Developer
User avatar

Joined: Mon Jun 09, 2003 6:21 pm
Posts: 4535
Location: /home/sweden
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)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000-2018 phpBB Limited

Copyright © Owen Rudge/The Transport Tycoon Forums 2001-2018.
Hosted by Zernebok Hosting.