Set effect issue on dynamically created object

0 favourites
  • 8 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Please have a look at the attached capx as I'm unsure why the created Sprite (System:Create Object) does not get its colour changed unless I have the 'Wait 0.1 seconds' (you'll see what I mean). I'm guessing it's because it hasn't had time to actually be created. Would it be better to have pre-created objects as I've seen suggested elsewhere? Thanks.



  • try this

  • Hey Lordshiva1948 thanks for the response. The functionality I'm after is to change an object's colour via the SetColor effect, after it is dynamically created. So I'm not sure that your example addresses that? Maybe I'm missing something. Also, why do you set the an object's animation frame to a non-existent frame?

    (My ultimate aim is to use a Family containing 2 different sprite object types and change all of their colour via the Family's SetColor behaviour. However it seems my issue has nothing to do with Familys.)

  • From the "how do I FAQ"

    [quote:24y74j7k]Understand the picking of newly created/spawn instances since R101 - LINK

    As you've noticed yourself you have to put the "wait" action for what you expect to happen.

    It's because the "Create" action takes a tick to perform, it won't be executed "directly" when encountered in the code, but queued and performed on start of the next tick.

    So when in your sub-event you're trying to pick all sprites, there's actually no sprite to pick, since the previous action hasn't been performed yet.

    Nevertheless, in your code, the "Pick all" sub-event is not necessary.

    If you want to apply a color to a newly created object or change any kind of its properties, simply place the action right after the "Create" action, it will do the job it is supposed to.

    The "pick all" condition allows to reset the picking and apply the actions to ALL the instances of that object type and would be useful only if you have several Sprite instances already placed in the layout in the editor for example plus the instance you're creating on start of the layout.

  • I am sorry I did not understand what was your intention. Kyatric is right in that case thanks Kyatric

  • Kyatric thanks for the response. Re the Pick all, that was just some experimentation that I should've removed .

    When you say put the action 'right after the create action', I think I'm misunderstanding because the following does not work. The 'Log' shows 1 picked object. You made me understand why though, because the created object is not available in the same tick.


    I guess you mean this, which does work:


    Without the Trigger Once of course this would be done every tick which is not good. However with the Trigger Once I need a way of re-triggering it whenever I spawn a new object that I need to change it's colour. I guess I just need a global 'TriggerColorChange' variable or something which I'd set every time I create a new object, and check for that in the above Every Tick. I suppose that's not so bad.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well actually, your first example works perfectly as intended, due to how events work.

    I invite you to also check back for anything under the section "Picking" in the How do I FAQ (linked in my previous post).

    When you create an object, only the newly created instance is picked. So Sprite.PickedCount is 1.

    It's hard to see what you're exactly trying to achieve with a single screenshot. I'm assuming you're having several instances of Sprite in the layout, and that's why you would expect Sprite.PickedCount to be superior to 1.

    For it to work, you would have to make sure to have the function executing on a new tick, and especially picking all the available Sprite instances.

    Or instead actually use the Sprite.Count property that will return the number of existing instances of the object type.

    Your second example is just overdoing.

    On "start of layout" only happen once, on start of the layout.

    The Sprite actions "Set effect "SetColor"" apply to the newly instance made with the Create action.

    So in the first example, you see the newly instance getting its new color. And if it is not the color you're expecting, maybe it's because you're assigning the parameter 0 of the color twice, instead of a parameter 2.

    Finally if what you're looking for is "just" applying "SetColor" parameter to a newly created instance, just make a function

    Function CreateSprite

    System: Create Sprite on layer Function.parameter(0) at (Function.parameter(1), Function.parameter(2))

    Sprite: Set effect "SetColor" parameter 0 to Function.parameter(3)

    Sprite: Set effect "SetColor" parameter 1 to Function.parameter(4)

    Sprite: Set effect "SetColor" parameter 2 to Function.parameter(5)

    On start of layout

    Function: Call Function "CreateSprite", 0, 200, 100, 0, 255, 0

    And here you have it. See the attached capx to have a working example.

    All actions referring to an object type placed in the same event and under a Create this object type action will apply to the newly created instance.

  • Kyatric thanks for taking the time. You have helped me understand these issues.

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