![]() |
| Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact |
| ||||||
This is a discussion on [LUA] Any tips against lag within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; If I use the for loop for enemies and I make more then 3 enemies the PSP acts sloww is ...
![]() |
|
|
LinkBack | Thread Tools |
|
|
#3 | |
![]() |
Quote:
Code:
for i=1, enemes do
if player.x < enemy[i].x then
enemy[i].x = enemy[i].x+1
enemy[i].image=Image.load("enm_right.png")
end
if player.y < enemy[i].y then
enemy[i].y = enemy[i].y+1
enemy[i].image=Image.load("enm_down.png")
end
if player.x > enemy[i].x then
enemy[i].x = enemy[i].x-1
enemy[i].image=Image.load("enm_left.png")
end
if player.y < enemy[i].y then
enemy[i].y = enemy[i].y-1
enemy[i].image=Image.load("enm_up.png" )
end
screen_startDraw()
screen.blit(enemy[i].x, enemy[i].y,enemy[i].imageload
screen_endDraw()
end
|
|
|
|
|
|
|
#4 |
![]() ![]() Developer
|
it's this: Image.load("enm_up.png" ) which is causing the slowdown insead do:
Code:
enm_right = image.load("enm_right");
enm_left = image.load("enm_left");
//etc
while true do
for i=1, enemys do
if player.x < enemy[i].x then
enemy[i].x = enemy[i].x+1
enemy[i].image=enm_right //<-----this
end
//etc
end
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#5 |
![]() Enter Custom Title
|
screen_startDraw()
screen.blit(enemy[i].x, enemy[i].y,enemy[i].imageload screen_endDraw() outside the for-loop Your loading 4 images every loop, these should be outside the loop. have like Code:
up = Image.load("enm_up.png" )
down = Image.load("enm_down.png")
etc.
---In your for-loop
if player.y < enemy[i].y then
enemy[i].y = enemy[i].y-1
enemy[i].image= up;
end
if player.y < enemy[i].y then
enemy[i].y = enemy[i].y+1
enemy[i].image= down
end
etc.
Edit: damn, slicer beat me to it :P |
|
|
|
|
|
#6 |
![]() ![]() Developer
|
1. u didn't, but u probably whipped it up on the spot like i did=-)
2. agreed
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#7 |
![]() Enter Custom Title
|
|
|
|
|
|
|
#8 | |
![]() |
Quote:
This is the exactly code: Code:
screen.startDraw() for i=1, enemies do if enemy[i].x > player1[1].x then enemy[i].x=enemy[i].x-1 enemy[i].image="images/enemy/imp_left.png" end if enemy[i].x < player1[1].x then enemy[i].x=enemy[i].x+1 enemy[i].image="images/enemy/imp_right.png" end if enemy[i].y > player1[1].y then enemy[i].y=enemy[i].y-1 enemy[i].image="images/enemy/imp_up.png" end if enemy[i].y < player1[1].y then enemy[i].y=enemy[i].y+1 enemy[i].image="images/enemy/imp_down.png" end enemy[i].imageload = Image.load(enemy[i].image) Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload) if i == enemies then i=0 end random=math.random (1, 70) end end screen.endDraw() |
|
|
|
|
|
|
#9 |
![]() Enter Custom Title
|
Just use the EXACT way slicer & I showed up.
if i == enemies then i=0 end Delete this. No need for it. Last edited by dan369; 06-15-2009 at 04:30 PM.. |
|
|
|
|
|
#10 | |
![]() |
Quote:
I could do this if enemy == 1 then image.blit(enemy[1].x,enemy[1].y,image) end if enemy == 2 then image.blit(enemy[1].x,enemy[1].y,enemy[1].image) image.blit(enemy[2].x,enemy[2].y,enemy[2].image) end and that all the way up to 10 but is there a other way? and this |
|
|
|
|
|
|
#11 |
![]() Enter Custom Title
|
My bad just released what you are actually doing, don't do what i previously said leave it in the for-loop. I remember what your trying todo from your previous posts.
|
|
|
|
|
|
#14 |
![]() ![]() Developer
|
predicted your misunderstanding, what not to do is commented from your previous code:
Code:
//first of all indent(see below) screen.startDraw() for i=1, enemies do if enemy[i].x > player1[1].x then enemy[i].x=enemy[i].x-1 enemy[i].image="http://forums.qj.net/images/enemy/imp_left.png"//<--stupid to save the paths end if enemy[i].x < player1[1].x then enemy[i].x=enemy[i].x+1 enemy[i].image="http://forums.qj.net/images/enemy/imp_right.png" end if enemy[i].y > player1[1].y then enemy[i].y=enemy[i].y-1 enemy[i].image="http://forums.qj.net/images/enemy/imp_up.png" end if enemy[i].y < player1[1].y then enemy[i].y=enemy[i].y+1 enemy[i].image="http://forums.qj.net/images/enemy/imp_down.png" end enemy[i].imageload = Image.load(enemy[i].image) //<---still loading an image in the main loop, what did i say?!!!!!!!!!!!!! Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload) if i == enemies then //<---these 3 lines, not needed!, i=1 up their at the for loop takes care of this! i=0 end random=math.random (1, 70) end end screen.endDraw() Code:
enm_left = Image.load("http://forums.qj.net/images/enemy/imp_left.png");
enm_right = Image.load("http://forums.qj.net/images/enemy/imp_right.png");
enm_up = Image.load("http://forums.qj.net/images/enemy/imp_up.png");
enm_down = Image.load("http://forums.qj.net/images/enemy/imp_down.png");
while true do
//assuming that while true do is right here
screen.startDraw()
for i=1, enemies do
if enemy[i].x > player1[1].x then
enemy[i].x=enemy[i].x-1
enemy[i].image=enm_left;
end
if enemy[i].x < player1[1].x then
enemy[i].x=enemy[i].x+1
enemy[i].image=enm_right;
end
if enemy[i].y > player1[1].y then
enemy[i].y=enemy[i].y-1
enemy[i].image=enm_up;
end
if enemy[i].y < player1[1].y then
enemy[i].y=enemy[i].y+1
enemy[i].image=enm_down;
end
Image.blit(enemy[i].x,enemy[i].y,enemy[i].image)
end
random=math.random (1, 70)
end
screen.endDraw()
end
edit: confused, it's working now? edit2: ok, i understand, didn't see you other post saying u figured it out, i'll leave this up incase you have any other questions that you may wish answered
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#15 | ||
![]() |
Quote:
if enemy1x == enemy 2 xx and enemy1y==enemy2y then enemy1x == enemy1x-random.math(-25,25) enemy1y == enemy1x-random.math(-25,25) end but the problem is I am working with tables so it should be like this Code:
if enemy[i].x == enemy[all enemies but not i].x and enemy[i].y == enemy[all enemies but not i].y then blablabla end Quote:
Last edited by Predricted; 06-15-2009 at 05:14 PM.. Reason: Automerged Doublepost |
||
|
|
|
|
|
#16 |
![]() ![]() Developer
|
i'd suggest pge lua if your making games, as for the collision, 1: you can't do if x == x2, y == y2, not that simple, that'd be asking that your characters fall on the exact same pixel, which is pretty rare, instead you need rectangle collision
Code:
if (x1 > x2 and x1< x2+width2) or (x1+width1 > x2 and x1+width1 < x2+width2) then
if (y1 > y2 and y1 < y2+height2) or (y1+height1 > y2 and y1+height1 < y2+height2) then
//collision response here
end
end
their's two methods i know on how to achieve going through your enemy list: 1. Code:
for i=1, enemys do
//movement code
for d=1, enemys do
if i not d then
//check collision, and respond if their is
end
end
end
however the faster method is: 2. Code:
int CurrentEnemyCheck = 1;
while true do
for i=1, enemys do
//movement code
if i not CurrentEnemyCheck then
//check collision and use the CurrentEnemyCheck to fill in for object b
end
end
CurrentEnemyCheck++; //add 1 to the var, don't know if this is legal in lua
if CurrentEnemyCheck>CurrentEnemys then
CurrentEnemyCheck = 1;
end
end
edit: if anyone knows another method, i'd be glad to hear it
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#17 |
![]() Enter Custom Title
|
It's best to have only one for-loop checking collision against the enemies, it's like slicer sadi could lead to a "drastic speed loss".
'++' isn't as you say legal. One way of doing making collision faster is to as i say check if there 'in-range', really it's pretty stupid to just check every enemie, what if that enemie is on the other side of the screen or on a totally different part of a map(just an example). you could do something like: Code:
for =1, #enemies do
if (player.x + player.width + 20) > enemies[i].x or (player.y + player.height + 20) > enemies[i].y then
---check collision
else
end ---do nothing
|
|
|
|
|
|
#18 |
![]() |
Opps thats my dumb mistake i made enemy enemies lol sorry fixed it
btw can we load 2 variables like i in a for loop so for example for i=1, enemies and for d=1, enemies do end so we don't need to load 2 for loops? just a idea haha Last edited by Predricted; 06-15-2009 at 06:28 PM.. |
|
|
|
|
|
#19 |
![]() ![]() Developer
|
no, as far as i know what you did is not possible, however i may be incorrect, regardless it wouldn't make sense to do that, you'd just be counting both at the same time and thusly you'd not be doing anything, as for dan, if anything that's simple creating a third check if that check holds true, i guess it's a weighing situation, how often do u plan your enemy's to come into contact, if often i'd suggest just always checking, if not than simple do your method, but either way it's adding to a possible check list
at the same time you can also condense my two if statements into one, in the end i personally find it easier to always check, regardless a check needs to be done somewhere, their's no way to magically say, one is across the unverse without comparing each of them in someway edit: however as i think about it, you could create a hierarchical list of objects, seperate them based on distance, than when one group of objects gets close to another they combine into one larger group, thusly, if a series of objects in on the left, only the left objects will be checked against themselfs, and if a series of objects is on the right, the same for only right objects, it can cut down on going throught all the enemy's with each and every object, and limiting to a small set of enemy's that each object checks itself against, but it'd require alot of thinking and code to effective put place(if this doesn't make sense it's because i'm partly thinking of it at the same time i'm writing it and am half talking to myself on the idea) edit2: just thought of a less complex idea, however i'm not certain if it'd save any speed, and could potential cause worse problems than you want: Code:
for i=1, enemys, do
//move
end
for i=1, enemys do
for d=i+1, enemys do
//check collision
end
end
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#20 |
![]() ![]() Developer
|
There is an alternative method that can potentially cut down on the number of checks you need to do but requires a sorted list of objects by their position and still has a worse case scenario of the code above. For sake of this example lets assume a Super Mario Bros game, the moving objects are sorted by x position then by y position:
Code:
for i=1, enemys do
for j=i, enemys do
if enemy[i].x +enemy[i].width < enemy[j].x then
break
else
-- check against the y axis for collision
end
end
end
__________________
[Blog] [Portfolio] [Homebrew Illuminati - Serious Homebrew Development Forums] [I want to make Homebrew FAQ] [How I broke into the Games Industry] [Programming Book List] [Programming Article List] |
|
|
|
![]() |
| Tags |
| lag , lua , tips |
| Thread Tools | |
|
|