I have some basic Squirrel syntax questions.

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
ektrules
Engineer
Engineer
Posts: 1
Joined: 09 Aug 2009 08:28

I have some basic Squirrel syntax questions.

Post by ektrules »

I've been messing around with NoAI for a little bit, and there are some things that confuse me.

In the NoAI introduction wiki article it says to put something like this in main

Code: Select all

class MyAI {
 function start();
}
function MyAI::start() {
...
}
1) Why does "function start();" have no implementation in the class, and why is "function MyAI::start() {...}" outside the class? What does that do? Is "function start();" like an interface or something?
2) Is "function MyAI::start()" static, public, or private?
3) What's the difference between the "::" and "." operators?
4) Does Squirrel support private class methods and private class variables?
5) Are there any editors that (at least) do syntax highlighting for Squirrel? I'm currently using Gedit with Javascript highlighting. That recognizes some things but not all.
6) Not a syntax question but... Since AIList is hard-coded, would it be better to put items in it, and sort the whole thing to get the first few elements in the order you'd like, or use the heap queue class written in Squirrel?
User avatar
Xander
Route Supervisor
Route Supervisor
Posts: 485
Joined: 18 May 2007 12:47
Location: Oxford
Contact:

Re: I have some basic Squirrel syntax questions.

Post by Xander »

ektrules wrote: 1) Why does "function start();" have no implementation in the class, and why is "function MyAI::start() {...}" outside the class? What does that do? Is "function start();" like an interface or something?
It's a standards/readability thing. The idea is that you have a function list at the top of the code and the functions proper lower down. Example: Which is easier to locate and read?

Code: Select all

class MyClass{
    function AddThings(a, b); 
    function SubtractThings(a,b){
        return a - b;
    };
}

function MyClass::AddThings(a,b){
    return a + b;
}
On such a small scale it's not very obvious but on much larger files it's real boon to just be able to look at the top for a function list.
ektrules wrote:2) Is "function MyAI::start()" static, public, or private?
Public. As far as I know squirrel doesn't do public or private.
ektrules wrote: 3) What's the difference between the "::" and "." operators?
:: declares the function, . calls it. To call the previous function you would use MyClass.AddThings(num1, num2);
ektrules wrote: 4) Does Squirrel support private class methods and private class variables?
See 2.
ektrules wrote: 5) Are there any editors that (at least) do syntax highlighting for Squirrel? I'm currently using Gedit with Javascript highlighting. That recognizes some things but not all.
Notepad++ set to C syntax works quite nicely.
ektrules wrote: 6) Not a syntax question but... Since AIList is hard-coded, would it be better to put items in it, and sort the whole thing to get the first few elements in the order you'd like, or use the heap queue class written in Squirrel?
Personally I use AIList as it's well documented in the same place as all the other NoAI functions. I don't know which is better, or even if there is a better/worse option.
Real Tycoons do it on Trains!

JAMI: Just Another Moronic Intelligence
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: I have some basic Squirrel syntax questions.

Post by Yexo »

ektrules wrote:1) Why does "function start();" have no implementation in the class, and why is "function MyAI::start() {...}" outside the class? What does that do? Is "function start();" like an interface or something?
It's a declaration. It's meaning is something like: This class has a function named start without any arguments, and I'll provide the implementation later.
2) Is "function MyAI::start()" static, public, or private?
It's a public member function. In squirrel you can't make any functions / member variables private. There is also no clear difference between static and non-static functions, other than by the way you call it.
3) What's the difference between the "::" and "." operators?
:: is only used when creating a new function that belongs to a class, like in your example above. If I remember correctly it can also be used to indicate you use some global variable, but normally you don't need that. For all other users you'll use a '.' instead.
4) Does Squirrel support private class methods and private class variables?
No
5) Are there any editors that (at least) do syntax highlighting for Squirrel? I'm currently using Gedit with Javascript highlighting. That recognizes some things but not all.
I'm not aware of any. Personally I use crimson editor with a slightly edited C++ syntax file.
6) Not a syntax question but... Since AIList is hard-coded, would it be better to put items in it, and sort the whole thing to get the first few elements in the order you'd like, or use the heap queue class written in Squirrel?
That depends on what you want to use it for. The main strength of AIList is the valuate function. AIList can only use integers as both keys and values. The different heap libraries support any squirrel type as value, and (not sure on this one) might also support different types as key as long as you provide provide a comparison meta-function.
User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: I have some basic Squirrel syntax questions.

Post by Dustin »

Yexo wrote:
ektrules wrote:1) Why does "function start();" have no implementation in the class, and why is "function MyAI::start() {...}" outside the class? What does that do? Is "function start();" like an interface or something?
It's a declaration. It's meaning is something like: This class has a function named start without any arguments, and I'll provide the implementation later.
Is there any difference to Squirrel if you use these declarations? It seems like you could get the same readability effect with comment blocks.
I took them out of my code since they seemed to serve no purpose.

Syntax Highlight?
PSPPad and UltraEdit will allow you to define syntax your own. I haven't tried it, but Visual Studio 2008 has a highlighter. too. VS 2008 express is supposed to work if you download a shell extension from MS first.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4765
Joined: 09 Sep 2007 05:03
Location: home

Re: I have some basic Squirrel syntax questions.

Post by Alberth »

Yexo wrote:
5) Are there any editors that (at least) do syntax highlighting for Squirrel? I'm currently using Gedit with Javascript highlighting. That recognizes some things but not all.
I'm not aware of any. Personally I use crimson editor with a slightly edited C++ syntax file.
vim has a syntax file: http://www.vim.org/scripts/script.php?script_id=2655
(vim has a syntax highlighting file practically every language at the planet).
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: I have some basic Squirrel syntax questions.

Post by Zuu »

I did hack together a language file for Vim that can be found at tt-forums, but it didn't include support for tables and some other stuff, so that came out red in Vim. I found the language file on vim.org recently (I see it was uploaded May this year) and it is way much better than my fast hack based on the C++ syntax file.

That said, unless you know Vim from before or are interested to spend some 30-60 minutes to get started with it, pick another editor. It is great if you spend time to learn it, but if you are not patient enough for that, then pick something else.
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 2 guests