Creating object Creator

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Creating object Creator

Post by Krizzu »

Hello World

I'd like to create simply Object creator with for loop:
for i=1,10 do

object.x=100
object.y=100
object.body=love.physics.newBody(world, object.x, object.y "dynamic")
object.shape=love.physics.newRectangleShape(21,21)
object.fixture=love.physics.newFixture(object.obj.body, object.obj.shape, 1)

end


But I got:
Attempt to index field "?" (a nil value)


So what's going on?

I came back to LUA and it's seems I forgot a little bit
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: Creating object Creator

Post by Larsii30 »

The table "object" has to been created before puttings something into it.

[edit deleted: does not work like this because of the physic things.]
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Re: Creating object Creator

Post by Krizzu »

Yea, I created it earlier, just didn't posted it.
object={}
And problem is still ariving

#EDIT

Eh, stupid me >_<

In loop, you must make table in table, which is:
object={}


for i=1,10 do
object={}
object.x=100
object.y=100
object.body=love.physics.newBody(world, object.x, object.y, "dynamic")
object.shape=love.physics.newRectangleShape(21,21)
object.fixture=love.physics.newFixture(object.body, object.shape, 1)

end
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Creating object Creator

Post by spir »

Krizzu wrote:Hello World

I'd like to create simply Object creator with for loop:
for i=1,10 do

object.x=100
object.y=100
object.body=love.physics.newBody(world, object.x, object.y "dynamic")
object.shape=love.physics.newRectangleShape(21,21)
object.fixture=love.physics.newFixture(object.obj.body, object.obj.shape, 1)

end


But I got:
Attempt to index field "?" (a nil value)


So what's going on?

I came back to LUA and it's seems I forgot a little bit


A function that creates tables (data structures) is usually called constructor or factory. What you want to do is probably something like this:

Code: Select all

make_objects = function ()
   local objectS = {}

   for i=1,10 do
      -- create new object
      local obj = {}
      -- describe its contents
      obj   = {}
      obj.x = 100
      obj.y = 100
      obj.body    = love.physics.newBody(world, obj.x, obj.y, "dynamic")
      obj.shape   = love.physics.newRectangleShape(21,21)
      obj.fixture = love.physics.newFixture(obj.body, obj.shape, 1)
      -- define it as item #i in objectS
      objectS[i] = obj
   end
   
   return objectS
end
Note: you should always call a table that represents a set of items (a collection) with a plural 's'. Unless there is an obvious name such as 'colony' for a set of ants or 'palette' for a set of colours (or 'staff' for a set of employees). Also, it is a good idea --to avoid mistakes or typos-- to use a shortcut name for items, in a loop that processes items in a row (as I did with 'obj' above).

Denis

OT: Seems the forum does not permit all of BBCode? (I tried to use [s] for srike-through). See http://en.wikipedia.org/wiki/BBCode
... la vita e estrany ...
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Creating object Creator

Post by Lafolie »

Code: Select all

t = {}

for x=1, 10 do
     t[x] = {}
     t[x].property = "value"
end
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 179 guests