how do I get an object by its variables?

Get help using Construct 2

Post » Mon Feb 24, 2014 4:41 pm

I was looking into arrays, so I created a 2d array, looping it to draw a grid of "Cube" sprites.
Each cube has an X and a Y variables to state their position inside the array.
When clicking on a cube, I want to check the cube near it (X+1, or Y-1 etc...)

Is there a "Cube[X-1 && Y-1]" way to get it?

Transition from real coding to event sheet really confuses me :/

Thanks!
B
18
S
7
G
1
Posts: 160
Reputation: 1,934

Post » Mon Feb 24, 2014 4:54 pm

You can pick an object based on its variable quite simply. Just use conditions "MyObject : compare instance variable", multiple ones in the same event, to "filter" every of the MyObjects to just keep the one that validate every conditions (if none validate everything, the events won't be run).

Or... If you come from "real coding", go for functions then :)

You can do a function "checkCube", waiting for 2 parameters (X and Y).
Functions in C2 work in different ways :
- you can have as many parameters as you like
- a function can use "pause" instruction, but will loose its context (and parameters)
- a function can act (meaning it can use actions and influence the game like any other events)
- a function can also be called "inline", in witch case it will be "evaluated" and the return value will be used where you called the function.
- a function can set a return value.
Here are some examples I made, and the manual
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Mon Feb 24, 2014 5:03 pm

You use the For Each [cube] condition and check if the values of the x ans y variables match your criteria
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Mon Feb 24, 2014 5:49 pm

Guizmus' suggestion is more direct:
http://blackhornettechnologies.com/Cons ... Cubes.capx
B
71
S
22
G
248
Posts: 3,757
Reputation: 138,065

Post » Mon Feb 24, 2014 8:02 pm

Ahhh this is great stuff! With functions, I feel at home ;)
Mission accomplished with ease :)

Thank you very much, everyone!
B
18
S
7
G
1
Posts: 160
Reputation: 1,934


Return to How do I....?

Who is online

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