spawn object with instance variable

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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! phil

  • 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.

  • 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.

  • 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

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)