Well I’m thinking of implementing something very similar to the Mario Stars system, aka, there’s a checklist (this is how i see it) of stars to get, and you go to each world, get a star, come back, but you can get them in any order. How do you recon I’d go about scripting this? Please don’t provide code samples, I just want some pseudo code to get me started on the idea of scripting this checklist system thing.
The very best way to get pickups like stars is to use triggers believe it or not. The physics engine culls collisions really effectively so you can have thousands littered around.
You can optimise further by having a giant trigger attached to your player which turns rendering on or off (including fading) for these items too if you have just too many of them.
Now the sexy part - which id?. Each star has its own script and own local variable in that script (just drop it into the prefab) - I needed to do this myself.
pseudo:
var index:int;
static var counter:int;
function Awake()
{
index = counter;
counter++;
}
Alright I know I said pseudo but this code is brutally simple. The cool part of Awake is that it is called on creation in the game world. I have only tested this with adding a script in code to a bunch of objects I created, but it correctly gave a unique index to each script that I added to objects in the world.
Now when you collide normally with a trigger pickup, the OnCollisionEnter in the above script should be able to fire a message to your player (you can get to the player via sendmessage or via collision otherCollider etc or simply store a reference to the players script).
It’s lightweight as its only calling code in the pickup prefab on collisions and when they spawn. You can expand upon this with the giant trigger concept (this is mentioned in unity docs btw as an optimization) where you have a giant visibility trigger. When you touch anything, your player can loop through all those it is touching, and update them with bounce anims, render them and so on or simply turn active on or off and have Update() scripts in the pickup prefab. By activating and deactivating you keep it light even with thousands.
Ok, well I already have a counter. I just need to give each one an ID that I can call from other scenes etc (so say, goes into level, gets star 24, and comes back, and star 24 is crossed off the list in another scene). Maybe use playerprefs… write each thing as an int.
so when each banana has it’s own line of code in a master script
like this:
PlayerPrefs.SetInt(star1, 0) // each is stored as a boolean, 0 meaning uncollected, 1 being got. So each time a level loads it gets the line for what the star’s index is, and if it’s collected, it sets it to 1, which could then be read in another scene by the getInt script (which must be seperate), and then do whatever if the star boolean = 1. What do you think? Good method? Prbs wasn’t explained well, but hey!
Thats fine too,
But if you are loading between worlds you probably would need to set it to not destroy on load.
yeah, or because it’s playerprefs, just recall the strings in a script in the next scene, because they are already saved.
hippo, I really liked your way of counting objects. What I have done so far is to add objects to a scene, then I need to count how many and in a GUI I need to manually set the X / Y
With this the Y value will adjust itself according to how many items I actually have in scene.
Fishman, I know this system is not good for you because you need crosslink. So a global “listener” is best as you say where you can read it from all stages