Post subject: Lua help with data types
Former player
Joined: 5/4/2005
Posts: 502
Location: Onett, Eagleland
I'm curious if Lua allows you to set a data type for variables. The reason I ask is I need a number to roll over...i.e. I want a 2 byte integer.
myint = 0xFFFE
myint2 = myint + 0x350
I want myint2 to return 0x34E. Do I have to do this manually or can I declare a 2 byte data type with Lua? I tried looking at the reference manual and I didn't see anything that allows you to do this. Thanks.
I think.....therefore I am not Barry Burton
Lex
Joined: 6/25/2007
Posts: 732
Location: Vancouver, British Columbia, Canada
Use the modulo operator (%).
myint = 0xFFFE
myint2 = (myint + 0x350) % 0x10000
Former player
Joined: 5/4/2005
Posts: 502
Location: Onett, Eagleland
Lex wrote:
Use the modulo operator (%).
myint = 0xFFFE
myint2 = (myint + 0x350) % 0xFFFF
Thank you.
I think.....therefore I am not Barry Burton
Lex
Joined: 6/25/2007
Posts: 732
Location: Vancouver, British Columbia, Canada
Pasky13 wrote:
Lex wrote:
Use the modulo operator (%).
myint = 0xFFFE
myint2 = (myint + 0x350) % 0x10000
Thank you.
You're welcome.
Lex
Joined: 6/25/2007
Posts: 732
Location: Vancouver, British Columbia, Canada
Oops! I did it wrong! Since (0xFFFE + 0x350) % 0xFFFF is not 0x34E, the correct code is
myint = 0xFFFE
myint2 = (myint + 0x350) % 0x10000
Skilled player (1885)
Joined: 4/20/2005
Posts: 2160
Location: Norrköping, Sweden
Huh, I learned something now too. I always use "math.mod(x,y)" instead of "x % y". I think the latter is simpler.
Lex
Joined: 6/25/2007
Posts: 732
Location: Vancouver, British Columbia, Canada
The modulo operator (%) was only implemented in Lua 5. Before that (Lua 4 and below), you would have had to use your previous method.