简体   繁体   中英

How to share data between two Lua scripts

I'm not even sure if my plan for doing things is the best way, so I apologize if this post is a bit vague. Also, I understand that similar questions have been asked before. However, I haven't been able to find anything that pertained to my situation and that made sense to me.

So me and my friends from school are building arcade machine, and I'm planning on putting together the main GUI that allows the user to select different games and load them if they have enough tokens. However, these separate windows will have to share some variables, mainly the number of tokens in the machine. I figured a separate Lua program could store such variable, and also have requests sent to it to perform other functions like opening and closing the different windows. Also, in case it's important to note, we will be using the Love2D engine for the games and be running all this on a Linux machine.

By what I've read, there seems to be some C and C++ code involved in this. I know next to nothing about C or C++, and we're trying to get this project moving along, so if you could include some code in your answer and instruct me on how to use it, that'd would be amazing. I can come back later and learn some C or C++, but right now Lua is my top priority.

My questions:

  1. Is there a better way to accomplish what I'm trying to do?
  2. How should I go about doing this?
  3. Can this be done solely with Lua, or is some C, C++, or any other external programming language/utility/etc. required?

Also, incase anyone brings it up, I have tried using global variables, but I couldn't seem to get two programs/scripts to use the same variable at once.

Again, sorry if I'm being a bit vague.

Thanks in advance!

(this method is a combination of @Puzzlem00n's suggestion and reading the comments, so I shouldn't take much credit from this at all)

Putting multiple games in one lua program!

In main.lua:

require("spaceInvaders") --require all the other Lua files
-- without the ".lua" at the end!

gamestate=spaceInvaders

function love.load()
--all the setup things
end

function love.update()
 gamestate.update()
end

function love.draw()
 gamestate.draw()
end

function love.keypressed(key)
 gamestate.keypressed(key)
end

--and so on for the rest of the love callbacks that you need (such as love.keyreleased())

Then in each game (which is a separate Lua file such as spaceInvaders.lua):

spaceInvaders = {}

function spaceInvaders.draw()
 --your draw code
end

function spaceInvaders.update()
 --your update code
end

--and so on for every other callback you want to use

What this code does is it gives each game its own set of love functions. When you want to play that game, you should set gamestate to that game's name. The spaceInvaders={} line defines spaceInvaders as a table, which stores each function in place. When you define a variable as an existing variable, you are actually creating a reference to it, for example:

t = {}
table.insert(t,1) --place 1 inside t
table.insert(t,3) --also place 3 inside t
s = t
print(s[1]) --will print 1, the first value of t
t[1]=2
print(s[1]) --will print 2, as it refers to t[1], which has just been changed

Sharing variables! (I worked out this bit!)

NOW, this means you can send variables around the program with another function. If you want to share score between games, you could do the following:

in main.lua:

function love.update()
 gamestate.update()
 score = gamestate.returnScore() --score will always equal to the current score being returned by the game
end

in a game, such as spaceInvaders.lua:

function spaceInvaders.returnScore()
 return score --or whatever the score is stored in
end

This will allow you to get a variable, such as score, from one game to main.lua! I'm sorry if this is a little confusing but hopefully this is what you're looking for! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM