How to target an instance by UID in a script?

0 favourites
  • 4 posts
From the Asset Store
Act on your instances, get/set their properties and variables only thanks to their UID. No more picking trouble! Plugin
  • Hello everyone :)

    So I have this piece of script that works well:

    var textObject = runtime.objects.H1.getFirstInstance();
    var content = textObject.text;
    navigator.clipboard.writeText(content)

    It allows me to copy the text content of the H1 object into the clipboard.

    But now the problem is when I try to target an instance of this object, by its UID stored in a variable "uidH1", with this script:

    var myVariable = runtime.globalVars.uidH1;
    var textObject = runtime.objects.H1.getInstanceByUid(myVariable);
    var content = textObject.text;
    navigator.clipboard.writeText(content)

    I get this error in my console:

    TypeError: runtime.objects.H1.getInstanceByUid is not a function

    I'm not very familiar with JavaScript, and I don't know where else to look. If anyone can help me, it would save me.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If on the eventsheet, you can do the picking in the conditions and then use

    runtime.objects.H1.getPickedInstances()
    

    Outside of the evensheet you can do

    let instances = runtime.objects.Sprite.getAllInstances(); //returns an array of all instances
    let pick = instances.filter(i => i.uid === 3)[0]; //filter the array based on UID. Just replace "3" with your variable.
    	
    pick.angle += 1; // do whatever you wanna do with the object
    
    
  • Thank you very much for your response WackyToaster.

    So I tried your solution:

    var textObject = runtime.objects.H1.getPickedInstances();
    var content = textObject.text;
    navigator.clipboard.writeText(content)

    And I no longer have any error messages in the console. However, where it should copy my text to the clipboard, all it returns is:

    undefined

    I can't seem to get any other results...

    Did I make a syntax error?"

  • Ok it works now.

    I just have to modify the script, adding "First" :

    var textObject = runtime.objects.H1.getFirstPickedInstances();
    var content = textObject.text;
    navigator.clipboard.writeText(content)

    Thanks you again WackyToaster <3

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