The annoying picking problem (Ashley please clarify it ty)

Get help using Construct 2

Post » Fri Apr 04, 2014 1:52 am

Think most people that have used C2 for a while have experienced this problem, when working with newly created objects. I search the manual and tutorials for something about doing this was not reliable, but I couldn't find it. But please correct me if im wrong, but as far as I remember from others posts, its due to these objects not actually being available or created until next tick, which is how C2 handles this at the very core and therefore cant be easily changed. And therefore this make picking/working with newly created objects unreliable. And the most common way to solve it I believe is to add a wait with 0 or very low wait time.

However since I ran into this problem with my game, as I create an object, which will again spawn another object that are connected to this, which again will connect to another object. And depending on some of the variable of these later created objects, the first object will have different settings. And it works in some cases, meaning that if I do it through a key like "D" it works. However if I do the exact same thing from a function it doesn't work.

So I made a small pointless program to show the problem.

What it does is spawn 3 objects (Clients) of different color, and each have a there color stored in a "color" variable as well. Then it also spawns another object (Master) with the same variable. It will then choose a random client object and copy its color to the masters variable.

I have made it so one uses functions and the other doesn't and also uses a "wait", which will make it work. However if you remove the wait it doesn't work either.

So my question is, as I really don't like the idea of having to slow down my game on purpose due to this, and because it doesn't solve my problem anyway when using functions.

How do you do this probably and can you do it without using a wait, really hope you can clarify this for me Ashley or someone else with a capx. As its probably the number one thing that annoys me? :D

(Check the "color" variable in the debugger for the master object, after clicking one of the buttons.)
You do not have the required permissions to view the files attached to this post.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Fri Apr 04, 2014 2:57 am

I feel your pain trying to keep things straight when calling multiple functions against a newly created object. However, you're actually running into a different issue here. Check this out in the manual: https://www.scirra.com/manual/142/families
Picking families in events
Families pick instances in the event sheet independently of the object types in the family. For example, consider Family1 consisting of SpriteA and SpriteB. Conditions for Family1 will never affect which SpriteA and SpriteB instances are picked. It will only affect which instances are affected when running an action for Family1. Likewise, conditions picking SpriteA and SpriteB instances will never affect which instances are picked in Family1. In other words, in the event sheet families are treated like an entirely separate object type, which just happens to have instances from other object types.


So, for example, in your Button2 On clicked event, you're creating child objects and then trying to pick randomly against the Family. The Family is a different object, and can't see the child objects until you hit a top level event. If you were to instead create a new Family1 object instead of the red/green/blue objects, it then works (however that's not an ideal solution either because when you create a new Family object, it randomly picks a child object to create). Also, you don't need the Wait in this case.

As far as the function calls (Button1), you're running into three issues :). The first is the same as the issue above, where you're creating red/green/blue objects, but then trying to pick a Family object in the "Choose random color" function. The second issue is that you're assigning Master's color to the return value of the current function, not the one you're calling.

When trying to assign something to the return value of a function, use this:
Code: Select all
Set Color to Function.Call("Choose random color")


But there's one more thing, and this is the thing you're talking about in your post, is that the object isn't ready for picking unless you specify UID (like you do in the first two function calls). So, to put all three things together and get the desired outcome,

1) Create a Family object in the "Set color" function
2) use
Code: Select all
Set Color to Function.Call("Choose random color",Family1.UID)

3) Delete the "Pick a random Family1 instance" condition and replace it with Family1 > Pick instance with UID Function.Param(0).

I realize this doesn't completely get you where you want to be because you might not want to create random objects, but hopefully it clears up what's going on with picking :)
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
38
S
5
G
4
Posts: 79
Reputation: 5,355

Post » Fri Apr 04, 2014 3:09 am

i see its already been handled :) i had this solution..
https://dl.dropboxusercontent.com/u/61666915/pickingproblem_test.capx

but i agree that its really a pain this issue, especially if you don't know of this, and how to work around it,

having the javascript console open can really help,, it showed an error on the color function
+ with the browser plugin, you can quickly log returnvalue

ps: i think the only problem was the "pick random family instance"
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Fri Apr 04, 2014 3:16 am

