A student has about 80 trees in a scene. When they collide with any of them, they want to restart the scene. How do we easily code this, so that they don’t have to add a line of code for each of the 80 trees? Thanks!
Hi @Kelly_Nickell here are two options to solve this:
For both options all tree’s must grouped together, and this group must be eneabled for coBlocks,
Then for option one, I turn this group into a list by looping through each of the indexes of the group, then adding an event for the player, that if it collides with “another item”, and “another item” is in the tree list, then: restart the scene.
For option two, i loop through each of the tree’s in the group and add an event for each tree that if they collide with the player they must restart the scene; However in this case it would make for 80 seperate events the computer must keep track of which can be quite expensive to run preformance wise, so I’d advice against using this one for that many objects.
Below the coSpace with the code, hope this helps!
There’s an easy way:
- Give every tree the same name, e.g. “Tree”
- Use the advanced collision event to assign the collided item to a variable (enable Advanced CoBlocks under Settings first)
- Check the name of the collided item to see if it equals, or contains, “Tree” (using an IF-THEN control block)
- If it is, restart the scene
Feel free to remix the following example:
Hope this helps! Let me know if you have any further questions. If this post solves your problem, please mark it as the Solution.
Also if you’re using @techleapnz 's method an easy way to keep the name the same is using the hotkeys ctrl+c, ctrl+v to copy and paste the object; This will keep the name identical, instead of using the right-click duplicate method which will add a number behind it.
Oooo, I did not know that - thanks for sharing, @Luc !