Indexs not found [RESOLVED]

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
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Indexs not found [RESOLVED]

Post by s0r00t »

Hi,

Today i started making a little IA for testing. But since i've tried a function (now deleted) for the HQ, all the indexs I use are not recognized :
Example :
Your script made an error : the index "TileUtils" does not exist
Somebody can help me?

Thanks,
Last edited by s0r00t on 24 Feb 2013 16:39, edited 1 time in total.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4765
Joined: 09 Sep 2007 05:03
Location: home

Re: Indexs not found

Post by Alberth »

We need actual source code for helping with these kind of questions (if possible a small example just for demonstrating the problem), there are just too many things that you can do wrong. With guessing it will take ages to find the problem.
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

Code: Select all

class soAI extends AIController 
{
  function Start();
}

function soAI::Start()
{
  AILog.Info("Hello, World ! Please wait, i'm creating my company...");
  AICompany.SetPresidentName("Nyan Cat");
  AILog.Info("President name is Nyan Cat");
  if (!AICompany.SetName("TransTime")) {
    local i = 2;
    while (!AICompany.SetName("TransTime #" + i)) {
      i = i + 1;
    }
  AILog.Info("My company is called TransTime");
  }
  BuildHQ()
  }
  function BuildHQ()
{
   AILog.Info("Building my HQ, please wait")
   local TownsNumber = AITownList();
   TownsNumber.Valuate(AITown.GetPopulation);
   TownsNumber.Sort(AIList.SORT_BY_VALUE, false);
   TownsNumber.KeepTop(10);

   TownsNumber.Valuate(function(t) { return AIBase.RandRange(100);});
   TownsNumber.Sort(AIList.SORT_BY_VALUE, false);

	foreach (town_id, dummy in TownsNumber) {
		local list = AITileList();
		local location = AITown.GetLocation(town_id);
		TileUtils.AddSimmetricRectangleSafe(list, location, 25, 25);

		list.Valuate(AITile.IsBuildableRectangle, 2, 2);
		list.RemoveValue(0);

		list.Valuate(AIMap.DistanceSquare, location);
		list.Sort(AIList.SORT_BY_VALUE, true);
		foreach (t, dummy in list) {
			if (AICompany.BuildCompanyHQ(t)) return town_id;
			}
		}
		return -1;
}
   
I don't have anymore the old code before the error.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4765
Joined: 09 Sep 2007 05:03
Location: home

Re: Indexs not found

Post by Alberth »

