How do I do this?

Just started using Construct 2? Post your questions here

Post » Sun Apr 16, 2017 2:37 am

I want each object (they are sprites in a tile formation) to find the instance variable of another object (next to it) and act according to the value. How would I do this ?(a great example would be how a tile in minesweeper knows if it has a mine next to it.) :|
All of the images in any of my games are self-created.
B
16
S
4
Posts: 12
Reputation: 935

Post » Sun Apr 16, 2017 8:20 am

'each object' = objectA
'another object' = objectB

'next to it' means (i think) from all objects that are some distance away, the most close one

Add behavior Line-of-sight to objectA
Give it the range that you want

System > For each objectA
(objectA) Has LOS to object (objectB)
(objectB) pick nearest(objectA.x objectA.y)
____ System > compare 2 values ... compare the instance variables
______________act
B
33
S
18
G
28
Posts: 2,470
Reputation: 20,904

Post » Sun Apr 16, 2017 6:41 pm

This is great, but the object (a) only "looks" at the instance variables of objects (all) that are to the side of it. I also need objectA to "look" at the inst. var. of objects (all) on its diagonals...

This has straightened me out a lot. Thank you.
All of the images in any of my games are self-created.
B
16
S
4
Posts: 12
Reputation: 935

Post » Mon Apr 17, 2017 3:46 pm

This would work (I think) assuming that the surrounding objects are of the same type (object):

Let's assume your objects are in a grid tile formation, with each object being the same dimensions as a tile.
That means that any one object has neighbours in its 8 surrounding squares, including diagonals.

We pick instance A, using whatever pick method you want (e.g. touch, a step function, etc)
Set its x and y coordinates to a pair of local static variables, l_x and l_y,
Set its width and height to a pair of local static variables, l_w and l_h
We can now check each for each object in each direction around object A by using a pair of nested "for" loops.

Add these conditions in the next event below the pick event (not as a sub event):

Code: Select all
System|For: "x" from -1 to 1
System|For: "y" from -1 to 1
System |Compare two values: If loopindex("x") does not equal 0
System |Compare two values: If loopindex("y") does not equal 0
System |Pick instance of objectX at (l_x+loopindex("x")*l_w,l_y+loopindex("y")*l_h)


Make sure they are all in the same event.
The first condition is going to check the tiles to the left and right;
The second condition is going to check tiles above and below;
The next two conditions exclude the tile you initially selected, where instance A is situated;
The last condition picks the object instance that is a set coordinate distance from instance A by taking its current (x,y) position and either adding or subtracting its height or width .

You can then grab whatever instance variable you want from the selected instance in the action e.g.:

Set global variable checkThis to instance.myCheckThis.


It gets trickier if you have different object types to check, but the same basic principle applies.

Hope that makes sense!
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Tue Apr 18, 2017 9:08 pm

Could you please give me a screenshot of
Code: Select all
System |Pick instance of objectX at (l_x+loopindex("x")*l_w&,&l_y+loopindex("y")*l_h)
? (Is it nth instance, or all instance, or etc?)

I think I have the wrong Pick Instance for this... Also it won't let me insert a comma, so I stopped. (It's surrounded by &'s.)

I understand everything else, and you will be listed in "special thanks" when my program is done. (with the other person who commented on this post)

Here's what I have so far:
Image

The tiles are objects (sprites).
All of the images in any of my games are self-created.
B
16
S
4
Posts: 12
Reputation: 935

Post » Wed Apr 19, 2017 7:21 am

Loopindex("x") * 20 = relative to LocalX

... means it should be ... LocalX + Loopindex("x")

The conditon = System > Pick overlapping point > with ... x = LocalX + Loopindex("x") and y = LocalY + Loopindex("y")
B
33
S
18
G
28
Posts: 2,470
Reputation: 20,904

Post » Wed Apr 19, 2017 7:24 am

I dont under stand the 'Pick All',
currently it picks all and the sets LocalX and Y to the instance with IID=zero
B
33
S
18
G
28
Posts: 2,470
Reputation: 20,904

Post » Wed Apr 19, 2017 10:11 am

@PixelByPixel

Hi there, you need to use Pick Overlapping Point, not Pick by Evaluate.

And also your local variables should only be referencing the starting object, i.e. the object in the middle - you don't need to pick all.
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Wed Apr 19, 2017 10:56 am

@PixelByPixel

Had to fiddle around with the events a bit (the check for loops not equalling zero was broken, but this works:

https://www.dropbox.com/s/jim9fwrlhqtbf ... .capx?dl=0
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Thu Apr 20, 2017 2:06 am

Message: PixelByPixel can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
@99instances2go
@mekonbekon
Thank you so much!

Would you like to beta-test my game (when available, looking at 1-2 weeks)?

It is a multiplayer version of
Code: Select all
 Conway's Game Of Life
(https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)

You don't have to, but the choice is yours. Thank you again.

*EDIT*
In the program that you sent me, what if I wanted the touched object to count how many shapes are around it? Is it a simple addition, or is the code all wrong?
Last edited by PixelByPixel on Sat Apr 22, 2017 3:49 am, edited 1 time in total.
All of the images in any of my games are self-created.
B
16
S
4
Posts: 12
Reputation: 935

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests