Two students in my "mobile gaming" course are pursuing final projects that involve some sort of collection mechanic. One student is reskinning the classic breakout game and another student is creating a language learning game that teaches users basic Swedish.
In both games, players are collecting objects along the way. On the final layout, we would like to be able to display all of the collected objects on the screen.
For now, we're not worried about positioning on the page. We would simply like to be able to quickly check to see if an item was collected or not. If the object has been collected, it should be displayed on the final page.
At first, I thought we might be able to do this with families. All of the collectable objects could be added to a family, and a family instance variable (isCollected) could be used to keep track of whether or not the variable was picked up. Then, on the final page, we could just loop through all items in the family and check to see which items should be displayed.
Unfortunately, as you can see from the linked file, this solution doesn't seem to work.
Debugging the file reveals that collecting the item successfully sets the isCollected instance variable, but then the object disappears on the next level. I have applied the 'persist' behavior to the entire family, but that doesn't seem to do the trick.
I also tried making all of those objects global, but then the objects appeared on every single layer, even when they weren't supposed to be accessible.
I would be grateful for any advice. In other programming languages, I would try to tackle something like this by putting references to the objects inside an array. Is that the preferred approach for C3?
Develop games in your browser. Powerful, performant & highly capable.
I would use logic such as on overlapping/collecting penguin, set a global variable itemPeng=1
Then when you get to the final screen, you compare everything that is set to 1, if itemPeng=1 then make the object peng on the final screen visible.
This allows you to have the final screen setup in a way where all items are already displayed in the boxes where they should be but made visible/invisible depending on if you have collected it. The global variables allow you to steer clear of using global objects, persist behaviours and instance variables that can become unreliable when used for something like this.
If you want to get a bit more complex and you're displaying them in the order that they are collected in the game then it kind of becomes a real inventory system. Arrays are good for this, as one is collected you 'push' the item to your inventory array which is a list of items. When you reach the final layout, each object would be displayed according to their position, so the first space is 0,0 of the array, second space is item at 1,0, third space is item at 2,0 displayed in the order they were collected.
There are a few ways to handle this, just better not to use the method where you are pulling the actual object through the game with you, I would use global variables for a simple method, and an array for a more complex approach.
Thank you so much, Plinkie! For game projects with a relatively small scope, I will encourage students to keep track of things with global variables. For larger projects, I will encourage them to use arrays.