Including files in Squirrel

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
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Including files in Squirrel

Post by paullb »

Forgive me if this is a silly question but I couldn't find the answer in the Squirrel documentation ( http://squirrel-lang.org/doc/squirrel2.html )

What is the syntax for including another file? (assuming it is possible. I'm always kinda of a freak about organizing the code correctly)
Mchl
Director
Director
Posts: 611
Joined: 05 Jan 2007 15:50
Location: Poland
Contact:

Re: Including files in Squirrel

Post by Mchl »

User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Including files in Squirrel

Post by Zutty »

For neatness I put all my require() statements in the constructor of my main class. I don't know if that's the "best" way to do it, but I think Squirrel is such a lightweight and simple language that no-one is going to care!! ;)
PathZilla - A networking AI - Now with tram support.
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: Including files in Squirrel

Post by Roujin »

Zutty: hmm, weird. If I remember correctly, this didn't work for me and gave some errors. Worked fine when I wrote them at the beginning of my Start() function though...
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: Including files in Squirrel

Post by paullb »

I'm not sure what they mean by "full code"...
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: Including files in Squirrel

Post by Finaldeath »

I think that means it has to compile, ie; contain a class in it somewhere. Not too sure myself though, I really should test it more.
Finaldeath
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Including files in Squirrel

Post by Zutty »

Sorry I should have posted something last night when I had access to my code (at work now!). I think I can remember the important bits, but this is just from memory...

My require()s go in the constructor of my main class. I have something like this...

Code: Select all

class PathZilla extends AIController {
    ...
    
    constructor() {
        require("common.nut");
        require("PathNode.nut");
        ...
    }
}

...
PathZilla is the rubbish working title of my as yet unreleased AI! :) This snippet above should demonstrate that require() doesn't just insert code line by line at the specified point, it adds definitions into global scope. For instance, my common.nut contains some generic functions...

Code: Select all

function sqrt(n) {
    // Can't remember off the top of my head how this one goes!
    // See codecodex.com and search for "Integer square root"
    local n2 = n / 2;
    ...
}

function array_contains(arr, item) {
    foreach(elem in arr) {
        ...
    }
}
My PathNode.nut contains a class...

Code: Select all

class PathNode {
    tile = null;
    cost = 0;
    ...
}
If require() only inserted code then the declarations would only be made local to constructor (i.e. being undefined everywhere else), however this works perfectly so I surmise that they automatically go into global scope. I don't think it matters where you call require(), as long as its before you actually use the definitions in the require()d files.

The main reason that I put mine in the constructor is to ensure that they aren't declared if my AI isn't started, to help prevent name collisions with other people's stuff. Almost certainly someone else is going to create functions with names like sqrt() or array_contains(), so if make mine global (as I understand it, global scope is global to ALL of OpenTTD and EVERY AI that it loads) they could interfere with another AI, possibly breaking it. I would hope that others do the same to prevent unexpected results.
PathZilla - A networking AI - Now with tram support.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest