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 / checking/ comparing loops

Author
Message
PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 10th Jan 2007 23:41
this is kind of a noobish question

is there a clean and easy way to check variables in every loop.

like if i need t check if the position of object in thecurrent loop was less than the last loop.

dont hate people who rip you off,cheat and get away with it, learn from them
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 10th Jan 2007 23:47
positions of objects are absolute, so what you define as "less" is not clear. what do you mean?

PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 10th Jan 2007 23:51 Edited at: 10th Jan 2007 23:52
suppose i want to check if the object is going down.
i will check if the y axis of the object position in the world was less than it was in the last frame

then i compare. if it is less, then its is going down

dont hate people who rip you off,cheat and get away with it, learn from them
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 11th Jan 2007 00:08
You only need one loop:



PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 11th Jan 2007 00:16
y# can't be more or less than the old# because they are declared as equal

dont hate people who rip you off,cheat and get away with it, learn from them
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 11th Jan 2007 00:41
Trust me - it works. I use that technique alot.

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Jan 2007 00:57
Zotoaster is right. If you look carefully at the two lines:

oldy#=y#
y#=object position y(1)

you will see that OldY# is set to object 1's old position (Y#) and on the next line Y# is updated to the object's current Y position.

The only way they can be the same is if object 1 is stationary.

TDK_Man

H4ck1d
18
Years of Service
User Offline
Joined: 27th Dec 2005
Location: Yes
Posted: 11th Jan 2007 00:57 Edited at: 11th Jan 2007 00:57
It does work. You set oldy# equal to y# before setting y# to the current position value of the object.

[edit] whoops, you beat me to it

PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 11th Jan 2007 00:58
do you use it on moving objects?

i don't know, but in my case it dosen't work. its not supposed to

this code doesen't check the loops. it just compares 2 positions which in my case will always stay the same

do

oldy#=y#
y#=object position y(1)

if y#<oldy# then print "moving down"

sync
loop


oldy#=y#
y#=object position y(1)
therefore oldy#=object position y(1)

old# can only be equal to y#

dont hate people who rip you off,cheat and get away with it, learn from them
PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 11th Jan 2007 01:03 Edited at: 11th Jan 2007 01:50
ok,.... sorry if i misunderstand,
oldy# is just a variable just like x# or kujfha# or y#

both oldy# and y# are in the same loop.


can there be a problem if i have the code inside of a function?

because this doesen't work for me

dont hate people who rip you off,cheat and get away with it, learn from them
PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 11th Jan 2007 02:18
Ok you win, the code works. I assume that the variable oldy# is no normal variable. its already assigned by the program isn't it? (this is my guess). if its assigned, it should in my opinion be blue.

although this works, i got a problem of getting it to work inside of a function. the inside of the main loop gets so messy and dirty, i think its better to simplify the inside of the loop by distributing the code to functions.

why can't i get Zoto's code to work inside a function?

dont hate people who rip you off,cheat and get away with it, learn from them
Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 11th Jan 2007 03:17
oldy# is just like any other variabe. The name has nothing to do with any of it, and therefore there is no reason it should be blue.

The reason Zoto's code works is the order in which y# and oldy# are set to each other.

Quote: "oldy#=y#
y#=object position y(1)"


If the order of this was reversed then Zoto's code would not work, because oly# and y# would always be the same. Instead oldy# is set to y# and then y# is updated with the new object position y(1) of the object. Because oldy# is set to the unupdated y# it becomes stores the object position y(1) of the last loop. Something important to remember is that only constants update automatically.

Zoto's code will not work in a function because variables inside a function are local unless defined otherwise beforehand using the "global" command. Local variables means that they are separate entities to what is outside the function. Zoto's code in a function does not work because "oldy#=y#" depends on the program remembering what y# was from last time the function was called. This will not happen unless y# is global.

Long story short- put "global y#" at the top of your program.

PAGAN_old
18
Years of Service
User Offline
Joined: 28th Jan 2006
Location: Capital of the Evil Empire
Posted: 11th Jan 2007 03:52 Edited at: 11th Jan 2007 04:13
Correct me if i am wrong, To update any variable don't you need to go through the loop at least once? since all the variables are in one loop, they all get updated at the same frame/loop. this should make all the variables equal to each other. all the time.

how does one get updated and the other stays old? in the same loop?


another question for advice.

since i am too lazy to pick out which variables are global and which are local, is it good to place all of them as global?

i believe everything should work fine, but i think there is something bad about putting all of them as global.

just now i found out that i might need to apply some local variables to other functions and subroutines, might as well make my work easier

dont hate people who rip you off,cheat and get away with it, learn from them
Zergei
19
Years of Service
User Offline
Joined: 9th Feb 2005
Location: Everywhere
Posted: 11th Jan 2007 11:45
Regarding the global/local variables: local variables are erased from memory once the function, from wich they where created, finishes. That means that your using less resources.
Global variables stay in memory unless until you close the program, thus using memory space and wasting resources if not needed to be global.

Quote: "how does one get updated and the other stays old? in the same loop?"


Because your thinking too much. You think (i suppose) that everything gets updated once the loop finishes, but tell me.... where does its sats that (for example) oldy# gets updated?, because once the command oldy#=y# was executed in the loop, it doesn't get executed until it does all the loop again, or you move the pointer there.

I've got a friend that gets confused with that also. He thinks that "x1=x1+1" has no sense. And in maths, it does has no sense, because "x1" can never be equal to "x1+1", but this is not maths (not all at least). That same line is read like "x1 'takes the value of' x1+1", but remember that the line is executed on that moment. On the next line the program doesn't care about the previous line.

.So on oldy#=y#, oldy# gets y# value, and then y# changes, but oldy# has already been given the value, and won't change unless you tell it to.

Further on my stuff at...

Login to post a reply

Server time is: 2024-11-26 03:38:22
Your offset time is: 2024-11-26 03:38:22