Scripts / Help With Question/quiz script - Not Working?

Author
Message
Rector
4
Years of Service
User Offline
Joined: 11th May 2019
Location: Manchester
Posted: 30th Nov 2023 23:51
Hi
This is my first attempt at scripting, but I'm have issues getting this to work.

Basically, I want the player to answer some questions where they get 10 points for a right answer and -10 for a wrong answer. But I just can't get it to work, because scripting isn't my thing and like I said this is my first attempt.

See Script Below.

-- Quiz Script

-- Initialize score
score = 0

-- Questions and Answers
questions = {
{
question = "What is the capital of France?",
answers = {
"Berlin",
"London",
"Paris" -- Correct answer
}
},
{
question = "Which planet is known as the Red Planet?",
answers = {
"Earth",
"Mars", -- Correct answer
"Jupiter"
}
},
-- Add more questions as needed
}

-- Current question index
currentQuestionIndex = 1

-- Function to display the current question and answers
function DisplayQuestion()
-- Display the question
Print("Question: " .. questions[currentQuestionIndex].question)

-- Display answer options
for i, answer in ipairs(questions[currentQuestionIndex].answers) do
Print(i .. ". " .. answer)
end
end

-- Function to check the player's answer
function CheckAnswer(selectedAnswer)
local correctAnswerIndex = 0

-- Find the index of the correct answer
for i, answer in ipairs(questions[currentQuestionIndex].answers) do
if answer == questions[currentQuestionIndex].answers[1] then
correctAnswerIndex = i
end
end

-- Check if the selected answer is correct
if selectedAnswer == correctAnswerIndex then
Print("Correct!")
score = score + 10
else
Print("Wrong!")
score = score - 10
end

-- Move to the next question
currentQuestionIndex = currentQuestionIndex + 1

-- Check if there are more questions
if currentQuestionIndex <= #questions then
DisplayQuestion()
else
Print("Quiz completed! Your final score is: " .. score)
end
end

-- Display the first question when the game starts
DisplayQuestion()
Processor i9-9900k CPU@ 3.60GHZ
Ram 32GB
Windows 10 64bit
GeForce RTX 2060
PM
Rector
4
Years of Service
User Offline
Joined: 11th May 2019
Location: Manchester
Posted: 2nd Dec 2023 13:43
Hello? Anyone?
Processor i9-9900k CPU@ 3.60GHZ
Ram 32GB
Windows 10 64bit
GeForce RTX 2060
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 2nd Dec 2023 16:48
How are you actually executing this?
Been there, done that, got all the T-Shirts!
PM
mikeven
12
Years of Service
User Offline
Joined: 31st Dec 2011
Location:
Posted: 3rd Dec 2023 18:25 Edited at: 3rd Dec 2023 18:30
Hello,

I spent my day on a quiz compatible with Game Guru MAX.
(problem : I don't remember how I can attach the code in this comment. The Selection of 'Code' in the editor doesn't work in Microsoft Edge.)

Quote: "

-- DESCRIPTION: 'quiz' is a script suggested by Rector, a member of Game-Guru Forum
-- This script is created by mikeven ( alias marcuswilm on GitHub ) on 2023-Dec-03. It is tested with GameGuru MAX Build 2023.10.27 ( STEAM release )

--**********************************************************************************************************************************************************
--*********** Init Function **************
--**********************************************************************************************************************************************************

function quiz_init ( )

quiz_print_question = "" -- to format the text "question + choices of answers"
quiz_score = 0 -- initialize score
quiz_question_index = 0 -- current question index
quiz_how_many_questions = 5 -- number of questions


-- Questions and Answers of the quiz

quiz_questions = {}
quiz_questions [1] = " What is the capital of France ? "
quiz_questions [2] = " What is the capital of Austria ? "
quiz_questions [3] = " What is the capital of Italy ? "
quiz_questions [4] = " What is the capital of Hungary ? "
quiz_questions [5] = " Which planet is known as the Red Planet ? "

quiz_correct_answers = {}
quiz_correct_answers [1] = " Paris "
quiz_correct_answers [2] = " Vienna "
quiz_correct_answers [3] = " Rome "
quiz_correct_answers [4] = " Budapest "
quiz_correct_answers [5] = " Mars "

