Jump to content

lua while loop issue


Rick
 Share

Recommended Posts

I can't figure out what I'm doing wrong here.

 

In an object I set "quit" to "true" when I press a key, yet the below never breaks the loop

SetGlobalString("quit", "false")
while (KeyHit(KEY_ESCAPE) == 0) or (GetGlobalString("quit") == "false") do

       fw:Update()
       fw:Render()
       Flip(0)
end

 

Yet this works, which is telling me that the object that I check for a keypress and set "quit" to "true" is working. So why doesn't it work in the above code?

SetGlobalString("quit", "false")
while (GetGlobalString("quit") == "false") do

       fw:Update()
       fw:Render()
       Flip(0)
end

Link to comment
Share on other sites

Lua handles logical operators different than what you are probably used to...

 

The or binary operator also does not necessarily return a boolean value. If the first argument is not false or nil it is returned, otherwise the second argument is returned. So, a boolean is only returned if the first argument is true or the second argument is a boolean.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

No, he really needs an AND in there. No matter what his GetGlobalString("quit") == "false" comparison is, the KeyHit(KEY_ESCAPE) == 0 is always true, so the or never goes any further.

 

Hence why it works without the KeyHit test. Use an and and all will be fine.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

No, he really needs an AND in there. No matter what his GetGlobalString("quit") == "false" comparison is, the KeyHit(KEY_ESCAPE) == 0 is always true, so the or never goes any further.

 

Hence why it works without the KeyHit test. Use an and and all will be fine.

 

yes, wasn't saying that it wasn't... was explaining the use of OR in lua

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

The use of OR in lua seems the same as using || in C++.

 

Lua supports both logical OR and bitwise OR, but using the same 'or' keyword. No confusion at all there :(

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...