Page 1 of 1
Access variables in different files
Posted: Fri Jun 19, 2020 1:08 am
by soulgirl2020
I am trying to create a basic zelda game and so far I have the main, player, util, animation, enemy1, and a struggling physics engine. I'm trying to use a global variable from main (tileWidth) and use it in player. It says that it is nill, and I've tried putting it in love.load and above it at the top but it still returns nill. I have all the files required in main as well. Also, how would I go about using player.x, player.y, etc in another file? My biggest issue is connecting the files.
Re: Access variables in different files
Posted: Sat Jun 20, 2020 11:30 am
by 4vZEROv
When you write require('file') you are essentially pasting all the content of the file at the place of the require line.
Also you can see here :
https://love2d.org/wiki/love.run where the love.load() function is really called inside the love2d game loop.
If you want to use a global variable in the file 'fileexemple.lua'
--main.lua
Code: Select all
my_global_variable = 100
require('fileexemple')
--fileexemple.lua
Your player file look like a class.
If you want to use it, you need to instantiate it.
Code: Select all
global_var = 100
require('player')
local my_player = player:init()
Re: Access variables in different files
Posted: Sat Jun 20, 2020 5:55 pm
by soulgirl2020
OMG! Thank you so much, it works now. I did not instantiate the class file in main.lua.