It looks like you're new here. If you want to get involved, click one of these buttons!
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
Comments
Here’s a bit more of the code:
Here’s another chunk:
Here’s the third last chunk of the code:
Here’s the second last big chunk of code:
And finally, here’s the rest of the code:
Also, some of the assets that I use are not built in.
@Creator27 I just skimmed thru your code not really looking for anything. Something you might want to do. Put/copy your assets into the project folder, that way they’re all in the same place. Then you can export everything as a zip file and post the zip file to the forum. That way when the zip file is used, all the assets are there and everything runs fine without having missing assets.
Also, so everyone doesn’t have to search thru your code, tell us where you spawn the entities and where the for loop is that’s giving you the problem.
If you’re unsure how to put assets into the project folder or zip the project, let us know.
@Creator27 Your code is too hard to get to run the way it is. Even after replacing models and sprites with anything that works, what are you supposed to do and where do you tap to do anything.
@dave1707, just to spare you the pain of looking over my entire code, I’ll just give you the code that I think is the problem, cannot confirm if it is.
math.random(-3,3) only returns -3, -2, -1, 0, 1, 2, or 3. you'll never see more than 7 spheres. could that be the issue?
@RonJeffries, I did play around with the numbers and sure enough, that was actually apart of the issue. The only thing is, that whatever I tried to put in just didn’t work, and in some cases made the situation worse.
that is possible. but no one can generally help with a 1000 line program. one needs to come up with small programs showing the problem.
@RonJeffries, I understand. I will make a quick example to show the problem.
@RonJeffriesm here it is:
@Creator27 If you’re trying to create spheres randomly on a surface, maybe you should have a random position for x and a random position for z instead of just -1 for z. As @RonJeffries pointed out, the way you’re doing it now, you can only see a max of 7 spheres. By adding the random position for z, you can see a max of 49 spheres.
@dave1707, I can’t tell if your code worked because of this: in my game, I have a number that defaults to 1. For my game, you use raycasting to destroy red spheres. If there are no red spheres left, the number I mentioned earlier increases by 1 and depending on what number it is, that is how many new red spheres will spawn. But for some weird reason, whenever I destroy two red spheres, no more spawn and I can’t seem to wrap my head around why.
@Creator27 Maybe it’s time to put print statements in your code to figure out what’s happening with the createFirstSphere function. Just remember, print statement can mess things up if they print constantly.
Sometimes I’ll put stop() in the code after the print statement. Since the function stop() doesn’t exist, the code stops with an error, but you can see what the print shows. If you need more print statements, you can create an actual stop function that increments a counter. If the counter exceeds a set value, then call a non existent function to cause an error. Then you can look at how many print statements you wanted.
@dave1707, I’m a bit confused at what you mean, as I’m not an extremely skilled programmer and have absolutely no idea of how to fix conditional loop problems.
@Creator27 I was suggesting that you put a print statement in the function createFirstSphere to see what values it getting and if it’s being called when you think it is. But the problem with print statements is, if they’re in the wrong place, they’ll print forever and eventually crash the code.
@Bri_G, are you able to give me a brief rundown on what exactly print statements are?
@Creator27 Why are you asking “what exactly print statements are”. I just looked thru your code and you have several print statement in there now. So apparently you know what print statement are.
I’m able to somewhat run your code, but I haven’t the slightest idea of what’s supposed to happen or how to do anything. That kind of hinders trying to find what problems you’re having.
@dave1707, yeah I think you’re probably right about that. Apologies if I got a bit annoying
@dave1707, in the project you move the capsule (the player) around using your finger on the left side of the screen. If your finger moves across to the other half of your screen then the player will stop moving. If you double tap on the right side of your screen, a red sphere will appear to try to eliminate you. In order to stop it from doing that, you must press the explosion button to shoot but be warned as you only have 25 bullets to shoot before you have to reload in which you tap the shoot button again. Also, sorry if the shooting doesn’t work well, I will try to get to that. If you kill the red sphere, another one will spawn and so on and so on. But when you kill the very second red sphere that spawns, that’s where the problem is.
@Creator27 Another problem with running your code are the assets. Because you’re not using builtin assets, when you post your code, none of your assets are included and kick out as errors when the code is run. Built in assets can be used to get the code to run, but then nothing makes any sense because none of the sprites match anything. So the code is almost useless to try to run let alone try to debug. So that’s why no one is coming up with a solution to your problem.
@Creator27 I am interested in trying to help you, not so much with your current specific problem, but with learning how to build a program like this one, so that you, and people who might help you, can understand it, test it, make fewer mistakes, and debug it when it doesn't work.
Doing that would entail a lot of work on my part, and even more on yours. Would such a thing be interesting to you?
@RonJeffries Would you be doing that in this forum. That might be interesting to a lot of other people, including me. I’m more of a hacker programmer and don’t really follow any kind of rules. I just code until it works and if it’s something that I want share or just keep for myself, I’ll go thru the code and try to make it more compact and readable.
I'm not sure how best to do it. It seems to me that @Creator27 may have very little programming experience—and I am quite impressed with how much they have done if that's the case—so it might have to start pretty basic. I'm not sure if it fits the forum style, or what. Usually, of course, I'd start a category on my web site for it.
Let's kick around some possibilities? What do you think would be ideal?
One issue with my site is that I don't allow comments. That is something to take into consideration. Maybe articles there and questions here?
@RonJeffries That would probably work OK.
@RonJeffries - it's worked before so probably will again. I'd like to see your approach.
well just a random notion at the moment, not sure how valuable it would be.
@RonJeffries, two things. Yes you are right, I don’t have a lot of programming language though I have dabbled in C# and am somewhat fluent in Lua. And I know this might be annoying to hear, but I’ve scrapped the idea anyways and I’m working on a different sort-of idea in the game. Apologies for not telling you earlier!
ok
@Creator27 the main thing to take from this string for you personally is that we are willing to help. The way other programmers can help is by having a dedicated example program that grows as a demonstration. Preferably in a manner that can be useful for all who come after. If you so desire such help personally, you need only ask, and to be willing to see said example project to the end.
Man I love this forum