Jump to content

TypeError: this.game is undefined - but function is under this context


Recommended Posts

Hello together,


in general I use this.game.global = {} to store global public assets to make them persistent when switching states - and this works very fine. Now I have one case, in which the this.game.global context seems to be unknown to a function, even if this function is part of an object attached to this game global.


Can anyone give me an advice, how to solve it?


My testState (shortend) is:

'use strict';var Party = require('../prefabs/party');var Inventory = require('../prefabs/inventory');function TestState() {}TestState.prototype = {    create: function () {        this.game.global = {party: new Party(), inventory: new Inventory()};        window.global = this.game.global;        // test case        this.game.global.party.addCharacter('Hero', 'mercenary', 10);        //try to without money        this.game.global.inventory.buyWeapon('bronzebow');        this.game.global.inventory.gold = 10000;        this.game.global.inventory.buyWeapon('bronzebow');        this.game.global.inventory.buyWeapon('bronzesword');        this.game.global.inventory.buyWeapon('ironsword');        //try to equip weapon not able to wear        this.game.global.inventory.equipWeapon('bronzebow','Hero');        //equip wearable weapon        this.game.global.inventory.equipWeapon('bronzesword','Hero');        //try to sell equipped weapon        this.game.global.inventory.sellWeapon('bronzesword');        this.game.global.inventory.sellWeapon('bronzebow');        //equip weapon if character has a weapon already equipped, the old weapon has to be deposited before        this.game.global.inventory.equipWeapon('ironsword','Hero');    }};module.exports = TestState;

this.game.global.inventory.equipWeapon(); is the function that throws an exception due "TypeError: this.game is undefined"


The code Inventory Class (shortend) as following:

'use strict';var Weapon = require('../prefabs/weapon');var Inventory = function () {};Inventory.prototype.constructor = Inventory;Inventory.prototype = {    gold: 0,    weapons: [],    addWeapon: function(weaponKey) {        this.weapons.push(new Weapon(weaponKey));    },	    equipWeapon: function(weaponKey, characterName) {        //check if weapon is in list and desposited        for (var i = 0; i < this.weapons.length; i++) {            if (this.weapons[i].key == weaponKey && !this.weapons[i].isEquipped) {                //check if character exists                for (var j = 0; j < this.game.global.party.length; j++) {                    if (this.game.global.party[j].name == characterName) {                        //check if character can use weapon                        for (var k = 0; k < this.game.global.party[j].characterClass.usableWeaponType.length; k++) {                            if (this.game.global.party[j].characterClass.usableWeaponType[k] == this.weapons[i].type) {                                //check if character has a weapon already equipped                                if (this.game.global.party[j].weapon.hasOwnProperty('name')) {                                    console.log('character has a weapon equipped, deposit');                                    this.depositWeapon(this.game.global.party[j].weapon.key, characterName);                                }                                //finally equip weapon                                this.game.global.party[j].weapon = this.weapons[i];                                this.weapons[i].isEquipped = true;                                this.weapons[i].equippedBy = this.game.global.party[j].name;                                console.log('weapon equipped');                                break;                            }                            console.log('character cant use weapon of this type');                            break;                        }                    }                }                console.log('character not found');                break;            }        }        console.log('weapon not found');    },    depositWeapon: function(weaponKey, characterName) {        //check if weapon is in list and equipped        for (var i = 0; i < this.weapons.length; i++) {            if (this.weapons[i].key == weaponKey && this.weapons[i].isEquipped && this.weapons[i].equippedBy == characterName) {                //find character and remove weapon                for (var j = 0; j < this.game.global.party.length; j++) {                    if (this.game.global.party[j].name == characterName) {                        this.game.global.party[j].equipment.weapon = {};                    }                }                this.weapons[i].equippedBy = '';                this.weapons[i].isEquipped = false;                console.log('weapon deselected');                break;            }        }        console.log('weapon not found');    }}module.exports = Inventory;

The exception is thrown in line:

[..]//check if character existsfor (var j = 0; j < this.game.global.party.length; j++) {[..]
Link to comment
Share on other sites

I think that 'game' is not defined in 'Inventory' object. You can pass it into the constructor to have access.

inventory: new Inventory(this.game)...var Inventory = function (game) {this.game = game;};
Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...