Okay, I'm dumb, but I can't figure out how to use the *Generator() methods.

0 favourites
  • 4 posts
From the Asset Store
Sync animations, animation frames, mirror state, sounds and anything else using these methods.
  • I have a companion post about *Instances(), and iterators, but now I am having trouble understanding how to best use *generators

    Specifically, I am looking at *children() , used to be able to iterate all children attached to an instance.

    But how? Needing to relate this back to event sheets, how would I recreate this:

    Pick children of type "A" -> Dothing

    If I simply do this:

    For (const child of someinstance.children()) {
    	If(child.type === Desiredtype) child.DoaThing();
    }
    

    ...Then why is this a generator. What power does that give me, etc... I tried reading the docs for JS, but I need to see it in use here. Code samples would be fabulous in the manual.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • you could potentially use it iterate thru a loop at different points and time. it could defer handling the next element.

    when you use it in a for each loop, I don't think there is an actual difference.

    but the generator has some methods like next() that lets you control when to handle the next item in the collection, here is a silly test

    drive.google.com/file/d/1CCLP2SWMNAX1xuDCV1B3vMrB2DwWe0_2/view

    where I have used these in the past, is for a card game, I have a deck data structure that had a generator function to return the elements, and i would just call deck.next() to get the next card. but this could also be accomplished using a stack or a queue with pop/dequeue,

    where generator excel is the value it returns is lazily evaluated so it's more memory efficient. instead of pulling the entire set in memory it only pull the elements its retrieved

  • Ah, thanks a bunch!

    When the manual calls *Instances() a method to return an iterable, is that the same thing as a generator function? In this case *children()?

  • Generator functions return iterables (and inside the function body they can use yield). An iterable is basically anything you can write for (const thing of iterable) or [...iterable] with. (The definition is a bit more complex but in practice that's what it means.) As ever there's lots more info on MDN.

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