Trigger by player press X to interact?

Hello there! I am new to gather town.
I want to track player’s behavior in gather town space.
So I use following nodejs code ():

const game1 = new Game('spaceinfo', () => Promise.resolve({ apiKey: 'apikey'}));

game1.connect();
// optional but helpful callback to track when the connection status changes
game1.subscribeToConnection((connected) => console.log("game1 connected?", connected));


//#1 start
game1.subscribeToEvent("playerMoves", (data, context) => {
  if(data.playerMoves.hasOwnProperty("mapId")){
   unique_id  = Web3Utils.utf8ToHex(context.playerId);
  //  console.log(unique_id);
    send_event_obj ={
      '1': ['gahter_town_id', context.playerId],
      '2': ['map', data.playerMoves.mapId],
      '3': ['gahter_town_name', context.player.name]
                       }
    send_record_code();
      }
});
//#1 end


//#2 start
game1.subscribeToEvent("playerJoins", (data, context) => {
    console.log("joinjoinjoin");
  unique_id  = Web3Utils.utf8ToHex(context.playerId); 

  send_event_obj ={
      '1': ['gahter_town_id',context.playerId]
                       }
    send_record_code();

});
//#2 end


//#3 start
game1.subscribeToEvent("playerInteracts", (data, context) => {
  //console.log(
    // player fields may be undefined as you receive initial game info, so this is
    // a convenient check if `player.name` is known yet, falling back to their ID if not
   //console.log(context);
    //context.player.map,
    //context.playerId,
    //"moved in direction",
    //data
  //);
   itemObj =game1.getObject(data.playerInteracts.objId,context.player.map);
   console.log('playerInteracts');
   console.log(itemObj);
   //itemMap = game.completeMaps(data.playerInteracts.mapId);
   console.log(itemMap)
   object_url='';
   if(itemObj.obj.properties.hasOwnProperty("url")){
     object_url  = itemObj.obj.properties.url;
                                                 }
    send_event_obj ={
      '1': ['gahter_town_id', context.playerId],
      '2': ['map', itemObj.obj.mapId],
      '3': ['gahter_town_name', context.player.name],
      '4': ['object_name',itemObj.obj._name],
      '5': ['object_url', object_url]
                       }
   send_record_code();

});
//#3 end


//#4 start
game1.subscribeToEvent("playerExits", (data, context) => {
  console.log(
    // player fields may be undefined as you receive initial game info, so this is
    // a convenient check if `player.name` is known yet, falling back to their ID if not
    //context,
    context.player.map,
    context.playerId,
    "playerExits",
    data
  );
   send_event_obj ={
      '1': ['gahter_town_id', context.playerId],
      '2': ['map', context.player.map],
      '3': ['gahter_town_name', context.player.name]
                       }
    send_record_code();

});
//#4 end

It’s working on #1, #2, #4.
But on #3, it did not pop anything on nodejs console,
so I can’t record player’s interact behavior.

This code was created by my former colleague,
and it seems worked on 2022. (Maybe? I am not really sure…)

Is there anything wrong on this code?
I think the answer maybe in this page, but I have no any idea.
Thanks!

Hey, there was a change around ~10 months ago which replaced the playerInteracts event with playerInteractsWithObject which uses key instead of objId, and mapId is required, but is otherwise the same.

so you’ll want to subscribe to that instead …

game1.subscribeToEvent("playerInteractsWithObject", (data, context) => ...

and then use getObjectByKey to get the object …

const { mapId, key } = data.playerInteractsWithObject;
game1.getObjectByKey(mapId, key);

check that you also have the latest gather-game-client

Thank for your reply!

It seems I have to upgrade npm package, so I try following code on current directory:

npm up @gathertown/gather-game-client
npm up @gathertown/gather-game-common

But it not pop anything on this code:

game1.subscribeToEvent("playerInteractsWithObject", (data, context) => {
  console.log("playerInteractsWithObject");
}

Maybe there upgrade bash didn’t work(or it wrong ?),
so I try following code:

npm i @gathertown/gather-game-client 
npm i @gathertown/gather-game-common

It still not pup anything when I pushed x to interact object.
(But other event like ‘playerJoins’,‘playerExits’… even ‘playerGhosts’ working normally.)

Then I created a new directory, and run npm install code again:

mkdir node2
cd node2
npm i @gathertown/gather-game-client 
npm i @gathertown/gather-game-common

In this time, it pop wrong message when I run nodejs code to import gather-game-client plugin:

/root/node2/node_modules/@gathertown/gather-game-client/dist/src/Game.js:77
        if (options?.logLevels) {
                    ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/root/node2/node_modules/@gathertown/gather-game-client/dist/src/index.js:17:14)

By the message from “package-lock.json” in new directory,
the version of two package is:
@gathertown/gather-game-client => 43.0.1
@gathertown/gather-game-common=> 35.0.1

… and node version is v8.9.1 .

Is anything I should do?

Thank you!

Hey, sorry about the delay, the problem is the nodejs version doesn’t support the version of javascript we’re using (es2020 in the current package, es2022 in the future). You will need to upgrade to nodejs v18.20.1 or higher.

Thank for your reply!
I will find time to upgrade Nodejs version.