I have a menu screen that has an options button. when I click on this button it load the options.lua file perfectly fine.
love.filesystem.load( "options.lua" )( ) love.load( )
On the option screen I want to add a back button to go back to the main menu. In my head to do this I need to unload the options.lua file.
function love.mousepressed( x, y)
if x > 275 and x < 320 and y > 305 and y < 325 then
end
end
Although Paul's answer is one option, you could approach this differently:
You are thinking about this in a way that will cause you a lot of grief, because in Lua, and many other languages loading files is not meant as a way to run code at variable times. Although this might make you change more code than you want, consider creating a variable for GUI States and only draw what you want if that variable has a certain value. Eg:
Add to your love.load function:
function love.load()
-- Set the gui state to the defualt ( the main menu )
guistate = "menu" -- You probably should use an integer, am using a string for the purpose of clarity.
end
This Goes In the Same Place:
function love.mousepressed( x, y)
if x > 275 and x < 320 and y > 305 and y < 325 then
-- The back button is pressed, change the state to the menu.
guistate = "menu"
end
end
Add to your love.draw function:
function love.draw()
if guistate = "menu" then
-- Draw the stuff for your main menu
elseif guistate = "options" then
-- Draw the stuff for your options menu.
end
end
Extras
Also take a peek at these GUI libraries if you are interested:
Love Frames, and Quickie
There is no way to "unload" that file; you can only negate the effect of its loading. You execute the content of options.lua in that fragment and if it has something like a = 5, to undo this change, you need to save the value of a before executing the code in options.lua and then restore the value later.
Something like this may work for a:
local default = {}
default.a = a -- save the current value of a (if any)
love.filesystem.load( "options.lua" )( ) love.load( )
function love.mousepressed( x, y)
if x > 275 and x < 320 and y > 305 and y < 325 then
a = default.a
end
end
You can go through any other value (for example, have a list of names you want to restore). If you want, you can save all values in the global table and restore them later by iterating over pairs(_G). If you deal with tables, rather than simple values, you'll need to use deep copy to save and restore values.
Related
So I have a task to be done which is to program the robot (AUBO) to pick different objects and place them in a certain order (Point A, B, C, D).
I'm using some vision system known as pim60. So if an object is detected it will go and pick and the rest of the program are waypoints to drop products. The first problem is I want it to go to the next waypoint to drop the and the second thing is, the next drop point cannot be skipped until an object is detected for that drop point.
In my own code, I wrote a rather lengthy program like this.
::LoopA::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position A
else
goto LoopA
end
::LoopB::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position B
else
goto LoopB
end
::LoopC::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position C
else
goto LoopC
end
::LoopD::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position D
else
goto LoopD
end
There is no error and the program runs as expected. However, I'm wondering if there is any better way to do it.
The only generally accepted use-case for goto is error handling, e.g. to jump forward to the cleanup code. But even for that it usually can and should be avoided.
You can probably do something like this:
-- loop B
repeat
take photo, etc.
located = ...
until(located == 1)
Drop at position B
Also, if you're repeating the same code three times, you should extract it into a function, and maybe give the position as a parameter. Or at least put the whole thing into a for loop.
I am making a roguelike in Love2D as a hobby project. My approach is to try and use as much of the native capabilities of Lua and the Love2D (0.10.1) API as possible, without relying on fancy libraries like middleclass or HUMP, so as to learn more about the language.
After reading PiL's chapters on OOP and seeing the power there, I decided to set up a Mob class (using metamethods to emulate class functionality) that encompasses the players, monsters, and other NPCs (anything that can move). So, far, it's working beautifully, I can create all kinds of instances easily that share methods and all that stuff. But there's a lot of things I don't know how to do, yet, and one of them is holding my prototype up from further progress.
Setting up collision with the map itself wasn't too bad. My maps are tables full of tables full of integers, with 0 being the floor. The game draws "." and "#" and "+" and such to denote various inanimate objects, from each table. Player 1 moves using the numpad, and their position is tracked by dividing their raw pixel position by 32 to create a grid of 32x32 "tiles". Then, inside love.keypressed(key), I have lines like:
if key == "kp8" and currentmap[player1.grid_y - 1][player1.grid_x] == 0 then
player1.grid_y = player1.grid_y - 1
and so on, with elseifs for each key the player can press. This prevents them from walking through anything that isn't an open floor tile in the map itself.
But, I'm trying to implement some kind of "collision detection" to prevent MOBs from walking through each other and to use in writing the rules for combat, and this is trickier. I had a method in place to calculate the distance between mobs, but I'm told this might eventually cause rounding errors, plus it had to be written for each combination of mobs I want to test, individually.
What I'd like to know is: Is there a known (preferably elegant) way to get all instances of a particular class to pass some number of values to a table?
What I'd like to do is "ask" every Mob on a given map where they are, and have them "report" self.grid_x and self.grid_y to another layer of map that's just for tracking mobs (1 if self.is_here is true, 0 if not, or similar), that gets updated every turn. Then, I could implement collision rules based on coordinates being equal, or maybe a foo.is_here flag or something.
I have only vague ideas about how to proceed, however. Any help would be appreciated, including (and maybe especially) feedback as to a better way to do what I'm trying to do. Thanks!
A simple idea is to store "who is here" information for every cell of the field and update this information on every move of every object.
function create_game_field()
-- initialize a table for storing "who is here" information
who_is_here = {}
for y = 1,24 do
who_is_here[y] = {}
for x = 1,38 do
who_is_here[y][x] = 0
end
end
end
function Mob:can_move(dx, dy)
local u = currentmap[self.y + dy][self.x + dx]
local v = who_is_here[self.y + dy][self.x + dx]
if u == 0 and v == 0 then
return true
else
end
end
function Mob:move(dx, dy)
-- update "who is here"
who_is_here[self.y][self.x] = 0
self.x, self.y = self.x + dx, self.y + dy
who_is_here[self.y][self.x] = 1
end
function Mob:who_is_there(dx, dy) -- look who is standing on adjacent cell
return who_is_here[self.y + dy][self.x + dx] -- return mob or nil
end
function Mob:roll_call()
who_is_here[self.y][self.x] = 1
end
Usage example:
-- player1 spawns in at (6,9) on the grid coords
player1 = Mob:spawn(6,9)
-- player1 added to who_is_here
player1:roll_call()
Then, in love.keypressed(key):
if key == "kp8" and player1:can_move(0, -1) then
player1:move(0, -1)
end
There are a few ways you could get all your instances data but one of the simpler ones is probably to have them all be added to a table when they are created. Providing you add the entire table for that instance, all the values will update in the main table because it acts like a collection of pointers.
function mob:new( x, y, type )
self.x = 100
self.y = 200
self.type = type
-- any other declarations you need
table.insert(allMobs, self)
return self
end
Here we insert all the mobs into the table 'allMobs'. Once we have that we can simply iterate through and get all our coordinates.
for i, v in ipairs(allMobs) do
local x, y = v.x, v.y
-- Do whatever you need with the coordinates. Add them to another table, compare
-- them to others, etc.
end
Now we have a table with all our mobs in it and a way to access each of their positions. If you have any further inquiries then let me know.
I'm writing a lua LÖVE program as a school project.
The task is something about ants, that need to find food, take some to the nest they came from and on the way leaving a trace of pheromons. In addition we've write a program visualizing the process. For 100 ants, 5 food sources and all this in a space of 500x500 squares
I chose lua LÖVE for the visualization and wrote the following code:
function love.load()
p = 500 -- Starting position
xNest, yNest = p, p -- Initializing nest position
xAnt1, yAnt1 = p, p -- Initializing ant position
xAnt2, yAnt2 = p, p
end
-- Changes position every frame.
function love.update(dt)
-- AntI // See what I did there?
xAnt1 = xAnt1 + math.random (-2, 2) -- Change position by a random number between 2 steps forward and 2 steps backward
yAnt1 = yAnt1 + math.random (-2, 2) -- Change position by a random number between 2 steps sideways
xAnt2 = xAnt2 + math.random (-2, 2)
yAnt2 = yAnt2 + math.random (-2, 2)
end
-- Draw ants and nest.
function love.draw()
-- Nest
love.graphics.setColor(0, 255, 255) -- set drawing color green
love.graphics.rectangle("line", xNest, yNest, 2, 2) -- draw a nest at xNest, yNest with a size of 2x2
-- Ant
love.graphics.setColor(255, 255, 255) -- set drawing color white
love.graphics.rectangle("line", xAnt1, yAnt1, 2, 2) -- draw an ant at xAnt(number of ant), yAnt(number of ant) with a size of 2x2
love.graphics.rectangle("line", xAnt2, yAnt2, 2, 2)
end
Since my task is to do what I did in
xAntX, yAntX = p, p 100 times, whereby X I mean the number for the ant, I need some kind of loop that creates xAntX, yAntX = p, p, xAntX = xAntX + math.random (-2, 2) , yAntX = yAntX + math.random (-2, 2) and love.graphics.rectangle("line", xAntX, yAntX, 2, 2 a 100 times.
I tried a for loop, but it always yelled at me for trying to append a variable ´i´ to the initialization xAnt .. i, yAnt .. i and then count i++ with i = i + 1.
Make xAnt and yAnt tables, and access individual entries as xAnt[i] and yAnt[i].
While this question is quite old, if you're new to Lua and ended up here with the same question, I'd like to propose a different solution which is in my opinion more elegant than the accepted answer:
One simple method of storing multiple properties in a structured way is to store them as nested "object"-like tables in an "array"-like table. For example, initialise an empty ants table like so:
local ants = {}
Then, use a numeric for to add the desired amount of ants to the ants table:
for i = 1, 100 do
ants[#ants + 1] = { x: ..., y: ... }
end
Each ant itself is also a table, but instead of numeric keys, it uses string keys. See Lua's documentation on tables for more information. Now, you can loop over all the ants in the list and update each ant using:
for _, ant in ipairs(ants) do
ant.x = ant.x + math.random(-2, 2)
ant.y = ant.y + math.random(-2, 2)
end
Specific ants may be updated directly through their index. To update the second ant, use ants[2].
The benefit of this approach is that you only have one global variable which holds all your ants, instead of having separate global variables for each individual property of each ant. This generally makes the code easier to reason about and aids in debugging as well.
Happy developing fellow Luanatics :D
TL;DR Randomly access every tile in a tilemap
I have a way of generating random positions of tiles by just filling an entire layer of them (its only 10x10) then running a forloop like for (int x = 0; x < 13; x++)
{
for (int y = 0; y < 11; y++)}}
Where I randomly delete tiles. I also have a cap to this, which is at about 30. The problem is that when the loop runs over, it uses up the cap to the left (Because it starts at x=0 y=0 then does x0 x1 x2 x3 ...). I tried randomly generating coordinates, but this didn't work because it doesnt go over all the coordinates.
Does anyone know a better practice for scanning over every coordinate in a map in random order?
Number your tiles 0 to n anyway you want. Create a NSMutableIndexSet, and add all to it. Use a random number generator scaled to the number of items still in the index set (actually the range of first to last), grab one, then remove it from the set. If the random number is not in the set generate a new be, etc, until you find one in the set.
I think the best practice to accomplish this is via double hashing. To find out more read this link: Double Hashing. I will try to explain it simply.
You have two auxiliary hash functions which need to be pre-computed. And a main hash function which will run in a for loop. Lets test this out (this will be pseudo code):
key = random_number() //lets get a random number and call it "key"
module = map_size_x // map size for our module
//form of hash function 1 is: h1(key) = key % module, lets compute the hash 1 for our main hash function
aux1 = key % module
//form of hash function 2 is: h2(key) = 1 + (key % module'), where module' is module smaller for a small number (lets use 1), lets compute it:
aux2 = 1 + (key % (module - 1))
//the main hash function which will generate a random permutation is in the form of: h(key, index) = (h1(key) + index*h2(key)) % module. we already have h1 and h2 so lets loop this through:
for (i = 0; i < map_size_x; i++)
{
randomElement = (aux1 + i*aux2) % module //here we have index of the random element
//DO STUFF HERE
}
To get another permutation simply change the value of key. For more info check the link.
Hope this helps. Cheers.
I've been trying to get input with IUP to make a small pong game. I wanted to try some input, and tried some of the code that comes with the IUPGL examples, but the input doesn't seem to be working at all. Here's the code as it stands so far:
require "iuplua"
require "iupluagl"
require "luagl"
paddle = {x = -0.9, y = 0.2}
function drawPaddle(x, y)
gl.Begin(gl.QUADS)
gl.Color(0.0, 0.5, 0.0)
gl.Vertex(x, y)
gl.Vertex(x + 0.1, y)
gl.Vertex(x + 0.1, y - 0.4)
gl.Vertex(x, y - 0.4)
end
canvas = iup.glcanvas{buffer = "DOUBLE", rastersize = "300x300"}
function canvas:action(x, y)
iup.GLMakeCurrent(self)
gl.ClearColor(0.0, 0.0, 0.0, 0.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Clear(gl.DEPTH_BUFFER_BIT)
gl.MatrixMode(gl.PROJECTION)
gl.Viewport(0, 0, 300, 300)
gl.LoadIdentity()
drawPaddle(paddle.x, paddle.y)
gl.End()
iup.GLSwapBuffers(self)
end
window = iup.dialog{canvas, title = "Soon to be Pong"}
function canvas:k_any(c)
if c == iup.K_q then
return iup.CLOSE
elseif c == iup.K_w then
paddle.y = paddle.y + 0.02
return iup.CONTINUE
else
return iup.DEFAULT
end
end
window:show()
iup.MainLoop()
It's my understanding that canvas:k_any() gets called when a key is pressed, but it's not responding, even to the quit command. Any ideas?
Here is at least part of the problem for the sample you've quoted. Before the IUP keyname constants are actually defined in your iup table, you need to call a function. The idea is that the memory footprint is lower if you don't need all of the key names, which many applications can do entirely without.
Try adding the following lines sometime shortly after require "iuplua" and before you use the names in any code:
iup.key_open() -- load key names
This is documented near the top of the topic Keyboard Codes as well as in a variety of places related to the keyboard.
Without the call to iup.key_open(), iup.K_q evaluates to nil (as does any other key name) and thus silently never matches any keycode passed to canvas:k_any().
Edit: I just ran your sample, and indeed calling iup.key_open() causes the q and w keys to be recognized. You have a further glitch you will notice next that just updating the paddle's position doesn't ask for the canvas to redraw. I'll leave that as an exercise as you go forward.
Edit2: Adding the line iup.Update(self) to the function canvas:k_any() just after you modify the state of the paddle appears to do what you want. Doing it that way rather than manually calling the action() method has the advantage of interacting with IUP's message loop handling in the expected way. In the general case, calling iup.UpdateChildren(window) instead might be preferred. That would keep any other IUP controls that are displayed in the dialog updated as well as the canvas, which can be useful if you have controls that don't do all their own updating internally.
Alternatively, you could move the drawing routines into their own function called from both the k_any() and action() methods of the canvas. I'm not sure I'd recommend that in general as it doesn't scale as well when you want to support multiple viewports or related controls.
You might want to rearrange the logic in your k_any() handler so that you are less likely to accidentally forget the call to iup.Update() as you add cases for new keys.