How to get length of array?

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
Kev!
Engineer
Engineer
Posts: 37
Joined: 21 Jun 2011 16:30
Location: In Da House!
Contact:

How to get length of array?

Post by Kev! »

Howdy,

If I have this...

local oink = ["foo", "bar", "lar", "lar", "lar"];

how do I get the number of elements in oink?


Please advise,
Kev! ?(
I DO like a nice caboose...
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: How to get length of array?

Post by krinn »

thatsize=oink.len();
User avatar
Kev!
Engineer
Engineer
Posts: 37
Joined: 21 Jun 2011 16:30
Location: In Da House!
Contact:

Re: How to get length of array?

Post by Kev! »

Hmm...

I could have sworn i got an error trying that? something about no such index?

Kev!
I DO like a nice caboose...
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: How to get length of array?

Post by krinn »

because last index is n-1

0..thatsize-1
Fingon
Engineer
Engineer
Posts: 10
Joined: 28 Apr 2008 18:49

Re: How to get length of array?

Post by Fingon »

it's actually

local size=oink.len();

if you forget the local you'll get the "no such index" error.
User avatar
Kev!
Engineer
Engineer
Posts: 37
Joined: 21 Jun 2011 16:30
Location: In Da House!
Contact:

Re: How to get length of array?

Post by Kev! »

Howdy,

I thinks I must have been mixing up the local array which uses .len() with some AI piece that uses .Count(). I was using both types and trying each method on both, when they each need their own.

Thanks for your responses,
Kev!
I DO like a nice caboose...
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 11 guests