Lua get table that function is attached to - lua

Hello I want to get the table that a function is attached to, I can't really find a nice way to explain it but I think i've explained it good enough within the code for. Basically I need to get the table that the function is attached to from another function, without passing in the table.
function DrawRect()
print(debug.getinfo(1).name) -- this gets the name of the function that is invoking DrawRect ('Paint')..
-- I want to be able to get the table that is attached to this function
-- So I can do table.x inside this function, and have it print 123
end
local r = math.random(1, 100)
_G["abc" .. r] = {
x = 123,
Paint = function(self)
DrawRect()
end
}
_G["abc" .. r]:Paint()
Example of the problem i'm trying to solve
This is my current code right now
function DrawRect(x,y,w,h)
draw.DrawRect(x,y,w,h)
end
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,x,y,w,h)
Color(40,40,40)
DrawRect(x,y,w,h) -- Draws dark background
end
}
As you can see paint has 4 args, x,y,w,h. I want to do away with x,y and only have w,h. I want to achieve this like this.
function DrawRect(x,y,w,h)
local relative_x = parent_table_of_paint.INTERNAL.draw_x
local relative_y = parent_table_of_paint.INTERNAL.draw_y
draw.DrawRect(relative_x + x, relative_y + y,w,h)
end
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,w,h)
Color(40,40,40)
DrawRect(0,0,w,h) -- Draws dark background
end
}
I know you can't see some of the properties in my example, but they are there.
Edit 2:
I am recreating the a frame, "VGUI".
https://wiki.facepunch.com/gmod/draw.RoundedBox
draw.RoundedBox( number cornerRadius, number x, number y, number width, number height, table color )
and as you can see this has the functionality I want
https://wiki.facepunch.com/gmod/PANEL:Paint
local panel = vgui.Create( "DPanel" )
panel:SetSize( 100, 100 )
panel:SetPos( ScrW() / 2 - 50, ScrH() / 2 - 50 )
function panel:Paint( w, h )
draw.RoundedBox( 8, 0, 0, w, h, Color( 0, 0, 0 ) )
end

A function is a first class value, so there may be many tables and variables that have a reference to the same function. There is no way for that function to know what those tables and variables are.

That's not how you would implement something like that. You don't implement a function that through some magic gets its caller's container so it can access its other values.
DrawRect should just draw a rect. If you want to draw a rect somewhere else you should provide that offset through the parameters of DrawRect.
I modified your button so it will simply put the buttons x and y coordinates (if they exist) into DrawRect
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,w,h)
Color(40,40,40)
local x = self.x or 0
local y = self.y or 0
DrawRect(x,y,w,h) -- Draws dark background
end
}
That way you can call Button:Paint(20,30) to draw a 20 by 30 rectangle at the buttons coordinates. If you want to add an offset, do that outside of DrawRect
# Edit 2:
This is something different. The host program will call the Paint function.
vgui.Create( "DPanel" ) will return a new panel instance and the game will add a reference to it to a list. Everytime the gui is updated it will call the Paint functions of all the panels in that list. That's how the function knows width and height of the panel.

Related

Drawing a Matrix

Im trying to generate a random map using a matrix but I dont really know how. Here is the
function for the matrix. wMap and hMap are the width and height, and mapSprites is a table containing some ground sprites. Also how can I draw the matrix? Im sorry if this is too much of a question, but Im really in need for some help
function buildMap(wMap, hMap)
for i = 1, wMap do
mt[i] = {}
for j = 1, hMap do
mt[i][j] = math.random(mapSprites)
end
end
end
Generating a random map in any programming language will utilize two core concepts: The language's random function and nested for loops, two for the case of a map/matrix/2d array.
The first problem, is you may or may not have mt initialized outside the function. This function assumes the variable exists outside of the function and each time the function is called it will overwrite mt (or initialize it for the first function call) with random values.
The second problem, the width, wMap, and height, hMap, of the map are in the wrong order, as maps/matrices/2d arrays first iterate over the height (y dimension) and then the width (x dimension).
The last problem, mapSpripes also has to be declared outside the function (which is not clear with your code snippet), which will be the highest possible value the random function can generate. You can read more about math.random here: http://lua-users.org/wiki/MathLibraryTutorial
Consider this function I wrote that makes those adjustments as well as has some additional variables for the minimum and maximum random value. Of course, you can remove these to have it fit your intended purposes.
function buildMap(wMap, hMap)
local minRand = 10
local maxRand = 20
for y = 1, hMap do
matrix[y] = {}
for x = 1, wMap do
matrix[y][x] = math.random(minRand, maxRand)
end
end
end
I suggest you use this function as inspiration for your future iteratins. You can make minRand and maxRand parameters or make matrix a returned value rather than manipulating an already declared matrix value outside of the function.
Best of luck!
EDIT:
Regarding your second question. Look back at the section I wrote about nested for loops. This will be crucial to "drawing" your map. I believe you have the building blocks to resolve this issue yourself as there isn't enough context provided about what "drawing" looks like. Here is a fundamentally similiar function, based on my previous function, on printing the map:
function printMap(matrix)
for i = 1, #matrix do
for j = 1, #matrix[i] do
io.write(matrix[i][j] .. " ")
end
io.write("\n")
end
end
For choosing random sprite, I recommend you to create a table of sprites and then save index of sprite in matrix. Then you can draw it in same loop, but now, you will iterate over matrix and draw sprite based on sprite index saved in matrix in position given by matrix position (x and y in loop) times size of sprite.
local sprites, mt = {}, {}
local spriteWidth, spriteHeight = 16, 16 -- Width and height of sprites
function buildMap(wMap, hMap)
mt = {}
for i = 1, wMap do
mt[i] = {}
for j = 1, hMap do
mt[i][j] = math.random(#sprites) -- We choose random sprite index (#sprites is length of sprites table)
end
end
end
function love.load()
sprites = {
love.graphics.newImage('sprite1.png'),
love.graphics.newImage('sprite2.png'),
-- ...
}
buildMap()
end
function love.draw()
for y, row in ipairs(mt) do
for x, spriteIndex in ipairs(row) do
-- x - 1, because we want to start at 0, 0, but lua table indexing starts at 1
love.graphics.draw(sprites[spriteIndex], (x - 1) * spriteWidth, (y - 1) * spriteHeight)
end
end
end

Why this lua function is modifying tables it shouldn't?

I have this function in my code to load and setup sizes for sprites.
function aux.Sprite:setTexture(renderer,imgPath)
... -- Not important for this question
img = loadImage(renderer,imgPath)
self.texture = img.texture
self.rect.w = img.w
self.rect.h = img.h
end
(loadImage here is the function implemented in C, and is returning the correct values)
Using it should be easy enough
bg = aux.Sprite:new()
bg:setTexture(R, "testfiles/bg.png")
ship = aux.Sprite:new()
ship:setTexture(R, "testfiles/testship.png")
The problem is that after the second call for setTexture the values for the FIRST sprite is changed!
for example
bg = aux.Sprite:new()
bg:setTexture(R, "testfiles/bg.png")
print(bg.rect.w)
ship = aux.Sprite:new()
ship:setTexture(R, "testfiles/testship.png")
print(bg.rect.w)
should return
1920 1920
because I'm printing the width for bg twice
but I'm getting
1920
300
That is, the second setTexture changes the value for "bg" and not only for "ship".
My guess is that self.rect.w = img.w is setting a "pointer", or whatever is called in lua, to img.w and when I use the function later this pointer is updated in all references?
What I'm doing wrong here? Is this the correct lua behavior?
PS: The definition of the Sprite:new function as asked
function aux.Sprite:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
There is nothing in the provided code that actually creates the rect (or aux.Sprite for that matter). I guess this means that is done via something like
aux.Sprite = { rect = {} }
This is a problem because it means all your sprites share the same rect.
aux.Sprite:new() returns a new empty table that has the metatable and its __index set to Sprite. Thus when in setTexture self.rect is searched in this empty table the one from Sprite is returned via __index.
You need to make sure every sprite has its own unique rect.
I don't really know what is the typical Lua object pattern here, but you can eg. have self.rect = { w = img.w, h = img.h } in setTexture or maybe o.rect = {} in new - something that actually sets rect to a new table for this particular sprite.

How to create textbox in love2d

I have been trying to make a box for a user to input data once the user clicks the box. This is what I have so far, but clicking the box does not cause text input to be added to it. Where is the issue?
function love.load ()
txt1 = ""
columnx = { 50, 160, 260, 375, 495, 600, 710 }
columny = { 130, 230, 330, 440, 540, 640 }
if show1 == true then
function love.textinput(t)
txt1 = t
end
end
end
function love.mousepressed (x, y)
if
x >= columnx[4] and
x <= 435 and
y >= columny[1] and
y >= 145 then
show1 = true
end
end
function love.draw ()
love.graphics.print(txt1, columnx[4], columny[1])
end
You're pretty much on the way, but I'll give you some tips on how to create a basic textbox.
Firstly, consider a conceptual textbox to be a singular table, that contains everything it needs to know about itself to properly update and render. It's much easier to reason about something that is self contained.
A textbox needs to know its position, its size, whether it is active, and which text it contains. We can condense that into a table that looks like the following.
local textbox = {
x = 40,
y = 40,
width = 400,
height = 200,
text = '',
active = false,
colors = {
background = { 255, 255, 255, 255 },
text = { 40, 40, 40, 255 }
}
}
(We also store some color info.)
After that the simple way to add text is through love.textinput, as you've seen. In your code we only check if the textbox is active once, in love.load, which it of course isn't since we likely haven't taken any user input yet. Instead of attempting to overload the function, we simply check if the textbox is active or not inside the handler, and proceed accordingly.
function love.textinput (text)
if textbox.active then
textbox.text = textbox.text .. text
end
end
We've covered how to check if the user has clicked within a rectangular area in the question: Love2d cursor positions. We want to deactivate the textbox if it is currently active and the user clicks outside of its space.
function love.mousepressed (x, y)
if
x >= textbox.x and
x <= textbox.x + textbox.width and
y >= textbox.y and
y <= textbox.y + textbox.height
then
textbox.active = true
elseif textbox.active then
textbox.active = false
end
end
And finally we need to render our textbox. We use unpack to expand our color tables, and love.graphics.printf to make sure our text wraps within our textbox's space.
function love.draw ()
love.graphics.setColor(unpack(textbox.colors.background))
love.graphics.rectangle('fill',
textbox.x, textbox.y,
textbox.width, textbox.height)
love.graphics.setColor(unpack(textbox.colors.text))
love.graphics.printf(textbox.text,
textbox.x, textbox.y,
textbox.width, 'left')
end
These are the basic ideas of creating a very rough textbox. It's not perfect. Note that you will need to consider what happens when the text gets longer height-wise than we initially set our textbox's height to, as the two are only loosely related.
To make your program easier to read, and easier to extend, everything you've seen above should really be placed into their own functions that handle textbox tables, instead of cluttering the love handlers with common code. Take a look at Chapter 16 of Programming in Lua, which covers Object-Oriented Programming - a generally vital topic for game development.
See the love.textinput page on how to handle a backspace, to delete characters.
Some extra things to think about:
How can we distinguish an active textbox from an inactive one?
How can we create a list of textboxes, so we can have multiple on screen (but only one active)?
I guess, the love.textinput() callback function is what you are looking for. But as the term 'callback' implies, it will be called by the LÖVE engine, not your code. It will be called whenever the user inputs some text while your game is running. Therefore you need to put it outside the love.load() function.
In the love2d.org wiki is an example for this (the lower one).
As for your example, move the love.textinput() out of love.load() and add an if statement:
function love.load()
txt1 = ""
columnx = {50, 160, 260, 375, 495, 600, 710}
columny = {130, 230, 330, 440, 540, 640}
end
function love.textinput(t)
if (show1) then
txt1 = txt1 .. t
end
end
-- The rest of your code.
-- And maybe mix it with the 'backspace example' from the wiki...
-- But you also might want to have some function to set 'show1' back to 'false' after the text input. Maybe something like this:
function love.keypressed(key)
if (key == "return") and (show1) then
show1 = false
end
end
I hope I could help you a bit!

Background infinite using corona sdk

I'm trying to scroll side a background in corona sdk (infinity background)
I used two images repeated (854x176).
I tried this function:
function mov(self, event)
if self.x < -854 then
self.x = 854
else
self.x = self.x - self.speed
end
end
it's working just fine but the problem that a small white space occurred between repetitions.
Is there a better way to do this?
One way of doing this would be to take advantage of Graphics 2.0 Engine's feature called repeating fills.
Here are the steps:
Set the default texture wrapping mode for x (you could do the same for y too):
display.setDefault("textureWrapX", "mirroredRepeat")
The modes for wrapping are:
"clampToEdge" - (default) clamped fill won't repeat
"repeat" - fill is repeated as if same tiles were placed side by side
"mirroredRepeat" - fill is repeated in a mirrored pattern, each tile being a mirror image of the one next to it
Create a rectangle the size of the background you want to have, eg. full screen
local background = display.newRect(display.contentCenterX, display.contentCenterY, 320, 480)
Fill your display object with your background image
background.fill = {type = "image", filename = "background.jpg"}
Animate it using whatever method fits your app. Below's just one way:
local function animateBackground()
transition.to(background.fill, {time=5000, x=1, delta=true, onComplete=animateBackground})
end
animateBackground()
Here you simply run transition on x property of background.fill object, delta=true indicating that we're rather using changes in x value, not final ending values (see here).
Play with the values for time, x, set delta to false, play with wrapping modes, just to see what effect it has on animation. You might even accidentally discover some cool effect which you might want to use later...
Check this excellent tutorial by Brent Sorrentino, who goes through more details on fills. Additionally, see sample code in CoronaSDK under Graphics-Premium/PatternFill.
Full code:
display.setDefault("textureWrapX", "mirroredRepeat")
local background = display.newRect(display.contentCenterX, display.contentCenterY, 320, 480)
background.fill = {type = "image", filename = "background.jpg" }
local function animateBackground()
transition.to( background.fill, { time=5000, x=1, delta=true, onComplete=animateBackground } )
end
animateBackground()

How can I get the colour (fill) of a DisplayObject in response to an event like a touch in Corona SDK?

Can anyone help me find out how can I can get the color of rectangle in corona? That rectangle I already filled with color, so now I want to get that color when I touch on rectangle.
Create your rectangle:
local rectangle = display.newRect(0, 0, 100, 100)
Put your color in RGBA (you can leave out the A) format in a table, and store it as a "custom property" for the rectangle:
rectangle.fillColor = {110, 30, 25}
Through the magic of the unpack function, which returns the values of a table, pass the table to setFillColor:
rectangle:setFillColor( unpack(rectangle.fillColor) )
Now you can always get the color like so:
print( unpack(rectangle.fillColor) ) --> 110 30 25
or
print( rectangle.fillColor ) -- simply returns the table
or to put each color in a variable:
local red, green, blue, alpha = unpack(rectangle.fillColor)
You'll see how this can come in handy for other things as well.
EDIT
Just thought of another cool way of doing it, by highjacking the setFillColor function:
local function decorateRectangle(rect)
rect.cachedSetFillColor = rect.setFillColor -- store setFillColor function
function rect:setFillColor(...) -- replace it with our own function
self:cachedSetFillColor(...)
self.storedColor = {...} -- store color
end
function rect:getFillColor()
return unpack(self.storedColor)
end
end
local rectangle = display.newRect(0, 0, 100, 100)
decorateRectangle(rectangle) -- "decorates" rectangle with more awesomeness
Now you can use setFillColor to set color as normal, AND getFillColor to return it :)
rectangle:setFillColor(100, 30, 255, 255)
print(rectangle:getFillColor())
in short: you cannot 'get' the fill colour. you have to save it yourself..
This is also another method to obtain color of the rect.
create a table of colors
local colors = { {255,0,0}, {0,0,255}, {0,255,0}, {255, 255, 0}, {255,0,255}}
Then when you create your rectangle, make it look like this:
local rect = display.newRect(0, 0,100,100)
rect.color = math.random(1,5)
rect:setFillColor(unpack(colors[rect.color]))
Now if you want to obtain the color of the rect do like this
local appliedcolor = colors[rect.color];
Thanks to https://forums.coronalabs.com/topic/18414-get-fill-color-of-an-object/
A DisplayObject's many property values, including RGB values for the fill color, can be inspected using object._properties. DisplayObject introspection was added in Build 2014.2511, i.e. two years after this question was asked.
The information is returned as a JSON formatted string, so the json.* library is used to extract the property (properties) of interest.
Assuming a DisplayObject called object, you would do something like:
local json = require( "json" )
local decoded, pos, msg = json.decode( object._properties )
if not decoded then
-- handle the error (which you're not likely to get)
else
local fill = decoded.fill
print( fill.r, fill.g, fill.b, fill.a )
end

Resources