Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Any tips on optimising code?

Author
Message
Todeswalzer
14
Years of Service
User Offline
Joined: 13th Apr 2010
Location:
Posted: 19th Apr 2010 12:25
I have just spent quite a while trying to get some 2d "popup" menus to work the way i want them to in a 3d environment... ok it took me hours... but i look at the code and think it could probably be neater, i know it's a pretty lame question, but i'm always after ways of making my coding take less space and look neater and considering what it is i'm trying to do being a very small part of the program... i kinda figure it should be alot smaller than it is.



the matrix and stuff is just there to provide a basic 3d world so i could test if it would function during movement and such. Oh yeah and it's using the keystates from a UK qwerty keyboard.
Mr Bigglesworth
16
Years of Service
User Offline
Joined: 4th Mar 2008
Location:
Posted: 20th Apr 2010 06:22 Edited at: 20th Apr 2010 06:23
Here, I cleaned it up a bit for you.





It won't speed it up much, but it will be easier to keep track of, and when it's easier to keep track of, it is easier to debug it.
veltro
User Banned
Posted: 20th Apr 2010 15:24 Edited at: 20th Apr 2010 15:35
Quote: "`move menu cursor
if upkey()=1 and sprite visible(1)=1 then RY#=RY#-90 : if RY#=<405 then RY#=405
if downkey()=1 and sprite visible(1)=1 then RY#=RY#+90 : if RY#=>585 then RY#=585
if leftkey()=1 and sprite visible(1)=1 then RX#=RX#-90 : if RX#=<505 then RX#=505
if rightkey()=1 and sprite visible(1)=1 then RX#=RX#+90 : if RX#=>685 then RX#=685"


you test sprite visible(1)=1 four times.

better

if sprite visible(1)
if upkey()=1 ...
if downkey()=1 ...
...
endif

what abou setting RY (and RX) value?

if upkey()=1 then RY#=RY#-90 : if RY#=<405 then RY#=405

you make a test, an assignment, another test and if succeded a second assignment that override the first one

better is

if upkey()=1
if RY# > 495 then
rem RY will never be <= 405 after subtracting 90
RY# = RY# - 90
else
RY# = 405
endif

this way you save an assignmnet


Quote: "`prevent cursor going outside of menu bounds by moving it to closest apropriate space
if (RY#=405 OR RY#= 585) and leftkey()=1 then RY#=495
if (RY#=405 OR RY#= 585) and rightkey()=1 then RY#=495
"


if the first if test succeds then RY is 495 so the next if statement is useless. Anyway you test (RY#=405 OR RY#= 585) twice.

Better is

if (RY#=405 OR RY#=585) AND (leftkey()=1 OR rightkey()=1) then RY# = 495

Login to post a reply

Server time is: 2024-11-24 17:23:15
Your offset time is: 2024-11-24 17:23:15