quiz_1st_wrong_answers = {}
quiz_1st_wrong_answers [1] = " Berlin "
quiz_1st_wrong_answers [2] = " Brussels "
quiz_1st_wrong_answers [3] = " Milan "
quiz_1st_wrong_answers [4] = " Bucharest "
quiz_1st_wrong_answers [5] = " Earth "

quiz_2nd_wrong_answers = {}
quiz_2nd_wrong_answers [1] = " London "
quiz_2nd_wrong_answers [2] = " Berlin "
quiz_2nd_wrong_answers [3] = " Naples "
quiz_2nd_wrong_answers [4] = " Kiev "
quiz_2nd_wrong_answers [5] = " Jupiter "

quiz_position_corr_answ = {}
quiz_position_corr_answ [1] = 2
quiz_position_corr_answ [2] = 3
quiz_position_corr_answ [3] = 1
quiz_position_corr_answ [4] = 2
quiz_position_corr_answ [5] = 2

quiz_flag_answered = -1 -- the value of this flag ( -1, 0, 1) combined with the value of the g_Scancode defines which message is shown

quiz_message_correct_answer = " Your answer is correct, you win one point "
quiz_message_wrong_answer = " Your answer is wrong "
quiz_message_final_score = ""
quiz_last_message_shown = " Welcome to this quiz. Use keys (1)(2)(3) for answers and (+) for questions "

-- initialization of the GG Max global variable used to store the value of a pressed key
g_Scancode = 0

-- variables relative to the numeric keys (1) (2) (3)
-- these variables are used to control the repetition of those keys when they are kept pressed ( the delay is adjustable )

-- key79 = numpad '1'
time_01_key79 = os.clock()
time_02_key79 = 0
time_elapsed_key79 = 0
time_delay_key79 = 0.020 -- seconds ( adjustable )
flag_key79 = 0

-- key80 = numpad '2'
time_01_key80 = os.clock()
time_02_key80 = 0
time_elapsed_key80 = 0
time_delay_key80 = 0.020 -- seconds ( adjustable )
flag_key80 = 0

-- key81 = numpad '3'
time_01_key81 = os.clock()
time_02_key81 = 0
time_elapsed_key81 = 0
time_delay_key81 = 0.020 -- seconds ( adjustable )
flag_key81 = 0

-- variable to control the key (+) of the numeric keypad

-- key78 = numpad '+'
time_01_key78 = os.clock()
time_02_key78 = 0
time_elapsed_key78 = 0
time_delay_key78 = 0.500 -- seconds ( adjustable )
flag_key78 = 0

end

--**********************************************************************************************************************************************************
--*********** Main Function **************
--**********************************************************************************************************************************************************

function quiz_main ( )


-- settings of the variables relative to the numeric keys (1) (2) (3)

time_02_key79 = os.clock() -- key79 = numpad '1'

time_elapsed_key79 = time_02_key79 - time_01_key79

if time_elapsed_key79 > time_delay_key79 then

time_01_key79 = os.clock()

if g_Scancode == 79 then
flag_key79 = 1
end

end

time_02_key80 = os.clock() -- key80 = numpad '2'

time_elapsed_key80 = time_02_key80 - time_01_key80

if time_elapsed_key80 > time_delay_key80 then

time_01_key80 = os.clock()

if g_Scancode == 80 then
flag_key80 = 1
end

end

time_02_key81 = os.clock() -- key81 = numpad '3'

time_elapsed_key81 = time_02_key81 - time_01_key81

if time_elapsed_key81 > time_delay_key81 then

time_01_key81 = os.clock()

if g_Scancode == 81 then
flag_key81 = 1
end

end

-- setting of the variables relative to the key (+) (numeric keypad)

time_02_key78 = os.clock() -- key78 = numpad '+'

time_elapsed_key78 = time_02_key78 - time_01_key78

if time_elapsed_key78 > time_delay_key78 then

time_01_key78 = os.clock()

if g_Scancode == 78 then
flag_key78 = 1
end

end

-- the question + the choice of three answers are shown in a message at the bottom of the screen

quiz_print_question = quiz_questions [ quiz_question_index ]

