Best way of interacting with different instances?

Get help using Construct 2

Post » Mon Dec 26, 2011 12:01 pm

I want to have more than one star in the level (1-5 max) that you have to collect, and the game needs to remember which stars have been picked when you reload the same level (for example by changing the color or opacity of the ones you already have collected).

I guess I could do it by creating different sprites for each one, but I'd like to know if there is a cleaner way of doing it with different instances of the same sprite. Thanks.

B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Mon Dec 26, 2011 12:38 pm

I think it's easy, but i don't fully understand what do you want to make.
B
38
S
15
G
6
Posts: 238
Reputation: 7,760

Post » Mon Dec 26, 2011 1:02 pm

If you ever played Super Mario 64 for example, in that game you had to collect 6 different stars in a level, when you collected 1 star, the level finished. And afterwards you could go back to the same level to get the rest of the stars, and the ones you already had appeared greyed out to indicate that you already had those ones.
B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Mon Dec 26, 2011 1:11 pm

Basically you need a way to tell your stars appart (an instance variable, or its position maybe) and a way to store their status across layout (global variables or global array)

For instance, set your star sprites to have an instance variable name 'ID'
Manually set each ID to a unique number each time you create a new star.

In events things could look like :
[code]Global variable pickedList = "" // List of picked stars
System: On start of Layout
System: For "" from 0 to tokencount(pickedList,newline)-2
    Star: ID = tokenat(pickedList,loopindex,newline)
        -> Star: Destroy
Player: On Collision with Star
    -> System: Set pickedList to picketList&Star.ID&newline
    -> Star: Destroy[/code]Yann2011-12-26 13:14:52
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Dec 26, 2011 5:12 pm

Interesting, I wasn't aware newline could be used as the deliminator.
Image Image
B
161
S
48
G
91
Posts: 7,358
Reputation: 67,271

Post » Mon Dec 26, 2011 5:22 pm

I often use "|" but newline looks better and is easier to read
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Dec 26, 2011 5:28 pm

Yeah, and it should display nicely in text as well.
I do wonder if |, or a single character would be faster.
Image Image
B
161
S
48
G
91
Posts: 7,358
Reputation: 67,271

Post » Tue Dec 27, 2011 5:43 pm

Thanks guys, I actually didn't know that you could assign different variables values to different instances of the same object, because some options carry across all instances and that got me confused.

Back to the topic, I managed to do what I wanted using an array and an instance variable, assigning a different value to each instance (ID):



Probably isn't the absolute best way, but at least I understand how this one works. The downside is that I have to copy and edit that part for each star on the level, but since there aren't many (1-3) I guess it's ok.
Also, right now I cannot save the array information since there isn't a way to do it, webstorage is just for variables I think, so I'll have to wait to implement a save system that saves the stars count on each level (which are stored in the array). Suggestions are very welcome.
B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Tue Dec 27, 2011 5:55 pm

[QUOTE=Yann] Basically you need a way to tell your stars appart (an instance variable, or its position maybe) and a way to store their status across layout (global variables or global array)[/QUOTE]

I like to use BINARIES for that kind of storage, but this is not straight forward in C2.

You can asign a bit for each star an flip them if collected.
B
42
S
19
G
12
Posts: 723
Reputation: 13,911

Post » Wed Dec 28, 2011 3:11 am

I see, you might like my bit plugin I should release it
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: GSquadron and 27 guests