QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

[LUA] Any tips against lag

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 ...

Reply
 
LinkBack Thread Tools
Old 06-15-2009, 03:13 PM   #1
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default [LUA] Any tips against lag

If I use the for loop for enemies and I make more then 3 enemies the PSP acts sloww is there anything I can do against this??
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:19 PM   #2
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

your doing something wrong because 3 enemies shouldn't cause no slow down

post your code.
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:28 PM   #3
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by dan369 View Post
your doing something wrong because 3 enemies shouldn't cause no slow down

post your code.
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
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:38 PM   #4

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

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
that should speed it up, try and not load anything in your main loop continuosly
__________________
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:41 PM   #5
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

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.
Indent!

Edit: damn, slicer beat me to it :P
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:45 PM   #6

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

Quote:
Originally Posted by dan369 View Post
Indent!
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
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 03:50 PM   #7
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

Quote:
Originally Posted by slicer4ever View Post
1. u didn't, but u probably whipped it up on the spot like i did=-)
2. agreed
exactly
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:00 PM   #8
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by slicer4ever View Post
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
that should speed it up, try and not load anything in your main loop continuosly
Okay I didnt post the exactly code but this is the exactly code and it don't got any image.load() in it but it still slows the game with every enemy that enters..
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()
Are there any other ways to speed up the game?
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:07 PM   #9
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

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..
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:20 PM   #10
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by dan369 View Post
Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload)

do you read? honestly don't have this in a for-loop!
Just use the EXACT way slicer & I showed up.

if i == enemies then
i=0
end

Delete this. No need for it.
Okay you got any other idea where to blit the image?
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
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:31 PM   #11
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

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.
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:51 PM   #12
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Damm this helped loading 30+ enemies without any lagg thanks guyss
HAHAHA MASSIVE SLAUGTHERR THIS GAME
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:53 PM   #13
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

Depending on the Luaplayer your using, you should get a good 100+ enemies easy.
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 04:54 PM   #14

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

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()
what to do after all the changes:

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
that's your issue, still loading in the main freaking loop, that should take care of it

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
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 05:12 PM   #15
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by slicer4ever View Post
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()
what to do after all the changes:

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
that's your issue, still loading in the main freaking loop, that should take care of it

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
Yea that was some nice help from you guys, if i had any other question then it would be those enemies are overlaying eachother so I want them to bounce when they reach the same x and y so

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
-=Double Post Merge =-
Quote:
Originally Posted by dan369 View Post
Depending on the Luaplayer your using, you should get a good 100+ enemies easy.
Thats possible cause I use LuaPlayerHM and its even not showing a little bit of lag at 30 enemies.... soo it would be possible

Last edited by Predricted; 06-15-2009 at 05:14 PM.. Reason: Automerged Doublepost
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 05:27 PM   #16

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

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
basically you have to check that each point on object a does not fall inside the 4 points on object b, or vice versa, if they do, than do the appropriate collision response

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
this method is slower but picks up on collisions immidiatly

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
if you have large, i mean very large numbers of enemy's i'd suggest the latter method, doing a second loop could be a drastic speed loss, however if it's a low number, or you control areas of enemy's thusly to lower the number of enemy's than go with the former so that you always have perfect collision

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
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 05:56 PM   #17
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

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
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 05:58 PM   #18
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

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..
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-15-2009, 06:57 PM   #19

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

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
efffectivly, as i increases, the number of objects needed to be checked against decreases, however doing the whole loop again might not be as benifical as i'm thinking
__________________
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-16-2009, 02:47 AM   #20

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,313
Trader Feedback: 0
Default

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
As long as the enemy table is sorted, when (enemy[i].x +enemy[i].width) is less the enemy[j].x, no other enemy in the table beyond j can collide with enemy i.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
lag , lua , tips

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 01:36 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us