Make Market prototype accessible to players


  • SUN

    Currently most object prototypes are accessible and can thus be extended by players.  However, the Market prototype is not accessible, which prevents players from creating their own market functions directly on the Market object.  Any attempt to access the Market class is met with errors.

     

    I discovered this when I was trying to create a reverse transaction cost calculator.  Since calcTransactionCost() is already on the Market class, I figured I would extend the prototype with my reverse cost function so that all the transaction cost functions would be in the same place.  However I am unable to do so since I cannot access the Market prototype.


  • Dev Team

    In JavaScript, prototype mechanic is needed only when you have many instances of one type. The market object is a singleton so that there is only one global instance. There is no Market prototype at all. You can extend Game.market methods directly:

    var originalCalcTransactionCost = Game.market.calcTransactionCost;
    

    Game.market.calcTransactionCost = function(amount, roomName1, roomNam2) { var originalValue = originalCalcTransactionCost.call(this, amount, roomName1, roomName2); // your own logic }


  • SUN

    I see, thanks - I incorrectly assumed that the Market object was implemented as a class.  

     

    I had tried your suggested approach earlier, but it didn't work - it turns out this is because I was setting the function outside of the main loop.  This tactic works for modifying other prototypes like the Creep that persist in the global scope, but functions I attached to Game.market outside of the loop do not appear to persist.  Presumably this means that Game.market is created anew each tick and does not persist in the global scope like the prototypes on Creep or Room.

     

    Moving the function definition into the main loop solved my issue.  Thanks for the helpful response, and sorry for the false alarm!