You probably won't like my suggestion any better than the others, but this has been debated many times before, and I doubt things will change. You just need to structure your code to create at a top level event and pick after, at the same level.
(Only addressed 'Start function' button).
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
71
S
21
G
221
Posts: 3,651
Reputation: 124,231

Post » Fri Apr 04, 2014 3:25 am

So, for example, in your Button2 On clicked event, you're creating child objects and then trying to pick randomly against the Family. The Family is a different object, and can't see the child objects until you hit a top level event. If you were to instead create a new Family1 object instead of the red/green/blue objects, it then works (however that's not an ideal solution either because when you create a new Family object, it randomly picks a child object to create). Also, you don't need the Wait in this case.

Yeah that's pretty huge problem, agree. Because well first of all spawning family objects are rarely very useful if you need to be precise :D
However in my game these objects are not part of families, but it still occurs however I can fix it by using a wait and make the call from a keystroke but not if I make the call from a function. I simply made the example using a family to make it faster to make. But ill try to make a more precise copy.

The second issue is that you're assigning Master's color to the return value of the current function, not the one you're calling.

There are nothing wrong with the way I use the return value, both ways work. If you make a new return in "Choose random color"
Code: Select all
Set return value to "Hello"

And remove the pick random Family1, it have no problem setting the value of the master using this return value.

I realize this doesn't completely get you where you want to be because you might not want to create random objects, but hopefully it clears up what's going on with picking :)

Yeah that will get me absolutely no where as these things shouldn't be random, but also as I mentioned they are not part of families, so in this case its not even possible :)


EDIT: In regards to them not being families, it might be because its all in functions and therefore it never hits top level. Just a thought.
Last edited by nimos100 on Fri Apr 04, 2014 3:42 am, edited 1 time in total.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Fri Apr 04, 2014 3:33 am

vtrix wrote:i see its already been handled :) i had this solution..
https://dl.dropboxusercontent.com/u/61666915/pickingproblem_test.capx

but i agree that its really a pain this issue, especially if you don't know of this, and how to work around it,

having the javascript console open can really help,, it showed an error on the color function
+ with the browser plugin, you can quickly log returnvalue

ps: i think the only problem was the "pick random family instance"

The way you have done it in "Set color" when you pass UIDs is also how I do It in my game.
Last edited by nimos100 on Fri Apr 04, 2014 3:40 am, edited 1 time in total.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Fri Apr 04, 2014 3:38 am

blackhornet wrote:You probably won't like my suggestion any better than the others, but this has been debated many times before, and I doubt things will change. You just need to structure your code to create at a top level event and pick after, at the same level.
(Only addressed 'Start function' button).

Im not really expecting it to change, just to know if it was possible some how :D However your suggestion is probably what ill end up doing, just redesigning it, and link them together in another way.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Fri Apr 04, 2014 3:47 am

There are nothing wrong with the way I use the return value, both ways work. If you make a new return in "Choose random color"
Code: Select all
Set return value to "Hello"

And remove the pick random Family1, it have no problem setting the value of the master using this return value.


I apologize, you're right about this. I just tested it out and learned something new :)
ImageImageImage
B
38
S
5
G
4
Posts: 79
Reputation: 5,355

Post » Fri Apr 04, 2014 3:49 am

I apologize, you're right about this. I just tested it out and learned something new :)

Me to had actually forgotten all about top levels :D

EDIT: In regards to them not being families, it might be because its all in functions and therefore it never hits top level. Just a thought.

However do you know if this is the case, because that might explain it I think?
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Fri Apr 04, 2014 4:03 am

EDIT: In regards to them not being families, it might be because its all in functions and therefore it never hits top level. Just a thought.

However do you know if this is the case, because that might explain it I think?


That's the conclusion I came to when I was trying to do something similar in my last project. It was a very text-heavy educational game, and I wanted to streamline the sprite font creation process, but once I got past the first function call I lost the newly created spritefont and had to go with a more primitive brute force approach :). If I were to go back and try again perhaps I'll see that I was just doing something else wrong, but I was at it for quite some time trying to get it working without adding Wait 0s everywhere!
ImageImageImage
B
38
S
5
G
4
Posts: 79
Reputation: 5,355

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 1 guest