Can't call table values??

General discussion about LÖVE, Lua, game development, puns, and unicorns.
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Can't call table values??

Post by ThatBoiNavy »

I am making a game thats like smash bros mixed with the American revolution but anyway, I am using some libraries and one of them is for a camera that follows the player. I followed this tutorial when setting it up: [youtube]https://www.youtube.com/watch?[/youtube]v=F3zKl70RJlk&list=PLqPLyUreLV8DrLcLvQQ64Uz_h_JGLgGg2&index=7 and I worked while I was on replit. But when I switched to ZeroBrane Studio it came up with this error:

Error: main.lua:9: attempt to call global 'camera' (a table value)
stack traceback:
[love "boot.lua"]:352: in function 'camera'
main.lua:9: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[love "boot.lua"]:368: in function <[love "boot.lua"]:355>
[C]: in function 'xpcall'
Program completed in 10.46 seconds (pid: 5886).

Again this was never an issue before and as far as im aware it only effects the camera

here is my code "british" is the player

oad()

anim8 = require 'libraries/anim8'
camera = require 'libraries/camera'
wf = require 'libraries/windfield'
world = wf.newWorld(0, 5000)
-- Declare open source code above this line /\
love.graphics.setDefaultFilter('nearest', 'nearest') -- Makes the images look crisper and not blurry
cam = camera()
british = {}
british.x = 400
british.y = 200
british.speed = 140
british.collider = world:newBSGRectangleCollider(british.x, british.y, 50, 39, 5)
british.collider:setFixedRotation(true)

map = love.graphics.newImage("Maps/Plains map.png")

british.spriteSheet = love.graphics.newImage("sprites/British musketeer1-sheet-sheet.png")
british.grid = anim8.newGrid(50, 40, british.spriteSheet:getWidth(), british.spriteSheet:getHeight())
british.animations = {}

british.animations.walkRight = anim8.newAnimation(british.grid('1-10', 1), 0.15)

british.animations.walkLeft = anim8.newAnimation(british.grid('50-40', 1), 0.15)

british.animations.jumpRight = anim8.newAnimation(british.grid('12-16', 1), 0.15)

british.animations.jumpLeft = anim8.newAnimation(british.grid('36-40', 1), 0.15)

british.animations.idleRight = anim8.newAnimation(british.grid('1-1', 1), 0.15)
british.animations.idleLeft = anim8.newAnimation(british.grid('50-50', 1), 0.15)

british.animations.fireRight = anim8.newAnimation(british.grid('17-18', 1), 0.15)

british.anim = british.animations.walkRight

local ground = world:newRectangleCollider(0, 480, 800, 120)
ground:setType('static')
ground.y = 120
end

function love.update(dt)
local ismoving = false
local vx = 0 -- velocity of x
local vy = 0 -- velocity of y

if love.keyboard.isDown("d") then
vx = british.speed
british.anim = british.animations.walkRight
ismoving = true
end
if love.keyboard.isDown("a") then
vx = british.speed * -1 --Turns the velocity negitive to move left
british.anim = british.animations.walkLeft
ismoving = true
end
if love.keyboard.isDown("s") then
vy = british.speed
end
-- if love.keyboard.isDown("w") and canjump == true then
--vy = british.speed * -3 --Turns the velocity negitive to jump up
-- british:applyForce(0, -british.speed * 3)
-- british.anim = british.animations.jumpRight
-- end
if love.keyboard.isDown("space") then
british.anim = british.animations.fireRight
end

british.collider:setLinearVelocity(vx, vy)

if ismoving == false then
british.anim:gotoFrame(1)
end

if british.y < 400 then
canjump = false
elseif british.y > 400 then
canjump = true
end

if canjump == false then
vy = british.speed * 4
end

british.anim:update(dt)
world:update(dt)
british.x = british.collider:getX()
british.y = british.collider:getY()

cam:lookAt(british.x, british.y)

local w = love.graphics.getWidth()
local h = love.graphics.getHeight()

if cam.x < w/2 then
cam.x = w/2
end
if cam.y < h/2 then
cam.y = h/2
end

local mapW = 800
local mapH = 600

if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end

function love.draw()
cam:attach()
love.graphics.draw(map, 0, 0)
british.anim:draw(british.spriteSheet, british.x, british.y, nil, nil, nil, 16.5, 19.5)
world:draw()
cam:detach()
end

Again this was only a problem when I moved from Replit to ZeroBrane Studio...
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Can't call table values??

Post by keharriso »

What camera library are you using? It looks like "camera = require 'libraries/camera'" returns a table, which you try to call on line 9 (just like the error message says).
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Re: Can't call table values??

Post by ThatBoiNavy »

@keharriso Im using the camera.lua file from https://github.com/vrld/hump which is a collection of many love2D libraries. The file is just called camera.lua.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can't call table values??

Post by zorg »

I mean, to answer the question in the topic, no, you generally can't call table values... unless either they are a function, or the __call metamethod is defined on the table... which in this case, it is.

Did you try using require correctly, by using dots as the separator, instead of slashes? does it work then?

Can't really think of anything else it could be to be honest.
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.
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Re: Can't call table values??

Post by ThatBoiNavy »

zorg wrote: Fri Jul 19, 2024 5:21 am I mean, to answer the question in the topic, no, you generally can't call table values... unless either they are a function, or the __call metamethod is defined on the table... which in this case, it is.

Did you try using require correctly, by using dots as the separator, instead of slashes? does it work then?

Can't really think of anything else it could be to be honest.
Using dots still comes up with the same error. Im still perplexed on why this code worked fine on replit but not on ZBS. Is there any other way I can call camera as I would like to be able to have an easy to use camera in my game.
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Can't call table values??

Post by MrFariator »

Since you stated that it worked in replit, but not in ZBS, this might be an issue with your development environment in some manner. Are you sure that the require line is returning the same contents between replit and ZBS?

By all accounts, so long your camera.lua is placed under a folder called "libraries" it should work, based on what the hump github contents detail.

Alternatively, based on the camera.lua's contents, the following should also work:

Code: Select all

local cameraLibrary = 'libraries/camera'
local newCamera = cameraLibrary.new() -- insert parameters as necessary
RNavega
Party member
Posts: 353
Joined: Sun Aug 16, 2020 1:28 pm

Re: Can't call table values??

Post by RNavega »

If it wasn't finding camera.lua it'd say such a thing and then print a list of all path combinations that it searched for. So we know that the module is being loaded.

So I agree with MrFariator in that the code in the local module is just different than the one in Replit (maybe different versions), and must be initialized in different ways.
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Re: Can't call table values??

Post by ThatBoiNavy »

RNavega wrote: Fri Jul 19, 2024 8:48 pm If it wasn't finding camera.lua it'd say such a thing and then print a list of all path combinations that it searched for. So we know that the module is being loaded.

So I agree with MrFariator in that the code in the local module is just different than the one in Replit (maybe different versions), and must be initialized in different ways.
So how would I fix it? I dont think the local module would matter as I haven't used local in the script. ZBS and replit should be the same, its both love2D and It worked in the tutorial in which the person used visual studio (But I used ZBS because I couldn't get VS too work) I switched off of replit as Windfield (a library for physics) didn't work on replit.

Also this problem only happens with camera.lua and not anim8.lua or Windfield.
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Can't call table values??

Post by MrFariator »

Might be beneficial if you just posted your project here, because otherwise we're just guessing. More information is needed.
RNavega
Party member
Posts: 353
Joined: Sun Aug 16, 2020 1:28 pm

Re: Can't call table values??

Post by RNavega »

ThatBoiNavy wrote: Fri Jul 19, 2024 9:09 pm I dont think the local module would matter as I haven't used local in the script.
Oh, I mean "local module" as in the module you have in your hard drive, which seems to be different than the module over in Replit's servers. Not as in "local vs global".

If you can't post the whole project for privacy reasons, maybe just attaching camera.lua so we can see how it's supposed to be initialized. As you've tried, it needs to be something different than what you had in your original script. Maybe the snippet that MrFariator shared.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 0 guests