How do I build a switch/lever based basic puzzle

I wish to incorporate some puzzle mechanics in my game. What would be the best way to set up a simple puzzle switch based system?

I suppose the switches should have a bool indicating if they are turned on or off. Then I’d need a referance to another script to actually do stuff there. However,I’m a bit stumped about what to do next.

Start by giving us a lot more information…or maybe by doing analysis to find out what you really want, if you don’t know the answers to these questions:

  • How do you want your game to work?

  • Is it about the sequence?

  • …the final state?

  • …a combination?

  • …timing?

  • What do you want the effect of completing a puzzle to be?

  • Is it always the same?

  • Does it vary by intensity but not by quality?

  • Is there a qualitative difference?

  • Is there one outcome? Two? More?

  • Is there a way to fail at a puzzle?

  • Does it need to be reset when certain things happen?

  • Does can it have a permanent negative effect?

  • Are there “checkpoints” you can get that limit the effect (if any) of failure?

With the level detail we have, I can tell you this:

  • You need a way to know the state of each switch.
  • You need a way to know (at least) when the state is changed, if not how it changed.
  • You need a way to determine exit conditions.

Hmm, basically I want to do all the stuff a typical zelda game does with switches.So my answers:

1.a)Yes, the player might need to hit switches in a certain order for certain puzzles
b) not sure what you mean by that.
c)Yes, they might need to hit several switches at once
d)Yes, the players might need to hit several switches in a limited amount of time.

2.a) Several. A door might open, the player might get loot and so on.
b)Not sure what you mean by that.
c)Do you mean that the result will vary depending on how well the player solves the puzzle? Not necessarily.
d)There can be.If one of the puzzle in sequence based then I’d need several outcomes for multiple sequences.

3.a) yes,maybe after a certain time .
b)puzzles are room based and the switches in one room probably won’t affect other rooms
c) I’d like a reset button for difficult puzzles so that the player can reset when stuck

I think you’re mistaking the scope of the game. My game isn’t a puzzle heavy game it has light puzzle elementsand is nothing like antichamber or portal which is fully based on puzzles.

I don’t remember any switches in Zelda but it’s been, like, thirty years so maybe I just forgot them.

1.2 was an alternative to 1.1. So, if the answer to 1.1 is “yes”, then the answer to 1.2 is necessarily “no”.

Anyway, you have a situation where the state of the system changes over time as a response to certain stimuli. That’s a State pattern type of problem.

You also have varying outcomes, possibly multiple things that need to happen when a certain event occurs. That’s an Observer pattern type of problem.

In this case, as often happens in this or any other industry, one pattern dovetails into another. The various terminal states of the State pattern in your problem are what need to cause the events raised in the Observer patterns present in your problem.

You’ll need to figure out exactly how you want it to work and that will govern the exact implementation you use. I can others can make some guesses to start, if absolutely necessary, and provide guidance along the way. In general, my advice would be this: start simple with a puzzle that has just a few switches and simple selection of outcomes: one combination means “unlock” and every other combination means “no change”.

Make sure you decouple the stuff that controls how the puzzle works from that specific puzzle so you can reuse it. Then select one more kind of rule you want to support (maybe one switch toggles another) and create a new puzzle that needs that rule. Extend your puzzle engine to support the new rule while still working for the old puzzle. Repeat this process one rule at a time. If you can do it by creating actual puzzles you want to include in your game, more’s the better.

This is called “emerging a design” and designs that are emerged have tended to be lighter-weight and more resilient to change than designs which are forecast (often called “Big Design Up Front”). This is true for a multitude of reasons that I won’t go into, here. If you don’t want to take my word for it, that’s great. At least humor me and try it for yourself and see if it does what you want it to do.

In addition to making your final design superior to what you would get if you tried to do everything at once, it also makes the work easier. Doing everything all at once can be daunting and demoralizing but even the newest of newbies can usually figure out how to solve a really small problem like making once switch toggle another one.

As you are emerging your design, the process will be even more efficient if you select puzzles that not only stimulate incremental growth of your puzzle engine design but also are puzzles that you want to actually incorporate in your game.

You can also just look for a puzzle engine on the asset store. I’d be shocked if there weren’t at least a few good ones on there… if there aren’t I’d be tempted.