Sometimes when I try to spawn entities through a for loop, I want it to spawn a set amount of entities into the scene, but most of the time, it doesn’t spawn as many as I want it to. For example, if I have a number that increases by 1 every time you touch the screen, that is how many entities I want to spawn. But sometimes, for some weird reason it doesn’t spawn all of them. Can somebody please shed some light on this situation???
Here’s all the code in my project (fair warning, it consists of a bit over 1000 lines of code, so yeah :/)
-- Bean
function setup()
-- Create a new craft scene
scene = craft.scene()
scene.sky.material.sky = color(0, 240, 255)
scene.sky.material.horizon = color(0, 131, 255)
scene.sky.material.ground = color(0, 131, 255)
parameter.watch("bean.rotation.x")
parameter.watch("weaponry[b].position")
parameter.integer("CloudsAlpha", 0, 100, 0)
viewer.mode = FULLSCREEN
bobActive = false
pm = false
hit = false
swingshot = false
spawned = false
dead = false
equipped = false
enemyDead = false
dead1 = false
dead2 = false
notDead = true
weaponDeployed = false
shoot = true
gtp = false
reloading = false
spawn = true
crateSpawn = false
recoil = false
setRecoil = false
reload = false
spawnCrate = true
explode = false
musicPlaying = false
shooting = false
following = false
followingButton = false
shielding = false
shieldingButton = false
shieldingAction = false
lock = false
chasing = false
stay = true
sad = false
deathSound = false
deathAnim = false
restartAnim = false
done = false
done2 = false
restartButton = false
sniperTargetPlaced = false
startHeartCollision = false
sniperTarget = {x = 0, y = 0}
health = 500
bulletSpeed = 2
enemySpeed = 1
weaponSelected = 1
buttonWidth = {s = 0}
cr = 0
tr = 0
tp = vec2(0, 0)
br = 0
br2 = 0
bc = 0
hc = 0
nbr = 4
et = 0
et2 = 0
et3 = 0
sp = 1
wave = 1
weaponRot = 0
camRot = 0
ammo = 25
cnt = 0
shr = 0
shrc = 1
hr = 0
jS = {x = 150, y = 126}
level = readLocalData("level", 0)
bobsKilled = readLocalData("total kills", 0)
bobsKilled2 = 0
choice = math.random(1,3)
explosion = math.random(1, 3)
bobs = {}
followers = {}
touches = {}
moveDir = vec3(0, 0, 0)
weapons = {sword = craft.model(asset.documents.Dropbox.blasterN_obj);
blaster = craft.model(asset.documents.Dropbox.blasterA_obj);
pistol = craft.model(asset.documents.Dropbox.blasterG_obj);
sniper = craft.model(asset.documents.Dropbox.blasterE_obj)}
weaponry = {}
raycasts = {}
bullets = {}
crates = {}
hearts = {}
bobDefenses = {}
spikes = {}
cS = {x = .1, y = .1}
forward = scene.camera.forward * moveDir.z
right = scene.camera.right * moveDir.x
up = vec3(0,1,0) * moveDir.y
finalDir = forward + right + up
-- Create a new entity
bean = scene:entity()
b = bean:add(craft.rigidbody, DYNAMIC)
b.sleepingAllowed = false
b.angularDamping = 0.4
bean.model = craft.model(asset.builtin.Primitives.Capsule)
bean:add(craft.shape.model, bean.model)
bean.position = vec3(0, -1, 0)
bean.scale = vec3(3, 3, 3) / 8
bean.material = craft.material(asset.builtin.Materials.Standard)
hat = scene:entity()
hat.model = craft.model(asset.documents.Dropbox.Army_Hat_obj)
hat.position = vec3(0, -0.575, 0)
hat.scale = vec3(0.15, 0.15, 0.15) / 8
lob = scene:entity()
lb = lob:add(craft.rigidbody, STATIC)
lb.sleepingAllowed = false
lob.model = craft.model(asset.builtin.Primitives.Sphere)
lob.position = vec3(bean.x + 1, -0.75, bean.z)
lob.scale = vec3(1, 1, 1) / 8
lob.material = craft.material(asset.builtin.Materials.Standard)
createGround(vec3(0, -3.12, 0))
shieldGen()
-- Create boundaries for game arena
makeWalls(vec3(4.5, -1, 0), vec3(4, 8, 32) / 8)
makeWalls(vec3(-4.5, -1, 0), vec3(4, 8, 32) / 8)
makeWalls(vec3(0, -1, 4.5), vec3(32, 8, 4) / 8)
makeWalls(vec3(0, -1, -4.5), vec3(32, 8, 4) / 8)
scene.camera.position = vec3(-5, 2.5, -5)
scene.camera.eulerAngles = vec3(25, 45, 0)
cameraSettings = scene.camera:get(craft.camera)
cameraSettings.ortho = true
cameraSettings.orthoSize = 4
end
function createGround(p)
ground = scene:entity()
g = ground:add(craft.rigidbody, STATIC)
g.restitution = 1
ground.name = "Ground"
ground.model = craft.model.cube(vec3(8, 4.5, 8))
ground:add(craft.shape.box, vec3(8, 4.5, 8))
ground.position = p
ground.material = craft.material(asset.builtin.Materials.Standard)
ground.material.map = readImage(asset.builtin.Blocks.Sand)
end
function createSubject(p)
bob[bc] = scene:entity()
bob[bc].name = "Bob No."..bc
bob[bc].model = craft.model(asset.builtin.Primitives.Sphere)
bob[bc].position = p
bob[bc].scale = vec3(1, 1, 1) / 8
bob[bc].material = craft.material(asset.builtin.Materials.Standard)
bc = bc + 1
end
function spikeGen(p)
spike = scene:entity()
spike.model = craft.model(asset.documents.Dropbox.blasterE_obj)
spike.position = p
spike.scale = vec3(.7, .7, .7) / 8
spike.material = craft.material(asset.documents.Dropbox.Laser)
return({spike = spike})
end
function ps(s, id)
sound(s, id)
end
function weaponGen()
for i=1, 1 do
if weaponSelected == 1 then
weapon = scene:entity()
w = weapon:add(craft.rigidbody, STATIC)
weapon.model = weapons.sword
w.sleepingAllowed = false
w.interpolate = true
weapon.eulerAngles = vec3(0, 180, 0)
weapon.scale = vec3(10, 10, 10) / 8
table.insert(weaponry, weapon)
elseif weaponSelected == 2 then
weapon2 = scene:entity()
w2 = weapon2:add(craft.rigidbody, STATIC)
weapon2.model = weapons.blaster
w2.sleepingAllowed = false
w2.interpolate = true
weapon2.eulerAngles = vec3(0, 180, 0)
weapon2.scale = vec3(10, 10, 10) / 8
table.insert(weaponry, weapon2)
elseif weaponSelected == 3 then
weapon3 = scene:entity()
w3 = weapon3:add(craft.rigidbody, STATIC)
weapon3.model = weapons.pistol
w3.sleepingAllowed = false
w3.interpolate = true
weapon3.eulerAngles = vec3(0, 0, 0)
weapon3.scale = vec3(10, 10, 10) / 8
table.insert(weaponry, weapon3)
elseif weaponSelected == 4 then
weapon4 = scene:entity()
w4 = weapon4:add(craft.rigidbody, STATIC)
weapon4.model = weapons.sniper
w4.sleepingAllowed = false
w4.interpolate = true
weapon4.eulerAngles = vec3(0, 0, 0)
weapon4.scale = vec3(10, 10, 10) / 8
table.insert(weaponry, weapon4)
end
end
end
-- Create shield for player
function shieldGen()
shield = scene:entity()
sh = shield:add(craft.rigidbody, STATIC)
shield.model = craft.model(asset.builtin.CastleKit.shieldBlue_obj)
shield:add(craft.shape.model, shield.model)
shield.position = vec3(10, 10, 10)
shield.eulerAngles = vec3(0, 0, 0)
shield.scale = vec3(1.7, 1.7, 1.7) / 8
end
function makeWalls(p, s)
wall = scene:entity()
wa = wall:add(craft.rigidbody, STATIC)
wa.sleepingAllowed = false
wall:add(craft.shape.box, s + s + s + s)
wall.position = p
wall.scale = s
end
function coinGen(p)
coin = scene:entity()
coin.model = craft.model(asset.documents.Dropbox.coinGold_obj)
coin.position = p
coin.eulerAngles = vec3(0, 0, 0)
coin.scale = vec3(1, 1, 1) / 8
end
function BulletGen(p)
bullet = scene:entity()
bub = bullet:add(craft.rigidbody, DYNAMIC)
bub.sleepingAllowed = false
bullet.model = craft.model(asset.builtin.Primitives.Sphere)
bullet:add(craft.shape.sphere, 1)
bullet.position = p
bullet.scale = vec3(.07, .07, .07)
bullet.rotation = quat.eulerAngles(90, 0, 0)
bullet.material = craft.material(asset.builtin.Materials.Specular)
bullet.material.diffuse = color(255,165,0)
return({bullet=bullet,bub=bub})
end