Game items state

Forums: 

Hi, I'm using "Drag and Drop" game, how can I know what image was draged to the drop zone at any given time.
Do I need to add some listener to follow the actions in the game?

otacke's picture

Hi Alex!

Yup, that'd probably be the way to go. The elements of Drag and Drop (draggables and droppables) communicate with each other and with the main component via events, and the draggables and droppables are exposed. So, if you get hold of the instance of Drag and Drop, you can listen to the events - I fear "dragend" is the best you get. You can try a dry run in the dev console in the iframe's context, e.g. with something like

H5P.instances[0].draggables.forEach(draggable => {
  draggable.on('dragend', () => {
    // Access draggable or H5P.instances[0] here as you please
  });
});

Please note, however, that when that event is triggered and you start looking for the data you need (e.g. H5P.instances[0].getCurrentState() for a test run), not all the processing may have been done, so not all information may have been updated already.

Best,
Oliver

Thank you for your quick response

otacke's picture

No problem at all.

Well I'm using your event listener example but on the fist draggable item i'm getting the event triggered but H5P.instances[0].getCurrentState() response is answers: [] on the next one is already correct, so can I get any other object to get the needed data?

otacke's picture

Hi Alex!

Well, I mentioned that when the event is triggered and your callback runs, the content type may not yet have processed anything, so `H5P.instances[0].getCurrentState()` will not yet deliver the correct answer in all cases. You can, of course, access the whole of the content type via `H5P.instances[0]` and retrieve all the information that you want to, but you'll still have to consider timing. Unfortunately there doesn't seem to be an event that's triggered after the processing is done.

Best,
Oliver

Hi Oliver 'xAPI' this event triggered after but only when item is draged to the drop zone

otacke's picture

Hi Alex!

Yeah, I was only looking at the internal events, not those that are dispatched to the platform.

Best,
Oliver