spawn object with instance variable

Get help using Construct 2

Post » Thu Jan 30, 2014 9:15 am

hey guys.

i have a game with four players. each of the four character sprites has an instance variable called "player" where the value for the player number is stored.
now i want to respawn them after they died.
and i need something like
spawner -> spawn object with instance variable = 2
i have tried it with adding more conditions like
"character.player = 1" where character is a family ..

i have tried every combination of sub-events and conditions.
the only way to work around would be to have the same sprite for every player but with 4 different animations, just playing the one according to what kind od character he is. but that would mean i had to restructure lots of parts of the game.

a veeeeery simple solution would be to spawn objects by name. then i could use expressions like
spawner -> spawn Object named ("player" & character.player)
but i already suggested placeholders (or wildcards) to scirra and the won't implement it.

can i somehow enter a name instead of choosing a sprite with the .. spawn another object.. .?

im afraid i cant provide a screenshot or a capx because im on my ipad at work.
but i will this evening if needed.
B
23
S
6
G
5
Posts: 132
Reputation: 4,487

Post » Thu Jan 30, 2014 10:38 am

@philx

No, you cannot spawn something based on a variable name, I ran into the same problem.

To my knowledge, there are 2 solutions, the first being the multi-animation method. Using the ReplaceObject tool, it may not be that long.
The other solution though is quite fitting in your solution. In your case, you have a finite, small number of possible sprites. You can have your spawn function starting with 4 IF (a switch case structure), where you do exactly the same thing in the 4 cases, aka you spawn the correct sprite depending on the playerID you received. In those 4 cases, you then call another function, passing the newly spawned player UID as argument, and this function can select the object by UID, selecting it from the family. This is some kind of "casting" as a family if you catch my drift.

Anyway, in case I wasn't clear, here is a capx demonstrating this.
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Thu Jan 30, 2014 12:28 pm

@Guizmus
thank you for your quick answer. i can only imagine half of what you're saying but i'm pretty sure i'll get it when i download your sample capx at home.

that switch kinda thing is also the most convenient solution i came up to.
like
on [all_players] destroyed
- [all_players].number = 1 -> respawn this one
- [all_players].number = 2 -> respawn this one
- [all_players].number = 3 -> respawn this one

where these are [families] and these - subEvents

but my problem was, if i do it lile that, i couldn't get a timer to work, so that the player had to wait different amount of time until he resapwns.

btw .. can i use the change od a value in an array for a trigger?
for example if array.At(expressionForX, expressionForY) turns 0 -> then do this?

it didnt work with "compare at xy" for me, even if i made it a sub event of every tick.
does anybody know a workaround? because this will also solve the problem described above.


thank you very much.

cheers! philphilx2014-01-30 12:55:01
B
23
S
6
G
5
Posts: 132
Reputation: 4,487

Post » Thu Jan 30, 2014 2:06 pm

If you have access to families and use the solution I sent (only with 2 players, not hard to expand to 4), timer shouldn't be a problem.

No you can't "observe" the change of a value. Instead, you can do a function "changeValue", that changes the value and calls another function if the value is now 0. Then, just always call this "changeValue" function instead of changing directly the value.

B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Thu Jan 30, 2014 2:34 pm

@Guizmus
allright, that would have been too easy though xD

yeah, actually my whole game is built with families and arrays. i'll definetly check your capx because i have to have a closer look at those UID's for another task ill come across further in my game development.

i havent played around much with functions yet. i'll check the manual and see if i can build what u'r talking about.
B
23
S
6
G
5
Posts: 132
Reputation: 4,487

Post » Fri Jan 31, 2014 9:45 am

@Giuzmus

soooo i checked your capx and it's a very nice example.
first i tried to do it in only one event by checking if the instance variable of the spawner aswell as the one of the player who dies the same is like parameter 0. that couldnt choose the right player to spawn, so i made it like you with 4 sub events for every player.

you helped me a lot. thank you very much
B
23
S
6
G
5
Posts: 132
Reputation: 4,487

Post » Fri Jan 31, 2014 9:49 am

@philx
Glad it helped. Once you get a hang on how functions and UID work (and work well together), C2 becomes a lot more... fluent ?

I didn't see your capx so it's hard to tell, but if I had to keep track of the dead player, a variable on the spawner, as you said, seems like a good way to go too. You may just have had a little bug in the events.
B
17
S
8
G
4
Posts: 473
Reputation: 6,211


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 42 guests