"Questions that don't deserve their own thread" thread
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: "Questions that don't deserve their own thread" thread
Assign values to a table that use values from the same table after the table is initialized.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: "Questions that don't deserve their own thread" thread
Or in other words, this would work:
or this, which is worse actually in terms of stuf you'd need to write and/or copypaste:
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
}
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 True 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.
Re: "Questions that don't deserve their own thread" thread
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
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
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
-
- Prole
- Posts: 8
- Joined: Thu Oct 13, 2016 12:36 pm
Re: "Questions that don't deserve their own thread" thread
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?
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.
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
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: "Questions that don't deserve their own thread" thread
Note that there's no one good way of doing what you want, you have the freedom of doing things differently!
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 True 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.
-
- Prole
- Posts: 8
- Joined: Thu Oct 13, 2016 12:36 pm
Re: "Questions that don't deserve their own thread" thread
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.
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: "Questions that don't deserve their own thread" thread
I could have used better var/field names though. But yes, you build your own classes, if you want/need to.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.
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 True 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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 3 guests