How to get current trigger and find it in array

I have trigger.js and BumpPositions.js

/*jshint esversion: 6 */


class Trigger extends pc.ScriptType {

    // initialize code called once per entity
    initialize() {
        this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
        console.log('trigger', this.entity);
    }
    onTriggerEnter(entity) {
        console.log('trigger', this.entity);

and part of BumpPositions.js

 fixPos(pos){
        if(this.app.keyboard.wasPressed(pc.KEY_Y)){
            this.data.

but I’m stuck at find the current trigger which reacted with ontriggerenter in my array, maybe because I dont know the index of trigger position
and there modify Y of pos
when I do console.log('trigger', this.entity);
I have the trigger

so add to array each trigger name or update the position not in array but position which was set on trigger entity then push all trigger positions into array?
Like the position was set so entity has position 0,0,0 for example etc.
and next I replace existing array with updated positions
generally this.data is looks like this

[{x: -644.7719116210938, y: 3.1999998092651367, z: -176.37185668945312},
{x: -643.83935546875, y: 3.3999996185302734, z: -180.26043701171875}, ... ]

so this is the position of trigger1 next position of trigger2 and so on
I mean array[0] trigger1
array[1] trigger2
so add name of trigger property? like {x: -644.7719116210938, y: 3.1999998092651367, z: -176.37185668945312, name: 'trigger1'}
I mean to update position of trigger which wasn’t yet set or update position of trigger which was set?
which wasnt set I mean

[{x: -644.7719116210938, y: 3.1999998092651367, z: -176.37185668945312},
{x: -643.83935546875, y: 3.3999996185302734, z: -180.26043701171875}, ... ]

so there are only positions
and which was set I mean the trigger with position like entity has position
what I want in short?
I drive the car into trigger position then get ontriggerenter event and next I want to know which trigger or the name of trigger car reaches so then I can fix the Y coord and the car automatically updates with trigger pos Y

In callback ontriggerenter, the this object is the scriptInstance so you can get the entity which gives you access to the name, position etc.

1 Like

but I want the trigger name in BumpPositions.js so how can I achieve this?
I assume with this scope is only for trigger.js in this case?
I mean not name but only entity I not understand sth
because how with this in BumpPositions.js from Trigger.js I can access the trigger entity?
it should be this.entity but I think it points to BumpPositions not Trigger script?
I mean this.entity will log in console the entity from BumpPositions not Trigger
in short: I want to which trigger car reaches and next find the position of trigger based on name
but I want to do this in BumpPositions.js not Trigger.js
maybe move the logic to Trigger.js not BumpPositions.js?
but there I have issue because then I dont know the trigger because I dont have attached script to it
because I attach the trigger script in BumpPositions.js to trigger
maybe I can recursively attach the script I mean
in Trigger.js do

entity.addComponent("script");
entity.script.create('trigger');

and attach the Trigger.js to Root?

Here, I would either do one of these things:

  • Have each trigger script have a reference to the bump positions script instance and call a function to it to add a position and the entity name.
  • Have each trigger script fire a global event (eg on this.app) that sends the position and entity name and the bump positions script has a listener for the event to add that data to an array
1 Like

yes the approach

maybe I can recursively attach the script I mean
in Trigger.js do

entity.addComponent("script");
entity.script.create('trigger');

and attach the Trigger.js to Root?

is wrong because it makes the RangeError and stackoverflow hmm