Jump to content

Function to increment a variable +1 (Only one time)


javilo99

Recommended Posts

Hi guys, right now I'm creating another Wherigo cartridge (this is so amusing and super adictive) and  I'm here again, to ask you about how I programme this:

 

I'm making a Wherigo with rooms, each room has a different riddle and I want to make some stats for the end of the game.

 

One of the things I want to sum up is the amount of riddles the player has solved (only one count for each riddle, for example if the player solve the first riddle 50 times, just the total of riddles will be one)

 

The problem is that if you fail, you may die and you have to restart the game and enter the rooms that you have solved previously. Also, I have different paths to progress in the game, so it's difficult to control and count the riddles only one by time...

 

 

 

I was trying to make a function called "riddles" and to programme if the player has enter the room and solve the riddle once, just to count as one... It's difficult since I'm making a lot of rooms and paths to get to the end...

 

The only way I see I can do it is to programme one variable for each room, so when the player solve a riddle, the room gets to 1 and the amount of riddles only count as 1 and so on...

This is a boring way because you have to create one variable for each room and I'm sure that it has to be a better chance to do this in a better way...maybe with some "restrictions" on adding one to the variable "riddles" just only the first time you solve the riddle...

 

Hope you can help me with this thing...

 

Regards,

 

Javilo99

Link to comment

It depends if you wanted to create some custom code or stay entirely within a builder application.

 

If you wanted to stay within a builder application, you'd need those separate variables: one for if a riddle was asked, one for if it was answered correctly.  If a game over does not allow riddles to be asked again, this would work.  If a game over does not allow riddles to be asked again and the player doesn't get points for correctly-answered riddles, your game over code would set all correct answer variables to false and leave the asked variable alone.  So on and so forth.  This would also persist if the cartridge was restored.

 

