abi>>forums

abi>>forums (http://www.anythingbutipod.com/forum/index.php)
-   Zen X-Fi2 Applications & Development (http://www.anythingbutipod.com/forum/forumdisplay.php?f=221)
-   -   Programming Q & A (http://www.anythingbutipod.com/forum/showthread.php?t=55930)

İreative 06-27-2010 10:02 PM

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!

ThievingSix 06-28-2010 06:30 AM

A1: Yes.

A2: Technically, no. It can be fudged though.

Brett_val 06-28-2010 06:35 AM

There is already a FAQ thread stickied, but maybe this Q&A is a little different..

Quote:

Originally Posted by İreative (Post 480298)
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"?

If you put dofile("functions") in the main.lua, it will read everything from the file "functions" and load it into the memory so you can indeed call and use those functions/variables.

I don't know about your second question..

ZaPx64 06-28-2010 08:41 AM

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. ;)

Habhome 06-29-2010 05:31 AM

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.

İreative 07-03-2010 10:33 AM

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)?

Habhome 07-03-2010 04:55 PM

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.

İreative 07-04-2010 07:54 PM

Q: Can the same table hold values with different data types?

ThievingSix 07-04-2010 08:50 PM

Quote:

Q: Does Lua have a increment/decrement operator, or must I use "n = n + 1"?
n += 1

Quote:

Q: Can the same table hold values with different data types?
Yes

Habhome 07-05-2010 07:42 AM

Quote:

Originally Posted by ThievingSix (Post 481815)
n += 1

Things like this would be great to have in the X-Fi2 LUA wiki so it is easily findable for beginners. So this together with the modulo operator and similar things could be handy to have in a little section.

ThievingSix 07-05-2010 06:11 PM

Quote:

Originally Posted by Habhome (Post 481874)
Things like this would be great to have in the X-Fi2 LUA wiki so it is easily findable for beginners. So this together with the modulo operator and similar things could be handy to have in a little section.

Reason I didn't was because I wanted to have only X-Fi2 information on that page only. That's why I linked to the full LUA manual.

İreative 09-04-2010 10:54 PM

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?

Brett_val 09-05-2010 01:25 AM

Quote:

Originally Posted by İreative (Post 497009)
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?

I'm not sure if I understand the problem, but it works the same as the other touch events, and they are usually all in the same loop.

Habhome 09-05-2010 01:48 AM

Quote:

Originally Posted by İreative (Post 497009)
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?

Don't take my word for it since I haven't used the touch.move() a lot, but I think that if you have a screen.update() within the action performed there the screen will be redrawn at the highest framerate possible unless it crashes due to too little sleep/wait time. I think, never quite figured that one out myself.

İreative 09-13-2010 10:54 PM

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
  newX,newY = touch.pos();
  viewX += newX;
  viewY += newY;
  screen.update;
end

Or must I put it inside my own while loop?

Brett_val 09-14-2010 02:39 PM

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?

İreative 09-14-2010 03:25 PM

I was just testing something from my Colonization game when I read your last post. The simulator did indeed give me errors on +=.

İreative 10-13-2010 10:04 PM

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);
where XYtoUV is a function that converts coordinates, being that my game is Isometric. I eventually discovered the problem: screen.fillrect() expects 5 arguments; startX, endX, startY, endY, and color. It was counting XYtoUV(20, 20, 60, 60) as one argument (even thought the function returns 4 numbers), and green as one argument. Can I make the interpreter run function XYtoUV() when it reaches that statement and then screen.fillrect() without having to save it to a variable first?

bzdbbb 10-14-2010 03:42 PM

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 :)

İreative 10-15-2010 05:06 PM

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:22 PM.