reference objects by index like an array

For questions about using Classic.

Post » Mon Feb 02, 2009 3:36 am

let's say you have a sprite called "monster"
I make a loop that pastes 20 monsters in a row at runtime
then I want to have a loop that does something along the lines of

if monster(loopindex) is dead then monster(loopindex-1) move here

I know it's possible in a roundabout way to reference a certain monster by index
by creating a private variable
called monsterindex
and then making two conditions like

loop i
if monsterindex = loopindex

then your actions

but it's difficult and cumbersome to do something where I select the current loop index
and the next four monsters, and do something slightly different with each

so is it possible to just reference instances by some type of sequential index?
like an array of objects?
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Feb 02, 2009 5:13 am

Why don't you just do:

[code:3pdaaefr]
+Monster.Value('isDead') = "true"
->Move wherever
[/code:3pdaaefr]

That will only affect the monsters that are dead. No need for a loop.

Or if you really need to use a loop then:

[code:3pdaaefr]
+For each Monster
+Monster.Value('isDead') = "true"
->Do stuff
[/code:3pdaaefr]
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Mon Feb 02, 2009 8:23 am

Check out the loop category of conditions in the system object - "For", "For Each", "For Each Ordered", etc.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,574

Post » Mon Feb 02, 2009 9:15 am

I was using the monster dead as an example for simplicity's sake
I actually need to do some interpolation
123456789
let's say the player clicks and drags number 3 down
I want to interpolate 3.y (loopindex)and 1.y(loopindex-2),
and 3.y(loopindex) and 5.y(loopindex+2) to find new positions for 2 and 4
[code:ad8enkr1]1 56789
-2-4-----
--3------
[/code:ad8enkr1]

would there be a way to do something like that?

edit: I just thought of a way that might work, but I won't get to test it out until after work, so maybe create an array that stores the unique IDs of each object instance. would there be a way to say set UID.x to something, or you can't access objects by their UIDs?
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Tue Feb 03, 2009 12:39 am

how bout with python?

[url:2swkm0he]http://www.scirra.com/phpBB3/viewtopic.php?f=9&t=2664[/url:2swkm0he]
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests