Navigation

    forum

    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    1. Home
    2. KnackwurstBagel
    • Flag Profile
    • block_user
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Groups
    • Blog

    KnackwurstBagel

    @KnackwurstBagel

    4
    Posts
    911
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    KnackwurstBagel Follow

    Posts made by KnackwurstBagel

    • RE: [SOLVED] room prototype stats problem reading length of hostiles found

      Hi Wheasle, Sorry for the delay in responce.  The function I wrote is to be used as a prototype, as such its not meant to be called directly.  The way you would use my code is by doing something like

       

      [main]

      require('logs.test')();

      ...

      var stats = Game.spawns['Spawn1'].room.stats();

      [/main]

       

      The first line I am not assigning to a variable because the import is a function that needs to run immediately in order to register my prototype

      A prototype is a way we can add functionality to objects we did not make.  The game has a room object , all objects have prototypes, the code I wrote registers my custom function with the room object in order to create a new method called stats in this  particular case.  What it does is create a new object for you which has usefull information about your current attacker, in this format:

      {
                      hostiles: foes,
                      numHostiles: foes.length,
                      hostileUser: foes[0].owner.username

      };

      So the idea here by doing something like var stats = Game.spawns['Spawn1'].room.stats(); is we get a new object called stats with three fields populated.  Lets say we have one attacker in your room and its SomeUser, and that person has 5 of their attacking creeps in your room:

      stats.hostiles would be an array of the 5 creeps your enemy has in your room (from what I remember, I havent played in a week)

      stats.numHostiles would be 5

      status.hostileUser would be 'SomeUser'

      Your script now knows that SomeUser has 5 of their creeps with attack body parts inside your room.

      I don't know how well my code works yet, just that its not throwing any errors, I havent had time to check.  I hope that made some sence.  I recomend searching for videos on youtube on the subject of prototypes, there are many good ones out there that explain it well.

       

       

       

       

      posted in Help
      KnackwurstBagel
    • RE: Steam subscription problem

      I am also on Steam, if this is a problem I guess I will wait till my 30 days ends to purchase the subscription so I don't run into the same problem.  I hope they resolve this for you and others.

      posted in Help
      KnackwurstBagel
    • RE: [SOLVED] room prototype stats problem reading length of hostiles found

      Ok well it looks like ive solved my problem so I wanted to update this post, it seems what was happening was because there were not any hostiles, I was indeed getting a empty array, and then trying to do stuff on an element [0] of an empty array.  Also I somehow mistyped the username code too:

       

      module.exports = function(){
          Room.prototype.stats = function(){

              var foes = this.find(FIND_HOSTILE_CREEPS, {
                  filter: function(object) {
                      return object.getActiveBodyparts(ATTACK) == 0;
                  }
              });
              
              if (foes.length > 0){
                  return {
                      hostiles: foes,
                      numHostiles: foes.length,
                      hostileUser: foes[0].owner.username
                  };
              } else {
                  return {
                      hostiles: foes,
                      numHostiles: 0,
                      hostileUser: undefined
                  }
              }
          }
      };

       

      So my solution was to test for an empty array and return a different object if its empty such that I am not accessing any methods of an undefined which I would have been getting with foes[0]

      posted in Help
      KnackwurstBagel
    • [SOLVED] room prototype stats problem reading length of hostiles found

      Update, I have solved the problem by checking if the array returned from the find function is empty, and returning an alternate object if its empty, such as not to access any methods on a undefined element at foes[0] in the event the room has no hostiles.

      SEE MY REPLY FOR UPDATED CODE

      I created a prototype method for the Room object called stats with one of the goals being to find information on current room hostiles.  The problem I am having is my code fails with 'cannot read length property of undefined' which I can't understand why this is happening.  Can someone please help me understand:

      [code]

      module.exports = function(){
      Room.prototype.stats = function(){
      console.log('this room',this);
      var foes = this.find(FIND_HOSTILE_CREEPS, {
      filter: function(object) {
      return object.getActiveBodyparts(ATTACK) == 0;
      }
      });

      return {
      hostiles: foes,
      numHostiles: foes.length,
      hostileUser: foes[0].length
      }
      }
      };

      [/code]

       

      it would seem that foes is undefined, however when i check with a if (foes === undefined) it returns false.  if i do a JSON.stringify(foe); I get what looks  like an empty array []

       

      Help 🙂

       

      posted in Help
      KnackwurstBagel