JavaScript need tip

For AI and campaign script related discussions and questions

JavaScript need tip

Postby Prot » 09 Feb 2017, 20:31

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 ?
User avatar
Prot
Trained
Trained
 
Posts: 57
Joined: 29 Nov 2010, 12:41

Re: JavaScript need tip

Postby Per » 09 Feb 2017, 23:45

We are using the QtScript engine, which I believe is based on an older version of JavaScriptCore.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
 
Posts: 3561
Joined: 03 Aug 2006, 19:39

Re: JavaScript need tip

Postby NoQ » 10 Feb 2017, 01:00

Strange, it's very basic stuff, shouldn't be that much broken.
User avatar
NoQ
Special
Special
 
Posts: 6041
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: JavaScript need tip

Postby Prot » 10 Feb 2017, 09:43

Thanks to all.. i solved it. It's not wz, it my typo in 'udefined'. Shame :oops:
User avatar
Prot
Trained
Trained
 
Posts: 57
Joined: 29 Nov 2010, 12:41

Re: JavaScript need tip

Postby Yome-- » 02 Mar 2017, 18:07

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.
Yome--
Trained
Trained
 
Posts: 41
Joined: 10 Feb 2017, 19:21


Return to Scripting

cron