android
  #1  
Old 06-27-2010, 10:02 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default 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!
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 06-28-2010, 06:30 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

A1: Yes.

A2: Technically, no. It can be fudged though.
__________________
Only ONE more functions in the Wiki need addressing!
Zen X-Fi2 LUA Wiki
Want to protect your applications? Click Here!
Tower Defense Thread
Zen Lock - Protect Your Zen!
Reply With Quote

  #3  
Old 06-28-2010, 06:35 AM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

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

Quote:
Originally Posted by İreative View Post
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..
Reply With Quote

  #4  
Old 06-28-2010, 08:41 AM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

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.
__________________
Zen X-Fi2 Apps: Notee-fi (notepad) | TicTacToe | Mahjongg
http://www.starfare.eu/ - a free real-time strategy game.
Reply With Quote

  #5  
Old 06-29-2010, 05:31 AM
Habhome's Avatar
Habhome Habhome is offline
Moderator
 
Join Date: Jan 2010
Location: Sweden
Posts: 1,081
Default

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.
__________________
"If you are good enough at English to apologize, then there is no need to." - A good friend of mine
Discovered something about the X-Fi2 you think others may not know? Post it here so others can learn about it!
Have a question about X-Fi2 apps? Consult the FAQ before creating a thread about it.
Like my work? Tell your friends. Don't like it? Tell me so I can improve. ^.^
Reply With Quote

  #6  
Old 07-03-2010, 10:33 AM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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)?
Reply With Quote

  #7  
Old 07-03-2010, 04:55 PM
Habhome's Avatar
Habhome Habhome is offline
Moderator
 
Join Date: Jan 2010
Location: Sweden
Posts: 1,081
Default

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.
__________________
"If you are good enough at English to apologize, then there is no need to." - A good friend of mine
Discovered something about the X-Fi2 you think others may not know? Post it here so others can learn about it!
Have a question about X-Fi2 apps? Consult the FAQ before creating a thread about it.
Like my work? Tell your friends. Don't like it? Tell me so I can improve. ^.^
Reply With Quote

  #8  
Old 07-04-2010, 07:54 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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

  #9  
Old 07-04-2010, 08:50 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

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
__________________
Only ONE more functions in the Wiki need addressing!
Zen X-Fi2 LUA Wiki
Want to protect your applications? Click Here!
Tower Defense Thread
Zen Lock - Protect Your Zen!
Reply With Quote

  #10  
Old 07-05-2010, 07:42 AM
Habhome's Avatar
Habhome Habhome is offline
Moderator
 
Join Date: Jan 2010
Location: Sweden
Posts: 1,081
Default

Quote:
Originally Posted by ThievingSix View Post
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.
__________________
"If you are good enough at English to apologize, then there is no need to." - A good friend of mine
Discovered something about the X-Fi2 you think others may not know? Post it here so others can learn about it!
Have a question about X-Fi2 apps? Consult the FAQ before creating a thread about it.
Like my work? Tell your friends. Don't like it? Tell me so I can improve. ^.^
Reply With Quote

  #11  
Old 07-05-2010, 06:11 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Quote:
Originally Posted by Habhome View Post
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.
__________________
Only ONE more functions in the Wiki need addressing!
Zen X-Fi2 LUA Wiki
Want to protect your applications? Click Here!
Tower Defense Thread
Zen Lock - Protect Your Zen!
Reply With Quote

  #12  
Old 09-04-2010, 10:54 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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?
Reply With Quote

  #13  
Old 09-05-2010, 01:25 AM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

Quote:
Originally Posted by İreative View Post
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.
__________________
this is a link to my personal homepage which I haven't
Reply With Quote

  #14  
Old 09-05-2010, 01:48 AM
Habhome's Avatar
Habhome Habhome is offline
Moderator
 
Join Date: Jan 2010
Location: Sweden
Posts: 1,081
Default

Quote:
Originally Posted by İreative View Post
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.
__________________
"If you are good enough at English to apologize, then there is no need to." - A good friend of mine
Discovered something about the X-Fi2 you think others may not know? Post it here so others can learn about it!
Have a question about X-Fi2 apps? Consult the FAQ before creating a thread about it.
Like my work? Tell your friends. Don't like it? Tell me so I can improve. ^.^
Reply With Quote

  #15  
Old 09-13-2010, 10:54 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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?
Reply With Quote

  #16  
Old 09-14-2010, 02:39 PM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

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?
__________________
this is a link to my personal homepage which I haven't

Last edited by Brett_val; 09-14-2010 at 03:08 PM.
Reply With Quote

  #17  
Old 09-14-2010, 03:25 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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

  #18  
Old 10-13-2010, 10:04 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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?
Reply With Quote

  #19  
Old 10-14-2010, 03:42 PM
bzdbbb's Avatar
bzdbbb bzdbbb is offline
X-Fi2 Development Moderator
 
Join Date: Dec 2009
Location: Gloucestershire, UK
Posts: 94
Default

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
Reply With Quote

  #20  
Old 10-15-2010, 05:06 PM
İreative's Avatar
İreative İreative is offline
Junior Member
 
Join Date: Jun 2010
Location: Minnesota, USA
Posts: 39
Default

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?
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:28 AM.