Static functions?

For AI and campaign script related discussions and questions
User avatar
Duha
Trained
Trained
Posts: 287
Joined: 25 Mar 2012, 20:05
Location: SPb, Russia

Re: Static functions?

Post by Duha »

NoQ wrote: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".
There is only one way to set function name: add it into function defenition

Code: Select all

_global.superFunction = function superFunction() {
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 »

Maybe i can set some sort of callee.caller.UUID (or some cheap alternative) and use that as cache hash, even though it will most likely not be preserved in savegames ...
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Static functions?

Post by aubergine »

It's because of hoisting and scopes. Takes a while to get used to, but persevere, it will all make sesne.

If you define things as vars, then the vars get hoisted to the top of the locla scope, but only assigned a value where they are coded to.

In the case of sandboxes, this means that assuming their value is assigned any later invocations of functions above them will have access to the value via the named variable reference.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Post Reply