"Questions that don't deserve their own thread" thread

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.
Locked
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: "Questions that don't deserve their own thread" thread

Post by raidho36 »

Assign values to a table that use values from the same table after the table is initialized.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by zorg »

Or in other words, this would work:

Code: Select all

msgbox = {
   padding = 10,
   container = {
      x = 10,
      y = screen.H/2, -- comma there not actually an error, which is neat
   }
}
msgbox.container.w = screen.W-msgbox.padding*2
msgbox.container.h = screen.H/5-msgbox.padding
msgbox.text = {
      x = msgbox.container.x,
      y = msgbox.container.y
   }
or this, which is worse actually in terms of stuf you'd need to write and/or copypaste:

Code: Select all

msgbox = {}
msgbox.padding = 10
msgbox.container = {}
msgbox.container.x = 10
msgbox.container.y = screen.W-msgbox.padding*2
-- ...you get the idea, but anyway, if you  can, keep some of the stuff in the initializer part if you can, otherwise you'll have to write a dozen lines like the above.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: "Questions that don't deserve their own thread" thread

Post by pgimeno »

Or this:

Code: Select all

do
    local PADDING = 10
    local X, Y = 10, screen.H/2
    msgbox = {
       padding = PADDING,
       container = {
          x = X,
          y = Y,
          w = screen.W-PADDING*2,
          h = screen.H/5-PADDING,
       },
       text = {
          x = X,
          y = Y
       },
    }
end
timbre
Prole
Posts: 2
Joined: Sat Oct 22, 2016 8:09 am

Re: "Questions that don't deserve their own thread" thread

Post by timbre »

Hey all! Question about LOVE and Lua. I've downloaded LOVE, and it works beautifully, but I'm curious because I didn't download or install Lua. Does download LOVE also download a form of Lua? I'm on a Windows machine if that's relevant. Thanks!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: "Questions that don't deserve their own thread" thread

Post by Nixola »

Yes, LÖVE comes with LuaJIT.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
timbre
Prole
Posts: 2
Joined: Sat Oct 22, 2016 8:09 am

Re: "Questions that don't deserve their own thread" thread

Post by timbre »

Thanks!
smunnelsnakzruule
Prole
Posts: 8
Joined: Thu Oct 13, 2016 12:36 pm

Re: "Questions that don't deserve their own thread" thread

Post by smunnelsnakzruule »

Hello again, I admit I am somewhat unfamiliar with lua, but more familiar with "typed" languages. Can someone explain in steps how I go about making classes, or "Meta-table archetypes" to pass as copies or "values" (not reference) to other tables so I can get a system of storing types or classes inside other ones. I tried making a global table and passing it to another table as what I hoped was a copy, sadly it complained that it initialized null. So I am confused. Maybe I need to use something like global.TableName with I pass it?

Code: Select all


[b]--[ WARNING BAD / WRONG SYNTAX ]--[/b]


--[Some file somewhere]--
--Global table

TableObjectClass = {

	value1 = 100,
	value2 = "cows",
	
	--Table within a table test?
	_myTableInventory = {
		thing1 = 2,
		thing2 = 4
	
	},

}



--[Some file somewhere else]--
--Function make copies and store them

GlobalTableStorage = {}


Function makeCopiesandfStore()
	-- Dont think lua has a "new" keyword
	Local _table = TableObjectClass  -- ???? pretty sure this is not correct
	GlobalTableStorage = {
		obj1 = _table 
	
	}
end

yes I realize some of this isnt spot on, I just need to understand how to do this. I probably need to study up Lua more.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by zorg »

Note that there's no one good way of doing what you want, you have the freedom of doing things differently! :3

Code: Select all

--[[ a.lua ]]--

-- a class
local class = {}

-- class member, not per-instance
class.meh = 0.0

-- method
class.foo = function(c,a)
  c.meh = a -- sets class variable
  c.kek = a -- sets instance variable
end

-- method
function class.bar(c)
  return c.meh -- returns class variable
end

-- method
function class:baz()
  return self.kek -- returns instance variable
end

local mtClass = {__index = class}

-- constructor
function new(a)
  local obj = {kek=1.0} -- new instance with an instance field/member
  obj = setmetatable(obj, mtClass) -- make it so the methods defined in class can be called from the instances
  obj.kek = a
  return obj
end

-- returning only the constructor; you could also return a separate table filled with class methods, if you wanted.
return new -- return classmethods

----------------------------------------------------------------------------------------------------------------------------------------------

--[[ main.lua ]]--

-- require in our defined class, this will return the constructor into bleh.
local bleh = require 'a'

-- a table holding instances of 'a'
local aTable = {}

for i=1, 10 do
  aTable[i] = bleh(i) -- will create class instances, that hold the number i within them.
end

for i=1,10 do print(aTable[i]:bar(), aTable[i]:baz()) end -- should print the following:

--[[
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
--]]

Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
smunnelsnakzruule
Prole
Posts: 8
Joined: Thu Oct 13, 2016 12:36 pm

Re: "Questions that don't deserve their own thread" thread

Post by smunnelsnakzruule »

Wow, thanks alot, so... I guess Lua makes you build your own class from a set of tables?

Is "return" the magic word here for making copies from a set of pre-built table functions?

Anyway, I'll study the example you gave me and look at the Lua reference manual again. thanks alot.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by zorg »

smunnelsnakzruule wrote:Wow, thanks alot, so... I guess Lua makes you build your own class from a set of tables?

Is "return" the magic word here for making copies from a set of pre-built table functions?

Anyway, I'll study the example you gave me and look at the Lua reference manual again. thanks alot.
I could have used better var/field names though. :3 But yes, you build your own classes, if you want/need to.

Technically, return just returns stuff. if inside a function, it returns whatever you put after it. If you put it at the end of a file, then it will return what you write after it when requiring the file itself. the local obj = {} line in the new function is what creates a new table, then i'm giving references to the "class" functions with setmetatable. (you can kinda do it without this, but then you'll need to make each instance have a copy of each "class" function, not just a reference to it, in essence)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Locked

Who is online

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