Page 2 of 2

Re: Trivia Game Engine

Posted: Fri Sep 18, 2015 2:02 pm
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!

Re: Trivia Game Engine

Posted: Fri Sep 18, 2015 4:38 pm
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.

Re: Trivia Game Engine

Posted: Fri Sep 18, 2015 9:54 pm
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!

Re: Trivia Game Engine

Posted: Sat Sep 19, 2015 7:49 pm
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

Re: Trivia Game Engine

Posted: Sun Sep 20, 2015 1:53 am
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...

Re: Trivia Game Engine

Posted: Sun Sep 20, 2015 3:09 am
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.

Re: Trivia Game Engine

Posted: Sun Sep 20, 2015 10:32 am
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.

Re: Trivia Game Engine

Posted: Sun Sep 20, 2015 5:49 pm
by Karai17
In the random number part, it should be questions[love.math.random(1, #questions)]

Re: Trivia Game Engine

Posted: Sun Sep 20, 2015 10:47 pm
by LeNitrous
How do I say this... Thanks alot? is that it? Its not enough but still, thanks! Its finally solved.

Re: [SOLVED] Trivia Game Engine

Posted: Sun Sep 20, 2015 11:15 pm
by Karai17
:) <3