It looks like you have forgotten an import of the TileUtils library, whatever that is (I don't have it in my directory).

The name "..Simmetric.." also looks wrong, but as it already breaks on TileUtils, you don't reach that point yet, so that's not a cause for your current error, I think.
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

The HQ code is a test from Terron AI code, so it probably need librarys. I will try with libs and i will edit.

EDIT : Wait, MailAI crash also for an index error.... I'm damned !
kormer
Engineer
Engineer
Posts: 39
Joined: 11 Sep 2004 17:05

Re: Indexs not found

Post by kormer »

s0r00t wrote:The HQ code is a test from Terron AI code, so it probably need librarys. I will try with libs and i will edit.

EDIT : Wait, MailAI crash also for an index error.... I'm damned !
Keep at it. When I was learning, index not found errors usually originated from scope issues such as calling a local variable that was defined someplace else and the current function wasn't aware of it. Typos, and as stated above, not importing libraries that define a function are all possible culprits.

Also this site was a god-send in understanding how the scripts are built:
http://www.squirrel-lang.org/doc/squirrel3.html
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Indexs not found

Post by Zuu »

Mind that OpenTTD do not use Squirrel 3.0. Instead, I would recommend the 2.2 documentation:
http://www.squirrel-lang.org/doc/squirrel2.html
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
kormer
Engineer
Engineer
Posts: 39
Joined: 11 Sep 2004 17:05

Re: Indexs not found

Post by kormer »

Zuu wrote:
Mind that OpenTTD do not use Squirrel 3.0. Instead, I would recommend the 2.2 documentation:
http://www.squirrel-lang.org/doc/squirrel2.html
Thanks, I did not know that.
svetovoi
Engineer
Engineer
Posts: 87
Joined: 12 Oct 2007 14:07

Re: Indexs not found

Post by svetovoi »

Alberth wrote:It looks like you have forgotten an import of the TileUtils library, whatever that is (I don't have it in my directory).
It is just from another file in my AI.
s0r00t, to enable other files like that you need to use require(<filename>) command.
kormer wrote:Also this site was a god-send in understanding how the scripts are built:
http://www.squirrel-lang.org/doc/squirrel3.html
And here you can find API documentation.

Alberth wrote:The name "..Simmetric.." also looks wrong...
:oops:
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

svetovoi wrote:
Alberth wrote:It looks like you have forgotten an import of the TileUtils library, whatever that is (I don't have it in my directory).
It is just from another file in my AI.
s0r00t, to enable other files like that you need to use require(<filename>) command.
I know this, but i don't know what file import. I will try.
svetovoi wrote:
kormer wrote:Also this site was a god-send in understanding how the scripts are built:
http://www.squirrel-lang.org/doc/squirrel3.html
And here you can find API documentation.
The API helped me in some of the ai's code. But some indexs (like COMPANY_FIRST) don't works, i don't know why.

svetovoi wrote:
Alberth wrote:The name "..Simmetric.." also looks wrong...
:oops:
The Simmetric error is your, svetovoi?
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

(need to rewrite message :evil: ) (i don't see the approval message, sorry)

I will try for the require().

For the API, at least 50% of my code came from. But some indexs don't works (COMPANY_FIRST for example).

And for the Simmetric, what can i do?

If i need to rewrite the BuildHQ function, i need help because i don't know how i can make an other HQ system.
Last edited by s0r00t on 24 Feb 2013 14:56, edited 1 time in total.
User avatar
Hyronymus
Tycoon
Tycoon
Posts: 13235
Joined: 03 Dec 2002 10:36
Location: The Netherlands
Contact:

Re: Indexs not found

Post by Hyronymus »

s0r00t wrote:(need to rewrite message :evil: )

I will try for the require().

For the API, at least 50% of my code came from. But some indexs don't works (COMPANY_FIRST for example).

And for the Simmetric, what can i do?

If i need to rewrite the BuildHQ function, i need help because i don't know how i can make an other HQ system.
No, you didn't have too. If you read the message you were given it said your post required approval. If you waited for that to happen it would've saved you time and effort.

EDIT:
Your post was approved 10 minutes after you wrote it. I find that a reasonable timespan for a moderator to pick it up.
svetovoi
Engineer
Engineer
Posts: 87
Joined: 12 Oct 2007 14:07

Re: Indexs not found

Post by svetovoi »

You do not need to rewrite the BuildHQ, just add required file to your script with, well, require(...). Or define/copy missing function directly in your soAI main file.

And simmetric is just an overlooked typo.
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

With the require("tile_utils.nut"), it works, but i have an other problem :

1- After the script is done, the IA crashw without error.
2-I want IA print "HQ built at" + town_id, but it don't works.

Here's my new code :

Code: Select all

require("tile_utils.nut");
class soAI extends AIController 
{
  function Start();
}

function soAI::Start()
{
  AILog.Info("Hello, World ! Please wait, i'm creating my company...");
  AICompany.SetPresidentName("Nyan Cat");
  AILog.Info("President name is Nyan Cat");
  if (!AICompany.SetName("TransTime")) {
    local i = 2;
    while (!AICompany.SetName("TransTime #" + i)) {
      i = i + 1;
    }
  AILog.Info("My company is called TransTime");
  }
  BuildHQ()
  }
  function BuildHQ()
{
   AILog.Info("Building my HQ, please wait")
   local TownsNumber = AITownList();
   TownsNumber.Valuate(AITown.GetPopulation);
   TownsNumber.Sort(AIList.SORT_BY_VALUE, false);
   TownsNumber.KeepTop(10);

   TownsNumber.Valuate(function(t) { return AIBase.RandRange(100);});
   TownsNumber.Sort(AIList.SORT_BY_VALUE, false);

	foreach (town_id, dummy in TownsNumber) {
		local list = AITileList();
		local location = AITown.GetLocation(town_id);
		TileUtils.AddSimmetricRectangleSafe(list, location, 25, 25);

		list.Valuate(AITile.IsBuildableRectangle, 2, 2);
		list.RemoveValue(0);

		list.Valuate(AIMap.DistanceSquare, location);
		list.Sort(AIList.SORT_BY_VALUE, true);
		foreach (t, dummy in list) {
			if (AICompany.BuildCompanyHQ(t)) return town_id;
			}
		}
   AILog.Info("HQ built at" + town_id);
}
   
I know that town_id is not a name, but i just want the sentence to be print.
svetovoi
Engineer
Engineer
Posts: 87
Joined: 12 Oct 2007 14:07

Re: Indexs not found

Post by svetovoi »

s0r00t wrote:After the script is done, the IA crashw without error.
This happens because your scripts stops after BuildHQ() call. In normal situation it should run like forever(until player ends game).
You need to make infinite loop with useful AI behavior inside.
Basically this:

Code: Select all

while (true) {
/* practical AI steps */
}
s0r00t wrote:I want IA print "HQ built at" + town_id, but it don't works.
This happens because BuildHQ() function never reaches your AILog.Info("HQ built at" + town_id). It ends here:

Code: Select all

if (AICompany.BuildCompanyHQ(t)) return town_id;
To print this line you need to move AILog.Info(...) call into Start() function and use number returned by BuildHQ() instead 'of town_id'.
Or put AILog.Info(...) before return:

Code: Select all

if (AICompany.BuildCompanyHQ(t)) {
     AILog.Info("HQ built at" + AITown.GetName(town_id));
     return town_id;
}
s0r00t
Engineer
Engineer
Posts: 9
Joined: 23 Feb 2013 14:09

Re: Indexs not found

Post by s0r00t »

Thanks a lot ! Now it works :) !

END OF LINE.

I think now i should ask help in a other subject.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Indexs not found

Post by Zuu »

s0r00t wrote:For the API, at least 50% of my code came from. But some indexs don't works (COMPANY_FIRST for example).
COMPANY_FIRST belongs to AICompany. (the COMPANY_FIRST constant is documented on the page for the AICompany class) To refer to it you write:

Code: Select all

AICompany.COMPANY_FIRST
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 12 guests