Page 77 of 91

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

Posted: Fri Oct 21, 2016 2:30 pm
by raidho36
Assign values to a table that use values from the same table after the table is initialized.

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

Posted: Fri Oct 21, 2016 3:25 pm
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.

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

Posted: Fri Oct 21, 2016 8:54 pm
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

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

Posted: Sat Oct 22, 2016 8:16 am
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!

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

Posted: Sat Oct 22, 2016 9:15 am
by Nixola
Yes, LÖVE comes with LuaJIT.

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

Posted: Mon Oct 24, 2016 12:30 am
by timbre
Thanks!

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

Posted: Mon Oct 31, 2016 11:37 pm
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.

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

Posted: Tue Nov 01, 2016 12:17 am
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
--]]


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

Posted: Tue Nov 01, 2016 1:01 am
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.

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

Posted: Tue Nov 01, 2016 1:46 am
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)