Calling it encapsulation is a big a stretch as calling Lua a compiled language.
If you only have a single such entity, there's no reason to do this - it will not eliminate bugs or make it easier to code, the only effect is that it will run slower, which isn't a very good improvement. Like with many such things, the general rule of thumb is "don't do it if you don't need it", along the lines of "don't fix what's not broken". If you plan on having more than one of those, then using tables/classes is a no brainer.
"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
Hello, I'm a bit new to Love and just finished a nice tutorial http://sheepolution.com/learn.
I have a question, how can I make the window move with my player object. Like in 2d platformers, you kept moving and the background moved with you, if anyone can direct me to some helpful library tool, that would be much appreciated.
Thanks in advance..
I have a question, how can I make the window move with my player object. Like in 2d platformers, you kept moving and the background moved with you, if anyone can direct me to some helpful library tool, that would be much appreciated.
Thanks in advance..
Re: "Questions that don't deserve their own thread" thread
If you mean to have your player always at the center of the screen/window then you have to make use of a Camera. I only know two implementations:plank223 wrote:...how can I make the window move with my player object...
- HUMP - Camera: Is one of the tools that come with HUMP. (personally using it)
- Gamera - This is also famous, but haven't used it.
Take a look at the docs and you will have it done in no time:
https://hump.readthedocs.io/en/latest/camera.html
Good luck!
-
- Prole
- Posts: 1
- Joined: Sun Jan 01, 2017 6:28 pm
Re: "Questions that don't deserve their own thread" thread
I'm fairly new to lua, coming from C++ mainly, and cant get file/function loading to work just right.
dofile() never seemed to work, so I tried require()
require() worked until I attempted to call the main function of the file.
main.lua
menuFile.lua
attempting to run main.lua returns an 'attempt to index local 'menuFunction' (a boolean value) on line 4
if the entirety of line 4 is excluded, main.lua returns no errors but mainFunction inside menuFile.lua is not executed.
This is very likely an elementary problem or my lack of competence, but functioning files would be nice.
dofile() never seemed to work, so I tried require()
require() worked until I attempted to call the main function of the file.
main.lua
Code: Select all
function love.load()
local menuFunction = require "menuFile"
menuFunction.mainFunction()
end
Code: Select all
--globals
function mainFunction()
--code
end
if the entirety of line 4 is excluded, main.lua returns no errors but mainFunction inside menuFile.lua is not executed.
This is very likely an elementary problem or my lack of competence, but functioning files would be nice.
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: "Questions that don't deserve their own thread" thread
You could use love.filesystem.load as it's kinda the löve version of loadfile; dunno about dofile, but require can work as well.AdobeWallHacks wrote:...
Your next issue is that require loads and executes the given file's body, that is, the file scope; then it returns either a boolean (true) on success (and probably false on failure, i never tested that out to be honest), or if you return something as the last line in the file, it will return that. (require can only return one thing, but you can cheat by "wrapping" more stuff into a table with: return {thing1, thing2} for example)
So, you either want to do return mainFunction which is the nice way that doesn't litter _G with globals (hint: declare mainFunction as local too, then) or call the function simply as: mainFunction() in your main.lua since it's loaded as a global, hence you can call it plainly.
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
You probably missed a few points for the PiL book.AdobeWallHacks wrote:<...>
The dofile function simply runs the file, the same way main file is run - it loads the whole thing as a separate "function" instance and runs all commands in it. If it creates globals, they go into global scope, if it creates locals - they stay inside that file. That function runs the file each time you call it. Require, on the other hand, always does this exactly once and caches the output, so that when you require the same file again, it doesn't runs it anymore and instead returns cached result. This is specifically made to load modules and avoid needless reloading. But there is also a very specific pattern to follow to make a module: you create a table and put module's functions and values into it, and return it at the end of file. So when you require something, you get that module table as return value.
Code: Select all
local module = { }
function module.foo ( bar )
return bar
end
return module
Code: Select all
local mymodule = require ( "module" )
local result = mymodule.foo ( fizzbuzz )
-
- Prole
- Posts: 19
- Joined: Fri Dec 11, 2015 7:01 pm
Re: "Questions that don't deserve their own thread" thread
So I'm currently following this tutorial:
https://gamedevelopment.tutsplus.com/tu ... -cms-25587
It's for JS but since it uses GLSL I'm trying to port the shader and code. However the shader is supposed to change values over time by using a buffer texture, but I haven't managed to accomplish that, or even putting a void function into shader code. I can only use the vec4 effect(). I managed to find a workaround using dt, but is there a better way?
https://gamedevelopment.tutsplus.com/tu ... -cms-25587
It's for JS but since it uses GLSL I'm trying to port the shader and code. However the shader is supposed to change values over time by using a buffer texture, but I haven't managed to accomplish that, or even putting a void function into shader code. I can only use the vec4 effect(). I managed to find a workaround using dt, but is there a better way?
Re: "Questions that don't deserve their own thread" thread
I'd like to make a mobile app.
Is the following thing going to work with the touch screen?
I need it to move the map
Is the following thing going to work with the touch screen?
I need it to move the map
Code: Select all
if love.mouse.isDown(1) then
print("mouse is down")
x, y = love.mouse.getPosition()
local center = wWidth / 2;
if x > center then
print("right")
dx = true
sx = false
else
print("left")
sx = true
dx = false
end
end
Re: "Questions that don't deserve their own thread" thread
You need to use the touch module - touch input is not a mouse equivalent.
Re: "Questions that don't deserve their own thread" thread
Mouse events should work on a touchscreen (at least getting cursor position works for me on Android).xNick1 wrote:snip
The problem is, however, that you'd need to process not one but multiple touches.
Use love.touch.getTouches() for that.
Just do smth like this:
Code: Select all
local touch = {x= love.mouse.getX(), y= love.mouse.getY()}
if love.mouse.isDown(1) then -- if you still want mouse support
-- here you should check for any collisions with any on-screen controls' positions
end
-- touchscreen:
self.touches = {}
if love.touch then
local touches = love.touch.getTouches()
for k,v in pairs(touches) do
local touch = {}
touch.x, touch.y = love.touch.getPosition(v)
----------------------------------------------------------
-- what you did to process mouse cursor goes here
----------------------------------------------------------
end
end
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests