 voicelessd Registered User |
Quote
|
| 2026-02-05 22:00:26 |
|
I'm making a very basic game where you are in a maze with zombies, and you are supposed to run from the zombies for as long as you possibly can before they inevitably catch you. I want to make it to where zombies spawn every 10 seconds, however, I can't seem to figure out how to get the clone function to work, and there are no tutorials on YouTube. Can someone walk me through it?
|
 guest_Guest Guest |
Quote
|
| 2026-02-06 07:31:14 |
|
/* <behavior jsname="behavior_EnemySpawner" description="Enemy Spawner"> <property name="SpawnPointsRoot" type="scenenode" /> <property name="EnemyTemplate" type="scenenode" /> <property name="SpawnIntervalSec" type="int" default="10" /> </behavior> */ behavior_EnemySpawner = function() { this.elapsedSeconds = 0; // Tracks how many whole seconds have passed since last spawn this.lastWholeSecond = 0; // Stores the last processed second to detect time change }; behavior_EnemySpawner.prototype.onAnimate = function(Node, TimeMs) { let currentSecond = Math.floor(TimeMs / 1000); // Convert milliseconds to whole seconds // Only update logic when a new second has started if (currentSecond !== this.lastWholeSecond) { this.elapsedSeconds += 1; // Increase timer by one second // Check if it's time to spawn a new enemy if (this.elapsedSeconds >= this.SpawnIntervalSec) { this.elapsedSeconds = 0; // Reset timer after spawning let spawnPointCount = ccbGetSceneNodeChildCount(this.SpawnPointsRoot); // Get total number of available spawn points let randomIndex = Math.floor(Math.random() * spawnPointCount); // Pick a random spawn point index let spawnNode = ccbGetChildSceneNode(this.SpawnPointsRoot, randomIndex); // Get the selected spawn point node let spawnPosition = ccbGetSceneNodeProperty(spawnNode, "PositionAbs"); // Read its absolute position in the world let enemyClone = ccbCloneSceneNode(this.EnemyTemplate); // Create a clone of the enemy template ccbSetSceneNodeProperty(enemyClone, "Name", "enemy_clone_" + randomIndex); // Assign a unique-ish name based on spawn point index ccbSetSceneNodePositionWithoutCollision(enemyClone, spawnPosition.x, spawnPosition.y, spawnPosition.z); // Move the spawned enemy to the selected spawn position } } // Store last processed second to prevent multiple updates in the same frame this.lastWholeSecond = currentSecond; };
Here’s a piece of code I once made for organizing object spawning in CooperCube. I came up with the logic (and actually generated it with ChatGPT) since I’m not a programmer myself.
The idea is very straightforward. You create a regular scene node that serves as a container for all your spawn points. Inside it, you place as many child nodes as you need - these are your spawn points, and you can freely position them around the level directly in the editor like normal objects.
Separately, you create a template node for the enemy (or any other object you want to spawn). This isn’t an actual enemy in the level, but rather a “prototype” that will be cloned.
Then you add this behavior to the root node of the scene, and in its properties you specify:
which node is the parent of all the spawn points,
which node is the enemy template,
and the time interval between spawns.
After that, the behavior runs automatically: every set number of seconds it randomly picks one of the spawn points, clones your enemy node, and places it exactly at that point’s position.
|
 guest_Guest Guest |
Quote
|
| 2026-02-06 07:34:21 |
|
And yeah - if it’s an enemy and you want the prototype to be hidden, you can make the original enemy node invisible, and then add a line like this that makes the cloned enemy visible after it’s created while keeping the prototype hidden:
ccbSetSceneNodeProperty(enemyClone, "Visible", true);
|