global variables?

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
Hephi
Engineer
Engineer
Posts: 72
Joined: 25 Oct 2009 09:56
Location: Belgium

global variables?

Post by Hephi »

A question on global variables. I've noticed no people really using them in their AI's. What would be the 'best' method of keeping
(static) variables like minimum_reliability = 80 and in which file should I store them. I'm thinking of creating a section with
all my variables in the same place so I can easily tweak my AI without having to look for in which .nut file they're used.

local variable

Code: Select all

class One
{
 minimum_reliability = 80;
 constructor()
}
function a(b)
{
 return b+minimum_reliability; //or this.minimum_reliability
}
global variable

Code: Select all

class InMainNut
{
 _minimum_reliability = 80; //prefixed with _ as globals should be according to guideline
}
class One
{
 constructor()
}
function a(b)
{
 return b+::_minimum_reliability;
}
although http://www.squirrel-lang.org/doc/squirrel2.html#d0e38 says that if we omit the :: squirrel will first look for a local variable and if not available look for a global variable. Should we put the :: in the code anyway?

So, to sum up. Could I just move all my values like minimum_population_for_road_access, maximum_loan and the like to my main.nut file? I'm not sure if that will constitute good coding though. How do you guys keep track of those values?
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: global variables?

Post by Yexo »

Globals in main.nut is one solution, you could also encapsulate all settings in a general class Settings and use Settings.my_setting instead of _my_setting. If you want to make changing them even easier you could also add them to the settings list in info.nut so users can change them on-the-fly without having to change your AI code.
Hephi
Engineer
Engineer
Posts: 72
Joined: 25 Oct 2009 09:56
Location: Belgium

Re: global variables?

Post by Hephi »

Ok, thanks for the answer. I guess there's no guideline for the AI builders as to where to put them.

I'm thinking of putting them all in my info.nut because as you say it's also the place where the
user-input settings are 'generated' and it's a pretty much empty file anyway.

EDIT:
actually, I'm having problems calling the settings. If I use MailAI.setting it will look in the main.nut file for the setting
but not in the info.nut file. I could make a GetterFunction for every setting though. Adding them all to the GetSettingsFunction
is another option but would result in too many parameters to alter for the user.

I'm thinking of just putting them all in main.nut as it's the second best place for them.
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests