The Daily Click ::. Forums ::. Klik Coding Help ::. Lua Functions inside loops
 

Post Reply  Post Oekaki 
 

Posted By Message

Johnny Look

One Happy Dude

Registered
  14/05/2006
Points
  2942

VIP Member
14th July, 2009 at 22:03:12 -

I have a function meant to be accessed by MMF2 inside a while loop, but for some reason all it does is making the game stop responding.
The function is meant to make the player go forward by a number of pixels specified by the parameters of the functions.

here's the code btw:


createobject("object","ball.png");

loop=1

while loop==1 do

moveobject("object",1,0);

end


 
n/a

Pixelthief

Dedicated klik scientist

Registered
  02/01/2002
Points
  3419

Game of the Week WinnerWeekly Picture Me This Winner!You've Been Circy'd!VIP MemberI like Aliens!Evil klikerThe SpinsterI donated an open source project
14th July, 2009 at 22:13:32 -

theres no terminating condition to your loop, so its just repeating infinitely
after the stack is large enough, the game simply crashes

try this:
function createobject(distance)
for loop=1,distance, do
moveobject(1,0);
end
end

if you look at your code, theres nothing that sets loop to anything but "1". So each time it compares loop == 1, it is true, and the loop keeps going. With a For structure, it increases by 1 each iteration, from 1 to X

 
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456

Johnny Look

One Happy Dude

Registered
  14/05/2006
Points
  2942

VIP Member
14th July, 2009 at 22:17:51 -

mmmh thanks but I need the infinite loop so that inside that loop I can check for player input.
Unless there's another way, my Lua knowledge is fairly limited.

edit: I just realized that I can't even do math operations inside the loop, jeez basic does give bad habits.

Edited by Johnny Look

 
n/a

Pixelthief

Dedicated klik scientist

Registered
  02/01/2002
Points
  3419

Game of the Week WinnerWeekly Picture Me This Winner!You've Been Circy'd!VIP MemberI like Aliens!Evil klikerThe SpinsterI donated an open source project
14th July, 2009 at 22:37:06 -

you should probably structure your code so that you don't need wait functions

when a programming language does an infinite loop, it doesn't wait for input, or run the event once per frame, etc. It will attempt to call the move function an infinite numbers of times all at once, which will of course instantly crash your program. Instead you should structure your code so that it calls the move function each time you need to move, not so that it waits for input in the middle of the loop.

 
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456

Johnny Look

One Happy Dude

Registered
  14/05/2006
Points
  2942

VIP Member
14th July, 2009 at 23:44:48 -

yeah well, but how ?

thanks a bunch for the help so far btw

 
n/a

Johnny Look

One Happy Dude

Registered
  14/05/2006
Points
  2942

VIP Member
15th July, 2009 at 01:02:15 -

nevermind got it sorted out, I only had to create a loop inside mmf instead of lua.

 
n/a

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49567

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
15th July, 2009 at 04:46:54 -

mmf does it again!

 
n/a
   

Post Reply



 



Advertisement

Worth A Click