"Questions that don't deserve their own thread" thread

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: "Questions that don't deserve their own thread" thread

Post by raidho36 » Wed Dec 28, 2016 1:11 pm

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.

plank223
Prole
Posts: 1
Joined: Fri Dec 30, 2016 12:11 pm

Re: "Questions that don't deserve their own thread" thread

Post by plank223 » Fri Dec 30, 2016 12:17 pm

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..

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

Re: "Questions that don't deserve their own thread" thread

Post by nyenye » Fri Dec 30, 2016 1:03 pm

plank223 wrote:...how can I make the window move with my player object...
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:
  • HUMP - Camera: Is one of the tools that come with HUMP. (personally using it)
  • Gamera - This is also famous, but haven't used it.
Then you have to make the camera look at the center of the player each frame.

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!

AdobeWallHacks
Prole
Posts: 1
Joined: Sun Jan 01, 2017 6:28 pm

Re: "Questions that don't deserve their own thread" thread

Post by AdobeWallHacks » Sun Jan 01, 2017 6:40 pm

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

Code: Select all

function love.load()
	local menuFunction = require "menuFile"
	menuFunction.mainFunction()
end
menuFile.lua

Code: Select all

--globals
function mainFunction()
	--code
end
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.

User avatar
zorg
Party member
Posts: 2801
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by zorg » Sun Jan 01, 2017 7:32 pm

AdobeWallHacks wrote:...
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.

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 :3True 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.

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: "Questions that don't deserve their own thread" thread

Post by raidho36 » Sun Jan 01, 2017 9:48 pm

AdobeWallHacks wrote:<...>
You probably missed a few points for the PiL book.
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 )

Substance12
Prole
Posts: 19
Joined: Fri Dec 11, 2015 7:01 pm

Re: "Questions that don't deserve their own thread" thread

Post by Substance12 » Mon Jan 02, 2017 2:19 am

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?

User avatar
xNick1
Party member
Posts: 265
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: "Questions that don't deserve their own thread" thread

Post by xNick1 » Wed Jan 04, 2017 9:57 am

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

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
Currently working on a lucid dreaming adventure called Aarstider: http://bit.ly/2mXmgPI

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: "Questions that don't deserve their own thread" thread

Post by raidho36 » Wed Jan 04, 2017 11:09 am

You need to use the touch module - touch input is not a mouse equivalent.

User avatar
4aiman
Party member
Posts: 248
Joined: Sat Jan 16, 2016 10:30 am

Re: "Questions that don't deserve their own thread" thread

Post by 4aiman » Wed Jan 04, 2017 11:16 am

xNick1 wrote:snip
Mouse events should work on a touchscreen (at least getting cursor position works for me on Android).
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

Locked

Who is online

Users browsing this forum: No registered users and 24 guests