If you want author script, look at my Cacher Pursuit cartridge.  In it, I have a lot of questions I ask at different places.  I have everything about a question contained in an object in an array (lua table, but I'll use different terminology here), including whether the question was asked before.  If you have an object for each riddle, you can store all sorts of information in it: the type of riddle so rooms can ask riddles of different types, whether the riddle has been asked before, which room asked the riddle, and so on.  At the end of the cartridge, you could iterate through this array to sum the total number of correctly-answered riddles.  In my cartridge, I created a function that retrieves the question to be asked.  If no question is retrieved, that means game over.

 

That's about all the time I have time to write at the moment.

Link to comment
1 hour ago, Ranger Fox said:

 

If you want author script, look at my Cacher Pursuit cartridge.  In it, I have a lot of questions I ask at different places.  I have everything about a question contained in an object in an array (lua table, but I'll use different terminology here), including whether the question was asked before.  If you have an object for each riddle, you can store all sorts of information in it: the type of riddle so rooms can ask riddles of different types, whether the riddle has been asked before, which room asked the riddle, and so on.  At the end of the cartridge, you could iterate through this array to sum the total number of correctly-answered riddles.  In my cartridge, I created a function that retrieves the question to be asked.  If no question is retrieved, that means game over.

 

That's about all the time I have time to write at the moment.

I think I have not understand nothing at all:anicute::anicute::anicute:

 

1 hour ago, Ranger Fox said:

Your game over code would set all correct answer variables to false and leave the asked variable alone.  So on and so forth.  This would also persist if the cartridge was restored.

But yeah, that's what I don't want, because I would have to create a lot of variables and then set them as 1 if the player answer the riddle right...

 

So maybe, to create a complex function I have to use .lua code and so on? (I have to comment that I have no idea about .lua code and all that stuff...)

Link to comment
3 hours ago, javilo99 said:

I think I have not understand nothing at all:anicute::anicute::anicute:

 

This is how I interpret Ranger Fox's words. I've commented this sample code to the best of my ability - hopefully you understand what I'm getting at.

-- Create an array that stores the player's state.
-- The first value in the list corresponds to the the first question, the second value to the second question, etc. A
-- falsy value means "not answered." Anything else means "correctly answered."
answers = {0, 0, 0, 0, 0}

-- Every time the player answers a question correctly, this function is called. It increments the correct value in the 
-- array. You could also just set the value in the array to some truthy constant, but this allows you to track the 
-- number of guesses the player took at each stop.
function correct(q) do
    -- Wherigo.MessageBox( "Congratulations, you entered the correct answer!" )
    answers[q] = answers[q] + 1
end
  
-- At the **end** of the game, we count the values in the array with a truthy value.
total_correct = 0
total_guesses = 0
for k, v in pairs(answers) do
    if (v > 0) then
        total_correct = total_correct + 1
    end
    
    total_guesses = total_guesses + v
end
  
-- Now, total_correct contains the number of questions that the player answered correctly. total_guesses contains 
-- the number of guesses that the player made, across all questions.

-- You can use these values to dynamically present success/game over screens.
  
if (total_correct > 4) then
    -- Wherigo.MessageBox( "Congratulations, you win. You had a total of " .. total_guesses .. " guesses." )
else
    -- Wherigo.MessageBox( "Sorry, not enough questions answered" )
end

 

Edited by Hügh
Link to comment

Depending on the language (sorry, I've no idea - never coded one) you could also start with an empty array and then add values to it each time a section was done correctly.

 

Then you just have to look at the length of the array - if you added 5 values to it then each bit is done.

 

Which might be a little more efficient, depending on the language and whether it allows you to have arrays which change length...

Link to comment
56 minutes ago, Hügh said:

 

This is how I interpret Ranger Fox's words. I've commented this sample code to the best of my ability - hopefully you understand what I'm getting at.


-- Create an array that stores the player's state.
-- The first value in the list corresponds to the the first question, the second value to the second question, etc. A
-- falsy value means "not answered." Anything else means "correctly answered."
answers = {0, 0, 0, 0, 0}

-- Every time the player answers a question correctly, this function is called. It increments the correct value in the 
-- array. You could also just set the value in the array to some truthy constant, but this allows you to track the 
-- number of guesses the player took at each stop.
function correct(q) do
    -- Wherigo.MessageBox( "Congratulations, you entered the correct answer!" )
    answers[q] = answers[q] + 1
end
  
-- At the **end** of the game, we count the values in the array with a truthy value.
total_correct = 0
total_guesses = 0
for k, v in pairs(answers) do
    if (v > 0) then
        total_correct = total_correct + 1
    end
    
    total_guesses = total_guesses + v
end
  
-- Now, total_correct contains the number of questions that the player answered correctly. total_guesses contains 
-- the number of guesses that the player made, across all questions.

-- You can use these values to dynamically present success/game over screens.
  
if (total_correct > 4) then
    -- Wherigo.MessageBox( "Congratulations, you win. You had a total of " .. total_guesses .. " guesses." )
else
    -- Wherigo.MessageBox( "Sorry, not enough questions answered" )
end

 

Well, I understand it more or less...

 

But how can I do this on Urwigo?

 

I only know how to work with the boxes, not as commands...

 

Also, this function covers if the player answer a riddle, it counts only one time? Cuz this is what I want to do... 

Link to comment
43 minutes ago, Blue Square Thing said:

Depending on the language (sorry, I've no idea - never coded one) you could also start with an empty array and then add values to it each time a section was done correctly.

 

Then you just have to look at the length of the array - if you added 5 values to it then each bit is done.

 

Which might be a little more efficient, depending on the language and whether it allows you to have arrays which change length...

How can I do this?

 

What I want to do is to count the solved riddle only as one, so if the player solve the riddle five times, it just count as one riddle solved...

 

This might be accurate if you change the value of each riddle to 1 when the player solve the riddle, at the end, you just have to sum up all "1" to give a message 

in the stats with all the riddles done, but the question is...how?

Link to comment

Yes, OK - that makes sense. I'm not sure it's as easy to use the expanding array method then.

 

Not without a riddleSolved = False sort of variable - and then you're back to having five variables for each section of the thing I guess.

 

Sorry - I program, but I've never come close to a Wherigo in any shape or form and just happened across this in the recent posts bit :-)

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...