Including files in Squirrel
Moderator: OpenTTD Developers
Including files in Squirrel
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)
What is the syntax for including another file? (assuming it is possible. I'm always kinda of a freak about organizing the code correctly)
Re: Including files in Squirrel
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.
Re: Including files in Squirrel
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...
Re: Including files in Squirrel
I'm not sure what they mean by "full code"...
-
- Engineer
- Posts: 72
- Joined: 09 Apr 2006 23:49
- Location: UK
- Contact:
Re: Including files in Squirrel
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
Re: Including files in Squirrel
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...
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...
My PathNode.nut contains a class...
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.
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");
...
}
}
...
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) {
...
}
}
Code: Select all
class PathNode {
tile = null;
cost = 0;
...
}
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.
Who is online
Users browsing this forum: No registered users and 1 guest