![]() |
Programming Q & A
I would like to have a thread where programmers could post their questions, and those with more experience, etc... might answer. I think I'll begin.
Q1: Can I attach a Lua script named "functions", for example, to "main.lua", so that I could call any of the functions in the attached script from "main"? Q2: Does Lua support OOP programming? Could I declare class "tree" and, within it, function "new", and then call it "tree.new();"? Thanks! |
A1: Yes.
A2: Technically, no. It can be fudged though. |
There is already a FAQ thread stickied, but maybe this Q&A is a little different..
Quote:
I don't know about your second question.. |
I understand that some programmers are quite used to OOP, but imho there is nood need for OOP in LUA due to the low complexity of the apps that the Zen could actually handle. ;)
|
I think we can keep this thread together with the regular FAQ, here we might find more things to add to that thread via discussions and questions, and not have to clutter the FAQ thread with unnecessary posts.
|
Q1: I read at the X-Fi2/Lua wiki that math.random() returns a float. Is this true even if I provide arguments such as math.random(1,20)?
|
A1: I believe it always returns a float, yes. Not that I myself differ much between the different kinds of numerical storage in the LUA language. I only say "number = 3" and that's that. Then I can set it to a Random or whatever later.
|
Q: Can the same table hold values with different data types?
|
Quote:
Quote:
|
Quote:
|
Quote:
|
What exactly does the touch.move() function do? If I have a game with a background map which the user navigates by clicking and dragging, would I have to put touch.move() within a loop so that the screen continually updates as the user's finger moves, or does it take care of this for me?
|
Quote:
|
Quote:
|
Thanks! Just a little more specific, does touch.move() contain it's own loop so that everything inside repeats until touch.move() is no longer 1? For example:
Code:
if touch.move() == 1 then |
Well it doesn't loop by itself or something, but since touch.move() should be called in the whole control.read() thingy in your main loop, the code you quoted will repeat until touch.move() is no longer 1. Besides, none of the touch.events will get a value if you don't call control.read() and control.isTouch() first, so you can't use touch.move() by itself. I like think of this as the "1" is pushed through to the appropriate event; first control.read is one, then isTouch becomes one, and then the event.
Note that using touch.move() may cause delay, because there is a lot (too much) of screen-input send to the app when you move and everytime it has to do the calculations. You may notice a lag on the player, and if you do, you can eliminate some screen-input by doing for i=0, 100 do control.read() end just before the end in the code you quoted. This erases 100 screen-inputs everytime to smoothen things up. Oh and I have a question for you, I see many people posting codes using things like your viewX += newX, but everytime I try "+=" to add stuff to a value, it gives me errors.. does it work for you? |
I was just testing something from my Colonization game when I read your last post. The simulator did indeed give me errors on +=.
|
So I was testing my Colonization game, and I was getting this mysterious error when I tried the following:
Code:
screen.fillrect(XYtoUV(20, 20, 60, 60), green); |
I don't think you can do that.. - the function returns to a stack local to that function call. So calling another function with it would just push that whole stack onto one memory block of the new stack.. (i think.. :P)
One way you could get round it is to redefine the screen.drawrect function to work in iso.. or define a new one that converts it and then calls the regular one :) |
Thanks! Also, someone noted that drawing an image at less than 0,0 caused the player to crash, so I'm trying to get around it by drawing only the part of that image which is visible on screen. exampleImage:draw(0, 0, 40, 40, 80, 80), which is supposed to just draw part of an image, doesn't seem to do anything; when I use it, no image is drawn. Do we know that this function actually works?
|
| All times are GMT -5. The time now is 12:37 AM. |