I need help
I have two for loops spawning objects from lists, but I can't make them disappear on collision, i btw used mrLmans tutorial.
If you can help, I can add you to the project
I have two for loops spawning objects from lists, but I can't make them disappear on collision, i btw used mrLmans tutorial.
If you can help, I can add you to the project
Well, I could help you
Well, I've been reviewing it and the code is quite strange, but I think I've managed to sort of fix the problem...
Okay, let's see, I have to give you some advice. Let's say you have a list where you put your orcs:
orcs = []
And you start adding what an "orc" is to that list
local orc = object
x = randRange(-205,200)
y = randRange(70,150)
attack = false
alive = true
speed = 2
width = 64
height = 64
dead = false
sprite = choose(orcSprites)
end
// Add an "orc" to that list called "orcs"
orcs.push(orc)
And now I want to eliminate the orc. To do this, you need to know why that orc would NOT be eliminated...
If an orc is not eliminated, then it is because that orc is alive (orc.alive == true)
Now that we know the necessary conditions for NOT being eliminated, we can create a system to eliminate orcs
To do this, every time you use updateOrcs, you must first place a temporary list:
updateOrcs = function()
local temp = []
// ...
end
And after that you have to do a loop checking each orc and see if that orc is alive or not:
local temp = []
for ork in orcs
if ork.alive == true then
ork.y -= 6 // Moving down
// Other actions the orc will take if he is alive (This orc was not eliminated)
end
end
This code does the following: it checks each element of orcs, and for each element in that list, it performs a local ork = (that element) operation
So ork will represent each orc in the orcs list. With that in mind, the code then checks if each orc is alive, and if so, it performs its actions, such as moving down ork.y -= 0.6
And among those other actions that the orc will take, the main one is to stay on the orc list (that is, not be eliminated). The steps to achieve this are shown below:
local temp = []
for ork in orcs
if ork.alive == true then
ork.y -= 6 // Moving down
temp.push(ork) // Keep the ork inside the temp
end
end
orcs = temp // Replace the original orc list with the temporary list
This creates a temporary list (temp), checks each element of the orcs list (I mean, each orc), and if the orc is alive, then moves it down (ork.y -= 0.6). Additionally, the orc is placed in the temporary list we created earlier. Finally, the original list (orcs) is replaced with this temporary list
It's like creating a temporary list, and every orc that's alive is placed on that list. Meanwhile, the remaining orcs (those that aren't alive) are removed, since the original list is replaced by the temporary list (the one containing the living orcs)
Now, the same process is followed for the army list. You must consider the conditions that indicate whether someone is alive and add them to the temporary list you created. Then, replace the original list with the temporary one
To do this, you technically need to place the actions of the army, see if they collide with the orcs; if they do, then those orcs are eliminated (I mean, orc.alive = false)
local temp = []
for arm in army
// Check if "arm" is alive
if arm.alive == true then
for ork in orcs
// Check all the orcs, see if any collide with "arm"
if collision(arm,ork) == true then
ork.alive = false // Take the orc's life if it is colliding
end
end
temp.push(arm) // Keep the "arm" inside the temp
end
end
army = temp // Replace the original army list with the temporary list
It's the same as above, except that among its actions, instead of moving down, it uses the collision function with each orc in the orc list. And if it collides with that orc, its alive property is set to false (In other words, the orc is dead)
And if the orc is dead, then when updateOrcs is used, that dead orc will be removed
Avoid using too many for loops. I suggest that for all actions you perform with the orcs, you use at most one for loop, if possible. Don't do something like:
for ork in orcs
// Bla bla....
end
for ork in orcs
// Bla bla... again?
end
If you've already used a for loop within the orcs list, it's better to put all your actions in that for loop, rather than splitting it into two or more for loops. Even so, if you see it as necessary, do it. But if you can reduce what you're doing to a single loop, all the better
I mention this because of the lag you could cause by checking too many items, since I saw that it reached about 1000 orcs
Try using the TAB key to keep your code organized. Also, if you select multiple lines and press TAB, you can arrange several lines of code at once
And if you want to go back those spaces, you can try the TAB + SHIFT combination
I was looking at the code and there were some things that didn't make sense in certain parts, like saying:
if ork.y > 30 then
if ork.y > 10 then
print("Yei")
end
end
There were also several things I didn't understand the purpose of; could you explain them to me so I can better adapt your code?
I mention this because the army arrived with about 500 members, and the orcs as well. So, for each member of the army, you had to check if it would collide with 500 orcs. In short, you'd be using the collision function about 15,000,000 times per second
So set a timer so that too many army members don't get on, otherwise your PC will explode
if global.time % 15 == 0 then
if armeija.war == true then
old.push(armeija)
end
end
And I honestly wish I could explain more, but it's 3 AM and my head is burning from sleepiness
Thank you very very much, this is really useful