Page 1 of 2
How to write this code in LUA?
Posted: Sat Mar 12, 2011 6:36 pm
by NÖÖB
Hey guys, I'm wondering how I would go about converting this BASIC code to LUA.. What's getting me is the "field X[4] and "en:enemy = new enemy". I've been looking at code from the people on this forum, but couldn't find anything like this; I really should read Programming in Lua. But, if anyone could be bothered to have a go, please do
Code: Select all
type enemy
field X[4]
field Y[4]
end type
function createEnemy:enemy( ..parameters.. )
local en:enemy = new enemy
en.x[0] = 10
en.x[1] = 20
en.y[0] = 10
en.y[1] = 20
return en
end function
function drawEnemy(en.enemy, size, color)
...
end function
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 6:59 pm
by crow
NÖÖB wrote:Hey guys, I'm wondering how I would go about converting this BASIC code to LUA.. What's getting me is the "field X[4] and "en:enemy = new enemy". I've been looking at code from the people on this forum, but couldn't find anything like this; I really should read Programming in Lua. But, if anyone could be bothered to have a go, please do
Code: Select all
type enemy
field X[4]
field Y[4]
end type
function createEnemy:enemy( ..parameters.. )
local en:enemy = new enemy
en.x[0] = 10
en.x[1] = 20
en.y[0] = 10
en.y[1] = 20
return en
end function
function drawEnemy(en.enemy, size, color)
...
end function
Where did you get the code from ? that does not look like basic to me it looks like mixed up lua
Just from what I can see its missing some code from somewhere like I see it is stating tables but no table as been defined anywhere the en:enemy is enemy, as it is = to new enemy
from what I can see enemy should be soemthing like
Code: Select all
enemy = {y = {0=0, 1=0, 2= 0}, x = {0=0, 1=0, 2= 0},};
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:03 pm
by NÖÖB
It's BlitzBasic Max
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:11 pm
by crow
Maybe something like this then
Code: Select all
enemy = {y = {0=0, 1=0, 2= 0}, x = {0=0, 1=0, 2= 0},};
function createEnemy(n_x_f, n_x_s, n_y_f, n_y_s, )
local en = enemy;
en.x[n_x_f] = 10;
en.x[n_x_s] = 20;
en.y[n_y_f] = 10;
en.y[n_y_s] = 20;
return en;
end
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:17 pm
by BlackBulletIV
I don't know hardly anything about Basic, but it seems that Basic has a class (type) system. This isn't the case in Lua, you have to use the language's data structure "tables" to do it, along with metatables. However, there are a number of libraries that take care of this for you, like
MiddleClass.
crow wrote:Maybe something like this then
Code: Select all
enemy = {y = {0=0, 1=0, 2= 0}, x = {0=0, 1=0, 2= 0},};
function createEnemy(n_x_f, n_x_s, n_y_f, n_y_s, )
local en = enemy;
en.x[n_x_f] = 10;
en.x[n_x_s] = 20;
en.y[n_y_f] = 10;
en.y[n_y_s] = 20;
return en;
end
No, that wouldn't work. You're not copying "enemy". This would be better:
Code: Select all
function createEnemy(n_x_f, n_x_s, n_y_f, n_y_s)
local en = {
y = {0=0, 1=0, 2= 0},
x = {0=0, 1=0, 2= 0}
}
en.x[n_x_f] = 10
en.x[n_x_s] = 20
en.y[n_y_f] = 10
en.y[n_y_s] = 20
return en
end
Although that does seem a bit of a weird way of doing OOP.
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:20 pm
by crow
Or ok
I was not far off tho hehe it was not bad for a shot in the dark hehe
Edit I was doing like that as if the table was used else where also
i am not really big on OOP
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:29 pm
by Robin
NÖÖB wrote:I really should read Programming in Lua.
You really should.
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 7:50 pm
by NÖÖB
"Type enemy" is like a blueprint for an enemy-being, it would include parameters like X, Y, size, strenght, image, inventory..
"Function CreateEnemy:Enemy(100, 100, small, mediumstrong, lizard) creates an instance of a new enemy.. it could be used over and over again, spawning enemies of different appearance, strength etc.
I just used x[4] as an example to show that it's possible
Re: How to write this code in LUA?
Posted: Sat Mar 12, 2011 8:09 pm
by crow
NÖÖB wrote:"Type enemy" is like a blueprint for an enemy-being, it would include parameters like X, Y, size, strenght, image, inventory..
"Function CreateEnemy:Enemy(100, 100, small, mediumstrong, lizard) creates an instance of a new enemy.. it could be used over and over again, spawning enemies of different appearance, strength etc.
I just used x[4] as an example to show that it's possible
you would have to build the whole function far as I know as love is a game framework not really a rebuild default engine and i think thats that Basic is
so you would need to set the love.newgraphics command i think that will set the x and y and you would need to tell it the image file and so on, sorry I
have not got really deep with love I will start in the next few months soon as I get my IDE stable
Re: How to write this code in LUA?
Posted: Mon Mar 14, 2011 10:24 am
by miko
NÖÖB wrote:Hey guys, I'm wondering how I would go about converting this BASIC code to LUA.. What's getting me is the "field X[4] and "en:enemy = new enemy". I've been looking at code from the people on this forum, but couldn't find anything like this; I really should read Programming in Lua. But, if anyone could be bothered to have a go, please do
Code: Select all
type enemy
field X[4]
field Y[4]
end type
function createEnemy:enemy( ..parameters.. )
local en:enemy = new enemy
en.x[0] = 10
en.x[1] = 20
en.y[0] = 10
en.y[1] = 20
return en
end function
function drawEnemy(en.enemy, size, color)
...
end function
Forget about translating Basic code directly to lua. Your code will look better in Lua! Read about MiddleClass here:
http://love2d.org/wiki/MiddleClass and do something like:
Code: Select all
require 'middleclass'
Enemy=class('Enemy')
function Enemy:initialize(x1,x2,y1,y2)
self.X={x1,x2}
self.Y={y1,y2}
end
function createEnemy(x1,x2,y1,y2)
return Enemy:new(x1,x2,y1,y2)
end
local e1=createEnemy(10,20,10,20)
print(e1,e1.X[1], e1.Y[1])
Notes:
- you dont need/can not create "types" (records?) in Lua
- tables are 1-based