[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
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

[SOLVED] Trivia Game Engine

Post by LeNitrous »

...This is my first post. Please don't hate me...
I'm trying to work on a game similar to Jeopardy. Instead the game starts immediately with a random question is chosen from a table. I'm a bit newbish to Love but I do understand the basics of Lua.

Code: Select all

local question = {}

function question.load()
    q = {
        { id = 1, text = "Is LOVE 2D Cool?", ch1 = "Yes", ch2 = "No", ch3 = "Probably", ch4 = "Indefinitely", correct = ch1 },
        { id = 2, text = "Is Lua easy?", a = "No", b = "Yes", c = "Indefinitely", d = "Probably", correct = b }
        }
end

function question.get( id )
    return q[math.random(#q)]
end

function question.getcorrect( id, correct )
end

function question.draw( text, ch1, ch2, ch3, ch4, correct )
    print(question.get(id)) -- just here for debug
end

return question
This is my question.lua, I'm thinking that I'm doing it incorrectly. Probably do it with metatables? Because when I get the result... I get random variables such as...

Code: Select all

table: 0x0252b768
... in the debug console. Am I doing something wrong? If so, please help!
Last edited by LeNitrous on Sun Sep 20, 2015 10:47 pm, edited 2 times in total.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple Choice Question Engine

Post by ivan »

Hello and welcome to the forums.
You're printing a reference to a table - and the result is showing you the table's address in memory.
Try:

Code: Select all

local randomQ = question.get(id)
print(randomQ.text)
To print all table elements (without recursion) you can do:

Code: Select all

for k,v in pairs(randomQ) do
  print(tostring(k) .. '=>' .. tostring(v))
end
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Multiple Choice Question Engine

Post by LeNitrous »

Thanks alot Ivan! It worked. With one major quirk though. Console is spammed so is the game screen. As if question.get() is getting called in a loop.
This is my question.lua as of now

Code: Select all

local question = {}

function question.load()
    q = {
        { id = 1, text = "Is LOVE 2D Cool?", ch1 = "Yes", ch2 = "No", ch3 = "Probably", ch4 = "Indefinitely", correct = ch1 },
        { id = 2, text = "Is Lua easy?", ch1 = "No", ch2 = "Yes", ch3 = "Indefinitely", ch4 = "Probably", correct = ch2 },
        { id = 3, text = "A more different question", ch1 = "It is?", ch2 = "It isn't?", ch3 = "wazitooya?", ch4 = "OBEY", correct = ch4  },
        { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = ch4  }
        }
end

function question.get()
    return q[math.random(#q)]
end

function question.getcorrect( id, correct )
end

function question.draw( text, ch1, ch2, ch3, ch4, correct )
    local randomQ = question.get()
    love.graphics.print( randomQ.text )
    for k,v in pairs(randomQ) do
        print(tostring(k) .. '=>' .. tostring(v))
    end
end

return question
Added in more elements in table to decrease the chance of it getting picked.
It looks like this...
Image
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Multiple Choice Question Engine

Post by LeNitrous »

Bump...
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple Choice Question Engine

Post by ivan »

Code: Select all

 { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = ch4  }
There is a small bug in the code above, ch4 is nil so it should be "ch4":

Code: Select all

 { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = "ch4"  }
Or better yet, use numeric indexes for the choices:

Code: Select all

{
  -- key part (meta info)
  id = 4,
  text = "Questions differ right?",
  correct = 4,
  -- numeric part (choices)
  [1] = "It doesn't", 
  [2] = "It does", 
  [3] = "Who cares", 
  [4] = "Sure..."  }
}
If you don't want to spam the console you can print each question per 1 line:

Code: Select all

    local text = {}
    for k,v in pairs(randomQ) do
        text[#text + 1] = tostring(k) .. '=>' .. tostring(v)
    end
    print(table.concat(text, ","))
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Multiple Choice Question Engine

Post by LeNitrous »

I made a really sloppy workaround with the for-loop since I think that was the source of the spamming.

Code: Select all

inQuestion = true
in the beginning of my lua file with some if-then-elseif-end statements. If someone can make a better one, that will help me alot!
User avatar
DeltaF1
Citizen
Posts: 64
Joined: Mon Apr 27, 2015 4:12 pm
Location: The Bottom of the Stack
Contact:

Re: Multiple Choice Question Engine

Post by DeltaF1 »

It's also spamming because you're calling it every frame, inside question.draw (which I assume gets called by love.draw)
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: Multiple Choice Question Engine

Post by LeNitrous »

Yet... A bit sloppy since it gets called atleast 10 times in a frame. If someone is willing to fix my code then I would be pleased.
I can't get to display one simple question with this, unfortunately...
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Multiple Choice Question Engine

Post by Karai17 »

I'd like to point out here that pairs is not JIT enabled, but ipairs is. ipairs looks through the sequential (1->n as long as no gaps are in the sequence) part of the table and ignores any named keys. You could replace your ch1, ch2, etc with simply adding them to the table and then loop through the table with ipairs. This will speed up your loops a whole bunch.

Code: Select all

local q = {
   id = 4,
   text = "Questions differ right?",
   correct = 4,

   "It doesn't",
   "It does",
   "Who cares",
   "Sure..."
 }

for i, ch in ipairs(q) do
   print(i, v)
end
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 being a pain in the butt but I decided to change it into a simple trivia game. A question and a textbox.
My current problem is still the same. I added in some changes to the lua file to accomodate my goal.
My problem is that I'm trying to use a table to handle the questions and answers:

Code: Select all

q = {
		{ id = 1, text = "What is the game?", correct = "Untitled" },
		{ id = 2, text = "What language does this game run on?", correct = "Lua" },
		{ id = 3, text = "What framework does this game run on?", correct = "Love 2D" }
        }
The "text" string handles the display of questions while the "correct" string handles the comparing of answers, returning true if it is equal. The "id" integer is just there for something...

Code: Select all

function question.get()
	return q[math.random(#q)]
end
I did this method of getting a random element from the table, the problem is that... It keeps looping

Code: Select all

1
1
1
2
2
2
3
3
3
Spamming this to the console ( note that it is printing print( question.get().id )
Sorry for the wall of text but tl;dr I need help, please read it.
Attachments
game.rar
For some reason the game won't start as a .love file. I got this .rar file instead
(13.13 MiB) Downloaded 154 times
Post Reply

Who is online

Users browsing this forum: No registered users and 198 guests