How to write this code in LUA?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

How to write this code in LUA?

Post 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
User avatar
crow
Party member
Posts: 186
Joined: Thu Feb 24, 2011 11:47 pm
Location: UK
Contact:

Re: How to write this code in LUA?

Post 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},};
Last edited by crow on Sat Mar 12, 2011 7:06 pm, edited 1 time in total.
Sir Kittenface
Möko IDE Codename (Erös) Returns Soon

I am dyslexic so if any of my replys confusing please just ask me to reword it as this will make things a lot easier for all parties lol.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Re: How to write this code in LUA?

Post by NÖÖB »

It's BlitzBasic Max
User avatar
crow
Party member
Posts: 186
Joined: Thu Feb 24, 2011 11:47 pm
Location: UK
Contact:

Re: How to write this code in LUA?

Post 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
Sir Kittenface
Möko IDE Codename (Erös) Returns Soon

I am dyslexic so if any of my replys confusing please just ask me to reword it as this will make things a lot easier for all parties lol.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: How to write this code in LUA?

Post 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.
User avatar
crow
Party member
Posts: 186
Joined: Thu Feb 24, 2011 11:47 pm
Location: UK
Contact:

Re: How to write this code in LUA?

Post 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 :P i am not really big on OOP :P
Sir Kittenface
Möko IDE Codename (Erös) Returns Soon

I am dyslexic so if any of my replys confusing please just ask me to reword it as this will make things a lot easier for all parties lol.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to write this code in LUA?

Post by Robin »

NÖÖB wrote:I really should read Programming in Lua.
You really should.
Help us help you: attach a .love.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Re: How to write this code in LUA?

Post 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
User avatar
crow
Party member
Posts: 186
Joined: Thu Feb 24, 2011 11:47 pm
Location: UK
Contact:

Re: How to write this code in LUA?

Post 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 :)
Sir Kittenface
Möko IDE Codename (Erös) Returns Soon

I am dyslexic so if any of my replys confusing please just ask me to reword it as this will make things a lot easier for all parties lol.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to write this code in LUA?

Post 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
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest