JavaScript need tip

For AI and campaign script related discussions and questions
Post Reply
User avatar
Prot
Trained
Trained
Posts: 242
Joined: 29 Nov 2010, 12:41

JavaScript need tip

Post by Prot »

I have a problem, i want to create function to remeber hard calculated-functions answers and store it in some while, i need check the multidemensional array, but warzone return error in code, i use this code for example:

Code: Select all

var _gi = [];
function getInfo(x,y,com){
  if(typeof _gi[x] !== 'undefined'
  && typeof _gi[x][y] !== 'undefined'       // <-- here error
    && typeof _gi[x][y][com] !== 'undefined') { //...code here }
  }else{
    return _gi[x][y][com];
  }
}
Error like "TypeError: Result of expression '_gi[x]' [undefined] is not an object.", i create test file on nodejs and it's work like a charm:

Code: Select all

#!/usr/bin/js

var ref = 100; // How many m.seconds do we remember
var _gi = [];
function getInfo(x,y,com){
        if(typeof _gi[x] !== 'undefined'
        && typeof _gi[x][y] !== 'undefined'
        && typeof _gi[x][y][com] !== 'undefined'
        && _gi[x][y][com].time > (new Date().getTime()-ref)){
            return _gi[x][y][com]; // We already know the answer, quickly returned 
        } else {
            _gi[x] = [];
            _gi[x][y] = new Object();
            _gi[x][y][com] = new Object();
            _gi[x][y][com].time = new Date().getTime();
            for(var n=0;n<50000000;n++){;} // heavy-heavy code here
            _gi[x][y][com].value = true;
            console.log("getInfo(): populate/return");
            return _gi[x][y][com];
        }
}

for(var i = 0; i < 10000; i++) {
    console.log(new Date().getTime()+": ", getInfo(2,3,"test"));
}

console.log(getInfo(2,3,"test").value);
I check the stackoverflow, and find answers: http://stackoverflow.com/questions/5134 ... javascript
The logic will return false if the first test returns false, and tests the second dimension only if the first one is true.

if(a[1] == undefined && a[1][2] == undefined)
Yeah, i do already this, and it work in nodejs, but in WZ this trick don't work.
I think in WZ javascript logic trying to compare all the passed variables in the "IF" directive..
What engine of js using in WZ ?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: JavaScript need tip

Post by Per »

We are using the QtScript engine, which I believe is based on an older version of JavaScriptCore.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: JavaScript need tip

Post by NoQ »

Strange, it's very basic stuff, shouldn't be that much broken.
User avatar
Prot
Trained
Trained
Posts: 242
Joined: 29 Nov 2010, 12:41

Re: JavaScript need tip

Post by Prot »

Thanks to all.. i solved it. It's not wz, it my typo in 'udefined'. Shame :oops:
Yome--
Trained
Trained
Posts: 41
Joined: 10 Feb 2017, 19:21

Re: JavaScript need tip

Post by Yome-- »

Hi,

(just for tips and helps)

A better way to to this test is just testing if the operand truly exist. Javascript is a powerfull language, when well used.

Maybe this could be better (so far, i do not exactly know how the QtScript engine is fine ;-) ) :

Code: Select all

    var today = new Date().getTime()-ref);
    if(_gi[x] && _gi[x][y] &&  _gi[x][y][com] && _gi[x][y][com]['time'] && _gi[x][y][com]['time'] > today) {
        ...
    }
.pace.
Post Reply