Targeting a specific Enemy instance using uid

  • Hi everyone, just want to mention that I am new to Construct. I spent most of my time understanding the event sheet system. I feel pretty comfortable with it now. So I started exploring the newly added scripting capabilities of C3. I'm progressing, but I hit a wall and I haven't been able to figure out how to move past it... I know some JavaScript but I am in no way a crack at it. Anyways, I would appreciate your help figuring out how to destroy a specific Enemy instance using the uid or other method. Here is my code:

    runOnStartup(async runtime => {
    	runtime.addEventListener("tick", () => Tick(runtime));
    });
    
    function Tick(runtime)
    {
    
    // 	const enemy = runtime.objects.Enemy.getFirstInstance();
    // console.log(enemy["uid"]);
    
    	// Test for collisions between enemies
    	for (const enemyInstance of runtime.objects.Enemy.instances())
    	{
    		CheckEnemySelfCollision(enemyInstance, enemyInstance.uid, runtime);
    	}
    
    	function CheckEnemySelfCollision(enemyInstance, enemyUID, runtime)
    	{
    // 		console.log("CheckEnemySelfCollision function called");
    // 		console.log(enemyInstance.uid);
    // 		console.log("Enemy ID: " + enemyUID);
    
    		for (const otherEnemies of runtime.objects.Enemy.instances())
    		{
    			if (enemyInstance.testOverlap(otherEnemies))
    			{
    				console.log("Enemies overlapping (" + enemyInstance.uid + ", " + otherEnemies.uid + ")");
    				
    // 				console.log(Object.getOwnPropertyNames(enemyInstance));
    				
    // 				Object.getOwnPropertyNames(enemyInstance).forEach(
    // 					 function (val, idx, array) {
    // 						console.log(val + ' -> ' + enemyInstance[val]);
    // 					 }
    // 				);
    				
    // 				enemyInstance.destroy(); // Will destroy both Enemy instances colliding. 
    				enemyInstance[enemyInstance.enemyUID].destroy(); // Uncaught (in promise) TypeError: Cannot read property 'destroy' of undefined
    			}
    		}
    	}
    }
    

    Thank you for your time.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I have resolved my issue.

  • I have a lot to chew on but my future self will definitely be asking you about this -

  • I have resolved my issue.

    Hi DFORMS

    Do you mind sharing how did you do it?

    I can't find anywhere on the Manual or the C3 Examples

    I would like to know how to Pick an instance by UID From a dictionary Value or a variable or just how to pick by UID in general

    Ps: I'm new to Js so I have no clue at all I'm just learning

    Thanks

  • Hi tarek2, I just saw the notification for your post in my email. I'm a little busy this morning with work, but I will post later today.

  • I've written a

    function

    that helps you filter from the criteria in the event table to whatever you want to filter.

    	//alert('Hello world!');
    	 function getIns(a,runtime) {
     const ins =runtime.objects[a];
     for(const insins of ins.getPickedInstances())
     {
     return insins;
     }
     
     }
    
    	fun 
    

    You can get the

    name of the obj

    you want to get!

    	//alert('Hello world!');
    	const player =getIns("player",runtime);
    	You got it!
    

    Of course, this is just my way! If there is a better and faster way, please let me know! I want to share him, you can get started quickly!

  • Thanks a lot chunchun really helpful

    DFORMS No worries mate, thanks for responding

  • Hi tarek, sorry for not replying yesterday. I was swamped... Alright, so I went back and had a look at what I came up with. I ended up using the event-sheet and a 2D array.

    Edit 1:

    Patrol 0 is moving left and patrol 1 is moving right.

    Edit 2:

    I only use the scripting when I can't get something to work with the event-sheet. Which is not often.

    Let me know if you have any additional questions.

  • Hi tarek, sorry for not replying yesterday. I was swamped... Alright, so I went back and had a look at what I came up with. I ended up using the event-sheet and a 2D array.

    Edit 1:

    Patrol 0 is moving left and patrol 1 is moving right.

    Edit 2:

    I only use the scripting when I can't get something to work with the event-sheet. Which is not often.

    Let me know if you have any additional questions.

    Ho no worries thanks very much)

  • You're welcome and good luck!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)