Page 2 of 5

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 7:42 pm
by essell
Some of that's getting a bit over my head to be honest :) Will look into it further down the line though - I'm told by my programmer mate that I should learn to use tables soon. Cheers.

Q. When there's a bug and my game won't run, I often get '<eof>' expected near 'end' as the error. What does this mean, generally?

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 7:51 pm
by bartbes
I guess it means that there is one 'end' too much in your code, so you're exiting code blocks more often than entering them. (that sounds complicated, simple is: too much 'end')

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 7:54 pm
by essell
That's what I suspected. Cheers :)

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 8:04 pm
by ljdp
You might already know about this but i
recommend you have good read of this
http://www.lua.org/pil/
Don't just read it too, have whatever you code in open next to it too
and type in all the examples by hand start experimenting simply with them.
It helps tremendously when learning a computer language.

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 8:10 pm
by bartbes
Never used it, I always stick with the reference manual (but then again, I already was a programmer before starting with lua, it was a small syntax switch)

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 8:49 pm
by essell
Aye a friend linked me to that too - will start dipping into it :)

Q. How do I take a float and convert / round it to the closest integer?

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 8:52 pm
by Jake
I don't think Lua has a round function but this will do the same ;)

Code: Select all

function math.round(x)
  return math.floor(x+0.5)
end

Re: My ongoing newbie questions

Posted: Fri Jun 12, 2009 5:25 am
by bartbes
Yes, that works, I'll warn you math.floor isn't the most effective function in lua though, btw you forgot something like:

Code: Select all

function math.round(n, dec) --dec = decimals
    dec = 10^(dec or 0)
    return math.floor(n*dec+0.5)/dec
end
Haven't tested this, but should allow you to set how accurate it is, so, if you set dec to 1, it will give you 1 decimal.

EDIT: Robin was right.. made a typo
EDIT2: and, of course, robin was right again, it's 10^dec
EDIT3: :ehem:

Re: My ongoing newbie questions

Posted: Fri Jun 12, 2009 5:33 am
by Robin
That doesn't work: firstly, s/x/n, secondly, /s/dev/dec.

EDIT: thirdly: still doesn't work.

EDIT2: this works:

Code: Select all

function math.round(n, dec) --dec = decimals
     dec = 10^(dec or 0)
     return math.floor(n*dec+0.5)/dec
 end

Re: My ongoing newbie questions

Posted: Fri Jun 12, 2009 5:41 am
by bartbes
I edited my post, thx Robin