[SOLVED] Trivia Game Engine

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
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Trivia Game Engine

Post by msilvestro »

My two cents here.

In my opinion, the problem is that you call that function inside the love.draw. You should not, since the love.draw. function is called by the main game loop a lot of times every second, so it's normal that is "spams" numbers like this. So, you should select a random question in the love.load function using that function and then, after a specific event happens (for example, the user enters the right question) you lode another question.

Another little advice, it's very important (in not fundamental) to be quite fluent in Lua to programme in Love2D. But, apart from that, I suggest you to implement the game in a command line environment in pure Lua. Then, when it's ok, you start to implement the GUI over that script in Love2D. At least, that's what I do.

Good luck!
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Trivia Game Engine

Post by Karai17 »

It looks like you are calling that function in either update or draw, which happens every frame. You need to only call the function after some condition is met and then set a flag so it isn't called again.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Trivia Game Engine

Post by LeNitrous »

msilvestro wrote:My two cents here.

In my opinion, the problem is that you call that function inside the love.draw. You should not, since the love.draw. function is called by the main game loop a lot of times every second, so it's normal that is "spams" numbers like this. So, you should select a random question in the love.load function using that function and then, after a specific event happens (for example, the user enters the right question) you lode another question.

Another little advice, it's very important (in not fundamental) to be quite fluent in Lua to programme in Love2D. But, apart from that, I suggest you to implement the game in a command line environment in pure Lua. Then, when it's ok, you start to implement the GUI over that script in Love2D. At least, that's what I do.

Good luck!
Thanks for the tip! I placed the function in [wiki]love.load[/wiki] and it looks like I can go through that since delta time is really fast. Its being called 3 times but it looks fine. Thanks for the tip too!
Karai17 wrote:It looks like you are calling that function in either update or draw, which happens every frame. You need to only call the function after some condition is met and then set a flag so it isn't called again.
Thanks for the help! It is a bit flawed but works as it should.

[EDIT]

I know that [wiki]love.graphics[/wiki] should be called in [wiki]love.draw[/wiki] right? Would someone help me make a workaround to solve my problem? I want to run some draw code in [wiki]love.load[/wiki] since thats the only way it works. Thanks!
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Trivia Game Engine

Post by Karai17 »

You need to access the questions table only once and store the results somewhere that you can access every frame. When a question is answered you need to access the questions table again, rinse and repeat
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Trivia Game Engine

Post by LeNitrous »

Sounds a bit too complicated for me as a rookie in Lua. Can you give example code to clear this out a bit? Sorry...
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Trivia Game Engine

Post by Karai17 »

Code: Select all

function love.load()
  questions = {
    { text="Question 1", answer="Answer" },
    { text="Question 2", answer="Answer" },
    { text="Question 3", answer="Answer" },
    { text="Question 4", answer="Answer" },
    { text="Question 5", answer="Answer" }
  }

  current_question = false
end

function love.update(dt)
  if not current_question then
    current_question = love.math.random(1, #questions)
  end
end

function love.draw()
  if current_question then
    love.graphics.print(current_question.text, 50, 50)
  end
end
The above will create a flag (current_question) and also uses it as a lookup for your drawing and checking the answer. You can use love.textinput to write an answer to another variable and use love.keypressed for when someone pressed return to check the answer.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Trivia Game Engine

Post by LeNitrous »

Sorry for letting you spoonfeed me, but it returns an error.
attempt to index global 'current_question' (a number value)
I'm very desprate and this is due tomorrow.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Trivia Game Engine

Post by Karai17 »

In the random number part, it should be questions[love.math.random(1, #questions)]
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Trivia Game Engine

Post by LeNitrous »

How do I say this... Thanks alot? is that it? Its not enough but still, thanks! Its finally solved.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: [SOLVED] Trivia Game Engine

Post by Karai17 »

:) <3
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests