| FBA The Creator http://www.fbacreator.com/fbaforum/ |
|
| Using a Table Instead of Multiple mousein Statements http://www.fbacreator.com/fbaforum/viewtopic.php?f=10&t=60 |
Page 1 of 1 |
| Author: | phrebh [ Tue Jun 24, 2008 7:32 pm ] |
| Post subject: | Using a Table Instead of Multiple mousein Statements |
Because keeping track of large amounts of "if mousein..." statements can be tedious and prone to error, I've been trying to find a way of getting the information I need without using them. To that end, I poked around the Lua Wiki and even though no one had what I was looking for, I figured out a way to do it using tables. Let me preface the example by saying that, yes, keeping long tables of information can also be tedious and prone to error, but it does keep your functions clean and manageable and also allows for greater reuse of code. For instance, I can (and probably will from now on) use my "boundedIn" function without changing it for multiple projects. Here is the example: Code: Look to the next post for the updated code. There are a couple things to note, here. Because this is looping through the table to find a match, this probably isn't as efficient as "if mousein..." for huge amounts of things to check, but I don't know either way since I haven't done any benchmarking. I did allow for this to some extent by having the function instantly return when a match is found instead of it continuing the loop. But since Lua (and therefore presumably FBA) doesn't necessarily keep table items in the order they are added, it probably won't do too much good to order your tables by what will be clicked the most often (but it couldn't hurt). The other thing to consider is also a factor with mousein, and should be treated in much the same way. If you have two area defined where one is inside of the other, you'll have to do two checks to find out which one is actually being clicked. I hope this helps someone else. |
|
| Author: | phrebh [ Tue Jun 24, 2008 9:04 pm ] |
| Post subject: | Re: Using a Table Instead of Multiple mousein Statements |
I realized that the boundedIn function wasn't generalized for the table variable, so here is an update: Code: table={
|
|
| Author: | Boiled Sweets [ Thu Jul 10, 2008 7:41 am ] |
| Post subject: | Re: Using a Table Instead of Multiple mousein Statements |
I do a similar thing when creating my "select level" buttons. There are say potentially 30 levels (mazes) but if they have only got to say the 3rd one then I only want to show 3 buttons... Code: START_LEVEL = 3 That bit of code seeds the button placement table. Then I check for the mouse event... Code: for i=1, START_LEVEL do Hope that helps someone, oh and the place the buttons... Code: local x = 25 |
|
| Author: | cic.lemur [ Fri Jul 11, 2008 1:46 am ] |
| Post subject: | Re: Using a Table Instead of Multiple mousein Statements |
Why not do something like a class: viewtopic.php?f=5&t=91 Although there is a lot of code defining the classes, but these will ultimately be static and reside in a different file and can be reused, and what ultimately goes in your main code will be greatly simplified. For example if you have a lot of areas to check for, following my example Kbutton could represent an area to check. You could put several Kbuttons in a Kform, and several other Kbuttons in another Kform, and if you need to switch between the areas to check you could set the Kform to be visible/invisible, or even set visibility at the button level. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |
|