Static functions?

For AI and campaign script related discussions and questions
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Static functions?

Post by aubergine »

I've also noticed the slight degradation in error reporting when using closures, that's probably something to do with the way JS API's include() function is working?

Anyway, closures are standard JS, and the sandbox approach is generally considered best practice (which is why practically everyone uses it). I wouldn't want the API to try and invent it's own way of doing sandboxing as that would be very confusing indeed.

Instead, I'd prefer time be spent on finding out what's causing degradation of errors that occur in closures in included scripts.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Static functions?

Post by NoQ »

A few more little benefits from splitting files include being able to see github changelogs for separate files (AI aspects!) and probably easily re-use modules in other projects (dreaming ...).
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: Static functions?

Post by Emdek »

NoQ, could you define those "AI aspects"? That would make things clear also for me. ;-)
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Static functions?

Post by NoQ »

Building structures, producing tanks, researching technologies, tactics and micromanagement, etc.
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: Static functions?

Post by Emdek »

OK, so common parts for all "profiles"?
Then I wouldn't split them, separate changelog for each file is minor benefit, you could simply use prefixes in commits messages to mark these changing specific aspects.
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Static functions?

Post by aubergine »

For people wanting to customise nullbot to their needs, not having to wade through a massive file makes life much easier, and also reduces the chances of them breaking stuff when they make edits.

Also, at some point it may be possible to parse some of the files to display stats or diagrams showing how that particular file is configured.

In any case, NullBot is already split in to several smaller files and has been for many versions and that approach has worked well so far...
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: Static functions?

Post by Emdek »

Well, there is always way like this:
http://www.sqlite.org/amalgamation.html
(for releases to be included in game only)
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Static functions?

Post by aubergine »

Yes, but what's the point? include() pulls in the files nice and easy and by time game starts warzone is looking at a single js file.

Also, the includes are used across several flavours/personalities of nullbot. So if some auto-merge thing was used during build process, you'd just end up with big dollops of code duplicated across several variants of the AI.

I really can't see any problem with using the includes, it's a great way of doing things. There's certainly no benefit whatsoever in merging the includes.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: Static functions?

Post by Emdek »

aubergine, this is example, it doesn't (yet) apply to WZ addons. :-)
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
User avatar
Duha
Trained
Trained
Posts: 287
Joined: 25 Mar 2012, 20:05
Location: SPb, Russia

Re: Static functions?

Post by Duha »

NoQ wrote:Building structures, producing tanks, researching technologies, tactics and micromanagement, etc.
The addition way to clear scope is using modules:

var utils = function() {} # just container

utils.foo = function() { ...
utils.boo = function() { ....

It that way you have only 'utils' name in global scope.

PS. never trust c++ programmers about file size, many cpp files in src dir is over 2000 K lines and one OVER9000 :) Some rumors in internat says file over 1000 is bad.
http://addons.wz2100.net/ developer
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland
Contact:

Re: Static functions?

Post by Emdek »

Duha, bad for developers, good for compilers (unless they are big due to something being done wrong). ;-)

EOT
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Static functions?

Post by NoQ »

Duha wrote:The addition way to clear scope is using modules:
I was considering this, but it makes the code longer and harder to read (: Yet, one of the benefits would be that you always know where to look for a certain function's body.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Static functions?

Post by aubergine »

You can use namespaces (or modules as Duha calls them) and sandboxes together if desired. So you could do something like this:

Code: Select all

// main script
this.NullBot = function(){}; // you can access NullBot from anywhere, it's on the global object
include("path/to/whatever.js");
Nullbot.foo(); // "hello"

Code: Select all

// whatever.js
(function(_global) {
   // private
   var msg = "hello";

   // public
   // NullBot namespace can be accessed from anywhere, even in this sandbox,
   // so no need to prefix with _global.
   NullBot.foo = function() { console(msg); }
})(this);
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
Duha
Trained
Trained
Posts: 287
Joined: 25 Mar 2012, 20:05
Location: SPb, Russia

Re: Static functions?

Post by Duha »

Emdek wrote:Duha, bad for developers, good for compilers (unless they are big due to something being done wrong). ;-)
EOT
IMHO code should be written for reading. (code for compiler is ugly and hard mantained).

NoQ wrote:
Duha wrote:The addition way to clear scope is using modules:
I was considering this, but it makes the code longer and harder to read (: Yet, one of the benefits would be that you always know where to look for a certain function's body.
In some cases it is better to read (especially if you not author). This is good for reusable modules.
http://addons.wz2100.net/ developer
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Static functions?

Post by NoQ »

Now for the "real" arguments.

If i do

Code: Select all

_global.superFunction = function() {
  megaFunction();
}
function ultraFunction() {
  megaFunction();
}
function megaFunction() {
  debug(arguments.callee.caller.name);
}
superFunction writes empty string to debug, but ultraFunction writes "ultraFunction".

:hmm: the same code, being advanced enough, can work completely differently in static and global functions with this sandboxing technique.

Background:
Spoiler:
Post Reply