picking objects and functions

Get help using Construct 2

Post » Thu Aug 27, 2015 3:01 pm

Image

Only a single blue sprite2 object is allowed to be created on any black sprite. The Boolean is not being changed so sometime 2 or more blue sprite2 objects are being created on the same black sprite object.

Why won't the function update the boolean?

If i delete the function and just update it with the currently disabled action it works as expected.

I'm guessing it has something to do with the fact that its in a loop, or the fact the sprite has just been created, but i thought adding wait 0 would ensure it was added to constructs internal object list? I've tried it with and without with no effect.

I've also tried different picking methods in the function itself incase that has something to do with it but anyway i though functions cancelled any objects previously picked?
B
43
S
23
G
21
Posts: 735
Reputation: 12,132

Post » Thu Aug 27, 2015 3:13 pm

Replace

Image
with
Image
(0=False;1=True)
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Thu Aug 27, 2015 4:58 pm

Thanks for trying to help, but that doesn't work. It does if i use the disabled action, and not with the function, which is what i'm after.
B
43
S
23
G
21
Posts: 735
Reputation: 12,132

Post » Thu Aug 27, 2015 5:51 pm

@Ethan

the problem is that the System > Create Object takes some time to actually create the object (or at least add the new instance to the list for that object - that seems to happen on the next tick).

So, when the function is trying to Pick the Sprite2 nearest to Sprite, it doesn't know the one you just created exists yet. Therefore, it doesn't pick it, and then the Sprite.isChanged variable is not getting set to true, so the next time through the Repeat loop that Sprite is still available to have a Sprite2 created on it.

Having a function re-loop through every Sprite while in a Repeat loop isn't an ideal way to do things. It would be better to pass the Sprite.UID into the function as a parameter, then in the fuction Pick Sprite by UID. Then you can do what ever you want to it - without having to loop through other instances. You can also pass the Sprite2 UID into a function and pick it as well. (trying to pick it using Pick Nearest doesn't work, but picking it directly by its UID does work).
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Thu Aug 27, 2015 6:04 pm

The wait is messing it up in this case. What it's doing is pushing the function call to the end of the event sheet, and what you want is the call done in place.

When creating a new object this defines when it can be picked:
picking-problem_p517685?#p517685
and
viewtopic.php?f=147&t=116098&p=837060&hilit=toplevel#p837060
"pick by uid" is the exception as it works for any object that was created.

For accessing new object's immediately from a function you should pass that object's uid. Then you can pick the object by uid in the function, and reverse it to pick a sprite from the sprite2.

For ex:
call "hasframechanged" (sprite2.uid)

on function "hasframechanged"
sprite2: pick with uid function.param(0)
sprite: pick closest to (sprite2.x, sprite2.y)
sprite: x = sprite2.x
sprite.y = sprite2.y
--- sprite: set ischanged to true


@allanR
The newly created object is added to the object list at the next toplevel event as per the links above.
B
94
S
33
G
118
Posts: 5,395
Reputation: 75,851

Post » Thu Aug 27, 2015 6:17 pm

@R0J0hound Thanks! that is good to know.
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Thu Aug 27, 2015 6:57 pm

@AllanR @R0J0hound thanks for your explanations.
B
43
S
23
G
21
Posts: 735
Reputation: 12,132


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 20 guests