if quiz_position_corr_answ [ quiz_question_index ] == 1 then
quiz_print_question = quiz_print_question .. "1 = " .. quiz_correct_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "2 = " .. quiz_1st_wrong_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "3 = " .. quiz_2nd_wrong_answers [ quiz_question_index ]
elseif quiz_position_corr_answ [ quiz_question_index ] == 2 then
quiz_print_question = quiz_print_question .. "1 = " .. quiz_1st_wrong_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "2 = " .. quiz_correct_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "3 = " .. quiz_2nd_wrong_answers [ quiz_question_index ]
elseif quiz_position_corr_answ [ quiz_question_index ] == 3 then
quiz_print_question = quiz_print_question .. "1 = " .. quiz_1st_wrong_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "2 = " .. quiz_2nd_wrong_answers [ quiz_question_index ] .. " / "
quiz_print_question = quiz_print_question .. "3 = " .. quiz_correct_answers [ quiz_question_index ]
end


if g_Scancode ~= 78 and quiz_flag_answered == 0 then

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_print_question
)
quiz_last_message_shown = quiz_print_question

end

----------------------------------------------------------------------------------------------------------------------------

-- detection of the key pressed by the user
-- if the key is (1) (2) (3) then the decision " correct or wrong answer " is shown in a message at the bottom of the screen

if flag_key79 == 1 then
flag_key79 = 0

if g_Scancode == 79 and quiz_flag_answered == 0 then -- numpad '1' is pressed
if quiz_position_corr_answ [ quiz_question_index ] == 1 then

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_correct_answer
)
quiz_score = quiz_score + 1
quiz_last_message_shown = quiz_message_correct_answer

else

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_wrong_answer
)
quiz_last_message_shown = quiz_message_wrong_answer

end

quiz_flag_answered = 1

end

end

if flag_key80 == 1 then
flag_key80 = 0

if g_Scancode == 80 and quiz_flag_answered == 0 then -- numpad '2' is pressed
if quiz_position_corr_answ [ quiz_question_index ] == 2 then

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_correct_answer
)
quiz_score = quiz_score + 1
quiz_last_message_shown = quiz_message_correct_answer

else

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_wrong_answer
)
quiz_last_message_shown = quiz_message_wrong_answer

end

quiz_flag_answered = 1

end

end

if flag_key81 == 1 then
flag_key81 = 0

if g_Scancode == 81 and quiz_flag_answered == 0 then -- numpad '3' is pressed
if quiz_position_corr_answ [ quiz_question_index ] == 3 then

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_correct_answer
)
quiz_score = quiz_score + 1
quiz_last_message_shown = quiz_message_correct_answer

else

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_wrong_answer
)
quiz_last_message_shown = quiz_message_wrong_answer

end

quiz_flag_answered = 1

end

end


-- waiting for the key (+) (numeric keypad) being pressed to show the next question

if flag_key78 == 1 then
flag_key78 = 0

if g_Scancode == 78 and ( quiz_flag_answered == 1 or quiz_flag_answered == -1 ) then -- numpad '+' is pressed

quiz_question_index = quiz_question_index + 1
if quiz_question_index <= quiz_how_many_questions then

quiz_flag_answered = 0
else
quiz_flag_answered = 1

quiz_message_final_score = " quiz completed ! Your final score is " .. string.format ( "%02d" , quiz_score )
quiz_last_message_shown = quiz_message_final_score

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_message_final_score
)
end

end

end

-- if all flag_key.. are set to zero then the last message shown stays at the bottom of the screen

if flag_key78 == 0 and flag_key79 == 0 and flag_key80 == 0 and flag_key81 == 0 then

SendMessageI ( "prompttextsize" , 4 )
SendMessageS ( "prompt" ,
quiz_last_message_shown
)

end


end

"
PM
mikeven
12
Years of Service
User Offline
Joined: 31st Dec 2011
Location:
Posted: 3rd Dec 2023 18:53
1. Copy the 'quoted script' (don't include the " placed by the editor at the start and at the end of the script).
2. Save it as 'quiz.lua' somewhere in the subfolder 'scriptbank\user' of your main user files of GameGuru MAX (optionally defined as the 'writable folder location' if modified in the 'Advanced Settings' of the Editor.
3. You could dedicate a .png icon to identify that script but it is not absolutely necessary because the name of that script will appear in the selection of the behaviors thanks to its description.
4. start a new project with a new level (an empty landscape is good enough to test that script) .
5. add an asset of your choice having these properties :
physics off + always active + isimmobile
6. select that script 'quiz.lua' and attach it as behavior to that asset.
7. save your level and test it.



PM

Login to post a reply

Server time is: 2024-05-03 18:14:26
Your offset time is: 2024-05-03 18:14:26