Basic Quit Function in LOVE2D - lua

I started using LOVE yesterday and I'm trying to code a basic quit function with LUA.
Here's my code
if function love.keyboard.getKey("q")
function love.event.quit()
end
I've tried it with and without the functions.
When I run it, it gives me this error
Error
Syntax error: main.lua:1: '(' expected near 'love'
Traceback
[C]: at 0x7ff9037828f0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

if function love.keyboard.getKey("q")
function love.event.quit()
end
Is invalid Lua syntax.
function is a keyword that is used to define function value. It is not part of the if statement and not used in function calls.
an if statement looks like
if condition then
-- block
end
love.keyboard.getKey("q") is not part of the love2d API.
What you want to do would probably be achieved by implementing a keypressed event handler.
Computer programs are not written by guessing some syntax and then asking for help.
Do a tutorial and read the Lua manual if you want to do anything useful with Lua.

Related

Requiring luasnip to neovim causes errors

I am trying to use luasnip.
The configuration I am using is AstroNvim.
The modifications I made to this configuration are :
require("luasnip.loaders.from_snipmate").lazy_load() at the end of init.lua
Made a directory snippets in the same directory of init.lua, and added some basic snipmate styled snippets inside.
Commands Like :LuaSnipListAvailable works well, and snippets from friendly-snippets works well too.
However the following error message pops up when opening neovim.
Error detected while processing /Users/myusername/.config/nvim/init.lua:
E5113: Error while calling lua chunk: ...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:171: Vim(echomsg):E121: Undefined variable: Error
stack traceback:
[C]: in function 'cmd'
...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:171: in function <...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:16
7>
/Users/myusername/.local/share/nvim/packer_compiled.lua:496: in function </Users/myusername/.local/share/nvim/packer_compiled.lua:485>
[C]: in function 'require'
/Users/myusername/.config/nvim/init.lua:23: in main chunk
I tried other methods of adding snippets, but any sort of require("luasnip") seems to break all.

About Simple Tiled Implementation (sti)

so I'm trying to import a tiled map into my game and gives me this error:
libraries/sti/utils.lua:195: Could not open file ../path/to/my/gamesheet/file. Does not exist.
so if you know the answer, let me know
Additional Errors:
stack traceback:
[love "boot.lua"]:345: in function <[love "boot.lua"]:341>
[C]: in function 'newImageData'
libraries/sti/utils.lua:195: in function 'fix_transparent_color'
libraries/sti/init.lua:106: in function 'init'
libraries/sti/init.lua:48: in function 'sti'
main.lua:7: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[love "boot.lua"]:361: in function <[love "boot.lua"]:348>
[C]: in function 'xpcall'
[Finished in 77.8s]
(I found out hot fix it) Answer:
In the lua file where you exported your tiled project, where it says "image" and it has a directory to the image, replace it with the name of your sprite sheet, but your gonna have to put the sprite sheet in the same directory as the tiled map

'end' expected near '<eof>' for map import for lua file

Error
Syntax error: main.lua:8: 'end' expected (to close 'function' at line 5) near '<eof>'
Traceback
[love "callbacks.lua"]:228: in function 'handler'
[C]: at 0x010558a810
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
I am getting the above error when I am trying to import a game map for my love2D game.
My code is below:
function love.load()
sti = require'lib/sti'
gameMap = sti('Maps/test_map.lua')
end
function love.draw()
gameMap:draw()
end
sti is a library which I am using to get the map into love. I learnt this from a youtube tutorial.
try looking at the function gameMap:draw, you might have forgotten an end on it

Torch 7/Lua cannot require a .so file, but package.loadlib() can

I get a problem when playing Torch 7 code. I have a shared library libhashnn.so file, and I want to load the functions in Torch 7 script, so I use this expression: require 'libhashnn', but the trepl gives an error.
However, if I use package.loadlibfunction, it does work. Here are the results, but I don't know why I can't use require, how can I use require to load lib successfully?
require'libhashnn'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:383: bad argument #1 to '?' (table expected, got string)
stack traceback:
[C]: in function 'error'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:383: in function 'require'
[string "_RESULT={require'libhashnn'}"]:1: in main chunk
[C]: in function 'xpcall'
/home/dazhen/torch/install/share/lua/5.1/trepl/init.lua:650: in function 'repl'
...zhen/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
and
f=package.loadlib('libhashnn.so' ,'luaopen_libhashnn')
[0.0195s]
th> f
function: 0x41630f38
Try calling the function that package.loadlib gives you. I bet you will then get the same error that you get when you use require.
To understand why: require does the equivalent of package.loadlib and then calls that function so that the module that you are loading can initialize itself. With just package.loadlib, this initialization is not done and so the error that occurs during initialization does not show up.
I don't know what hashnn is and so I cannot tell you why it is broken, but looking at /home/dazhen/torch/install/share/lua/5.1/trepl/init.lua line 383 might be a good start to figure out what is going on.

Lua: "Attempt to index a nill value"

Hi so I just installed Lua and I have been playing around with it a bit. When I run a program that is supposed to calculate whether an integer is even or odd it throws an error at me.
Program:
function is_even(n)
if bit32.band(n,1) == 0 then
print('Even')
else
print('Odd')
end
end
This is the error that I receive:
stdin:2: attempt to index a nil value (global 'bit32')
stack traceback:
stdin:2: in function 'is_even'
(...tail calls...)
[C]: in ?
What am i doing wrong here? This program is supposed to work on Lua 5.2+ I currently have Lua 5.3.3 installed.
The bit32 library was deleted from Lua 5.3, because it now supports bitwise operators.

Resources