Coin Collect and Not Re-spawn

highgroundhighground Member, BASIC Posts: 26

Hi guys ... I'm sure this has been covered a million times before:

Is it possible for an actor be used multiple times on a scene, get collected, and not re-spawn. The obvious example is a range of coins.

We can have the coin collected, play a sound and add to the score ... but we're having difficulty calculating how to use one actor multiple times and have each coin disappear only and not re-spawn.

Any ideas?

«1

Comments

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772

    Can you post the code/behaviors you are using? I would say that the default way actors work is to be used/collected and not re-spawn unless you specifically tell them to re-spawn. So I suspect there is something in your code that isn't working how you expect it to work.

  • highgroundhighground Member, BASIC Posts: 26

    Hi Jamie,
    thanks very much for helping out with this

    I'll explain a bit more just so you get the overall picture...

    The game is a platform game that uses static scenes for each level. (not one large scrolling scene)

    The player starts on the left side of the screen and works his way across platforms to the right, collecting coins as he goes. (if he exits scene1 he appears in scene2 but he can go back and forth between scenes) (It's a castle he's exploring)

    We have ONE coin actor which we copy and paste multiple versions of in each scene.
    The coins can be collected which destroys them individually and adds to a (game level attribute) game.CoinCount which keeps track of our coin score.

    This all works perfectly.

    But, the problem appears when we exit and re-enter a scene as all the coins we have collected reappear.
    We need the coins that we have collected to be removed from the game entirely.

    Now I know we can do it with a game level boolean attribute, something like game.CoinCollected but this means we'd have to create an attribute for every coin in the game which could be hundreds.

    Any help would be amazing.

    thanks in advance
    Alasdair

    PS - I can get the code/behaviours over to you, but I thought with the explanation above you'd understand.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    @highground said:
    But, the problem appears when we exit and re-enter a scene as all the coins we have collected reappear.
    We need the coins that we have collected to be removed from the game entirely.

    The issue you're facing is that GameSalad resets a scene each time you change to it. While game attributes don't reset when that happens, everything else does: actor positions, actor's self attribute values, and scene attribute values. Any destroyed actors will once again be in the scene when it resets.

    The way around this is to keep track of actors that were destroyed and then re-destroy them (those poor actors!) when the scene resets. You can do that by assigning each instance of an actor a table cell (in this case, using game.spawnID to keep track of the spawn order as a row number):

    Spawner actor
    Change attribute game.spawnID to game.spawnID+1
    When tableCellValue(tableName, game.spawnID, 1) = 0
         Spawn actor [actor name]

    Spawned actor
    Change attribute self.spawnID to game.spawnID
    When [actor needs to be destroyed]
         Change Table Cell Value row: self.spawnID col: 1 value: 1
         Destroy actor

    The trick then is to re-spawn only those actors that were not destroyed (that is, only those actors whose table cell values are still zero. You can do that with a Loop over Table or one at a time, as I've shown above. An alternative to the condition to spawn each actor is to have a rule instead that says:

    When tableCellValue(tableName, game.spawnID, 1) = 1
         Destroy Actor

    The difference is that the actor would be spawned and then immediately destroyed whereas the first example I gave would just never spawn that particular actor.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26
    edited May 2018

    Thanks Tatiang,
    I haven't ventured into creating tables yet so this is new ground....

    Can I just check I've got this right:

    We create 2x new attributes

    self.SpawnID (for the coin actor)
    game.SpawnID (game level attribute)

    _------
    We spawn multiple coins from a Spawner actor:

    _------
    Spawner actor
    Change attribute game.spawnID to game.spawnID+1
    When tableCellValue(tableName, game.spawnID, 1) = 0
    Spawn actor [actor name]

    _------
    (this checks a table to see if it is 1 or 0)
    _------

    And in the Coin actor we add this rule.....

    _------
    Spawned actor
    Change attribute self.spawnID to game.spawnID
    When [actor needs to be destroyed]
    Change Table Cell Value row: self.spawnID col: 1 value: 1
    Destroy actor

    _------
    (this changes the coin's self.spawnID into the game.spawnID)
    (and also has the rule to change the table cell value to 1 when we want to destroy the coin)

    _------

    Have I got that right??
    If so, the only thing I don't understand is how to set up the table to be populated with these details.

    Could you just go over that bit and I think I'll have it
    (go slowly as if I'm an idiot!)

    _------

  • highgroundhighground Member, BASIC Posts: 26

    Hi Tatiang,
    sorry I replied to Jamie above initially!! (I should learn to read!!)

    Thanks very much for your reply. It is very very useful.

    I think I have it apart from setting up the table to contain those coin details.

    Can you walk me through it?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited May 2018

    Yes, you have it right. :)

    The table is just a blank table. Create a new table and give it a name. That's it. It only needs to have one column. I would create rows based on the number of coins you will be spawning (e.g. if you know you will always spawn 200 or fewer coins, create 200 rows) or just go above and beyond what might ever occur in the game (e.g. 5000 rows).

    You could also, instead, create table rows dynamically by adding a new row right before you spawn each coin actor but based on the fact that you are resetting the scene, I wouldn't recommend that. It's going to make the coding logic quite complicated.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Also not sure if you're complimenting me on my videos (which are honestly not that great) or more likely @jamie_c on his videos which I recommend. :p

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Tatiang, this is brilliant. Very clear instructions thanks so much. I'll take my first step into the world of tables tomorrow!!

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772

    Sounds like this got sorted while I was at work, glad to hear it! :)

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    @highground said:
    Tatiang, this is brilliant. Very clear instructions thanks so much. I'll take my first step into the world of tables tomorrow!!

    Let us know if you have any questions about tables... there are some great tutorials available. I tend to just Google tables gamesalad as a starting point.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Thanks Tatiang!
    Yes I'll be Googling 'Tables/GameSalad' late into the night!!

    And I'll be back to hassle you if Google fails!!

  • highgroundhighground Member, BASIC Posts: 26

    @jamie_c said:
    Sounds like this got sorted while I was at work, glad to hear it! :)

    Hi Jamie, yes, I have my homework to do on Tables but with a bit of luck we'll have this issue sorted.

    Many thanks to both of you.

  • highgroundhighground Member, BASIC Posts: 26
    Hello Tatiang and Jamie.
    I followed your instructions but the coins kept re-spawning when I returned to the scene.
    ( Definitely my fault but.....

    I've got it working in a different way so that each duplicate coin actor adds a (1) value to a different row in a table when it is collected. (if it is 1 it is destroyed)
    This way we can have the table check if that version of the coin actor is collected or not.

    This works perfectly but it does mean that we have to modify each and every coin actor to change the table row information.

    So
    Coin1 checks row 1
    Coin 2 checks row 2
    etc.

    Is there a better way to do this WITHOUT having to modify each coin actor?

    Here is a WeTransfer link to the stripped down version of the game.
    https://tinyurl.com/ybg7pdxr

    Please feel free to make any changes!!!!!!

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Is there a better way to do this WITHOUT having to modify each coin actor?

    Yep. Without having seen your project file yet, you should probably have a single coin actor. Follow my instructions above and use self.spawnID to determine which table row contains information for each coin instance.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Hi Tatiang,
    thanks for getting back to me!
    We do have just one coin actor.
    We duplicate the actor on each scene and double click on each coin to edit that instance of the actor.

    Each coin is told to change a different row on the table to a value of 1 when it is collected.
    If value on that row is 1 that version of the coin is destroyed.

    I understand what you mean about using self.SpawnID to control the information in the table but I can't get that part to work.

    Do you think you'd be able to modify my GameSalad file to do this. I could then learn from a master!!

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited May 2018

    Flattery will get you everywhere.

    Okay, I looked at your file. The idea of a spawnID uses the fact that GameSalad spawns actors at different times. So the first spawned actor will be assigned self.spawnID=1, the next self.spawnID=2, etc.

    However, since you're not spawning the coins, you need a slightly different system. Fortunately, we can also take advantage of the fact that GameSalad processes actors' rules according to their layer order. Therefore, we can simply assign a unique ID using a counter.

    Also, I'm curious... why are you using separate unlocked instances of the coin actor? What is unique about each coin? If it's just to add a unique table row value, I'd recommend locking the actors (reverting to prototype rules) and using this new method instead to assign coin IDs. It's always best when you can avoid instances because if you end up adding 44 coins and you later decide to change one of the coin actor rules, you then have to adjust it in all 44 copies. Ugh.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Hi Tatiang,
    thanks for this!

    Just to answer your questions:

    I did initially spawn the coins (following your initial instructions) but I couldn't get it to work correctly.

    I'm not against spawning the coins if you think it's the correct way to do it?

    The reason I duplicated the coin actor as multiple 'unlocked' actors was indeed just to add a unique table row value to each coin as you guessed.

    I'm away from my computer at the moment but will jump onto your new version very soon and get back to you!!

    Very excited!

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Here's a version that spawns the coins. You could pull x and y position values from the table but that's a whole 'nother thing.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Hi Tatiang,
    your version works great!! - (I love the fact that the coins are numbered so we can see what is happening!
    I did as you suggested and went back to the prototype coin actor and added the rules there.

    BUT,
    after adding the 'destroy coin' rules back into the prototype I've hit the same issue I had when I did the spawned coins..

    The problem is that when the scene is reset, new coins appear. The CoinID just keeps going up and you get a never ending supply of coins coin 6, 7 8 9 10, then reset the scene coin 11, 12, 13, 14 15 etc.

    Have I done something stupid?

    I've attached my version here

  • highgroundhighground Member, BASIC Posts: 26

    Ps, my v187 is based on your v186b (not your spawning version v186b2).

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Keep in mind that changing or resetting a scene doesn't change game attribute values. So you need to do that manually:

    When [condition]
         Change attribute game.NextCoinID to 0
         Reset Scene

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Wow! That's so simple. Of course!
    Thank you so much.
    I'm going to try it now!

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    You're welcome!

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Sorry Tatiang!
    I'm still hitting issues.

    I've added the reset of the game.NextCoin attribute.....
    Change attribute game.NextCoinID to 0

    I added it to the player actor (right at the bottom) (IF PLAYER IS DEAD)
    and to the GO TO NEXT SCENE and GO TO PREVIOUS SCENE actors at the edge of each scene.



    I've added another 5 coins to scene 2.

    The problem is, as it now resets the game.NextCoin attribute to zero, the coins that are added on scene 2 are being given the numbers 1,2,3,4,5 rather than 6,7,8.9.10 and, as we've already collected 1,2,3,4,5 on scene 1 the coins on scene 2 are automatically disappearing.

    Help!

    Really sorry about this and I really appreciate how much help you are being.
    I've learnt a lot just in the past few hours.

  • highgroundhighground Member, BASIC Posts: 26

    Sorry, here is the GameSalad file

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Hmm... so if you're using the same coin spawner actor on both scenes, you'll need to code things a bit differently. I guess you'll need to keep track of how many coins are spawned in each scene and then reset game.NextCoinID to that value. So if you spawn:

    5 coins in scene 1
    8 coins in scene 2
    6 coins in scene 3,

    ...but you return to scene, you'd want to restart the numbering at 6 (since scene 1 has 5 coins)?

    Then you'd need a table - or I'd actually use a text attribute but let's not get sidetracked -- like this:

    5
    8
    6

    And you'd check the row equal to the scene number. This is all getting pretty complicated. I'm not sure how much more I can help over the forums but I'll try!

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Oh heck, it is getting complicated isn't it?
    I can't believe that simply collecting coins is so complex.

    I really do appreciate your help.
    I guess I'll revert to my original v186 version. It worked but I felt that there was a less complicated way of doing it but perhaps not.

    I was thinking that pretty much all games have a similar 'collection' element so what I'm trying to do must be possible without the complexity?

    You've helped a lot though and I really do appreciate it.

    (If you get a brainwave in the middle of the night and think of a solution then I'm all ears!!!)

    Hmmmm, I keep thinking.... It's only collecting coins...... how hard can it be?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Honestly, it's not that hard. It's just that explaining it all or doing the work for you isn't something I can necessarily do. When I was consulting, I would often build things like this for clients but I'm not doing much of that anymore.

    It's really about keeping track of the number of coins either as a static value (e.g. scene 2 will always have 13 coins) or dynamically (e.g. change attribute game.spawnCount to game.spawnCount+1, spawn actor, repeat).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • highgroundhighground Member, BASIC Posts: 26

    Hi Tatiang, I totally understand.
    You've helped massively and I really appreciate it!

    You've got me thinking about using tables in ways that I hadn't considered so I'll go back over your instructions and work this out.

    I like your idea of using a text attribute in the table. I might try to dynamically add each scene name to a table to isolate the coins on that scene.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited May 2018

    I wasn't very clear but the idea of a text attribute is a bit more advanced perhaps conceptually as well as more efficient code-wise:

    Assume that the character position within the string is equivalent to the row of a table (but here we're not using tables at all).

    So let's say you store the number of coins that will be spawned on each level as a two-digit number. You might have this for the text attribute game.coinsToSpawn:

    05081119

    In other words, 5 coins on level 1, 8 coins on level 2, 11 coins on level 3, and 19 coins on level 4.

    When you want to retrieve a particular level's value, you would assign this expression to an integer attribute:

    textSubStr(game.coinsToSpawn, game.level * 2 -1, game.level * 2)

    If you're getting the hang of tables, I'm not necessarily suggesting you should switch to a text attribute. It's just another way of doing the same thing.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Sign In or Register to comment.