Set effect issue on dynamically created object

Just started using Construct 2? Post your questions here

Post » Tue Jun 17, 2014 7:39 am

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.

createobject.capx


createobject.png
You do not have the required permissions to view the files attached to this post.
Last edited by codah on Tue Jun 17, 2014 12:09 pm, edited 1 time in total.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Jun 17, 2014 7:57 am

try this
You do not have the required permissions to view the files attached to this post.
B
257
S
43
G
15
Posts: 1,992
Reputation: 41,535

Post » Tue Jun 17, 2014 8:17 am

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.)
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Jun 17, 2014 1:43 pm

From the "how do I FAQ"
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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Tue Jun 17, 2014 3:32 pm

I am sorry I did not understand what was your intention. Kyatric is right in that case thanks Kyatric
B
257
S
43
G
15
Posts: 1,992
Reputation: 41,535

Post » Wed Jun 18, 2014 12:02 am

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

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.

1_picked_sprite.png


I guess you mean this, which does work:

setcolor_working.png


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.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Wed Jun 18, 2014 12:01 pm

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.
You do not have the required permissions to view the files attached to this post.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Wed Jun 18, 2014 10:35 pm

@Kyatric thanks for taking the time. You have helped me understand these issues.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests