1

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!

11
  • What exactly are you trying to do, at a higher level; what problem are you trying to solve? I assume you have some code already written. Could you perhaps boil it down to one concrete, clear, and concise question? Sharing data between two Lua scripts isn't that difficult. That's what require's for, so I feel there's something more here. Commented Nov 26, 2013 at 1:33
  • Actually... I don't have any code written yet. To be honest, I don't really know what I'm doing. I guess that's why I'm here :P. In essence, what I want to know is: how do I create a variable that is shared between two programs while they are running? Commented Nov 26, 2013 at 3:33
  • That's a bit clearer. It depends on what the programs are. I'm assuming you're implying different games made with Lua using Love2D, since you mentioned different games then separate windows shortly afterward. Is there a reason why they would be entirely separate programs needing to communicate among one another? Maybe it would help you to try out a tutorial or two and write some code first to get a sense of how things work at a less abstract level. Commented Nov 26, 2013 at 4:09
  • I do have some basic knowledge of Lua, Love2D, and previous (although limited) experience with programming, so that's not too much of a problem. My concern is that in-between the GUI closing and a game opening--and vise-versa--there could be some issue with data transferring over. I'm not sure if I'm correct on this or not. I figured a background program/engine of sorts would eliminate any such malfunction. Am I possibly being to cautious? Commented Nov 26, 2013 at 4:45
  • 2
    Why not have it all be one Lua program using Love2D that reuses the same window and process for multiple games? There would then be no data to transfer, because it's all in one place. I apologize if I sound a little pedantic, but quicker than what? You said you had nothing written yet, so I don't believe you have anything to make a comparison against. This almost sounds like a case of premature optimization. Commented Nov 26, 2013 at 14:25

1 Answer 1

1

(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! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.