Using The Crank Like A Safe Dial

I have written some code that basically takes the crank and treats it like the user is spinning a safe dial. Imagine that the user is at a safe and the crank represents the face of the dial, a circular dial that you can spin that has numbers on it. The user can spin the dial to a particular number, then interact with the safe to enter that number. I feel like what I have should work, but it does not because the variable called openSafe does not update and I cannot figure out why. Here is what I have right now, I usually use openSafe++ instead of openSafe+=1 (openSafe starts at 0):

on interact do

crankAngle = floor event.aa

// safe combination is 325, 75, 240

// start safe combination piece
if openSafe==0 then
	
	if crankAngle==325 then
		
		sound "allowedEntry"
		openSafe+=1
		say "{openSafe}"
		
	else
		
		sound "deniedEntry"
		say "{openSafe}"
		
	end
	
end
// end safe combination piece

// start safe combination piece
if openSafe==1 then
	
	if crankAngle==75 then
		
		sound "allowedEntry"
		openSafe+=1
		say "{openSafe}"
		
	else
		
		sound "deniedEntry"
		openSafe = 0
		say "{openSafe}"
		
	end
	
end
// end safe combination piece

// start safe combination piece
if openSafe==2 then
	
	if crankAngle==240 then
		
		sound "allowedEntry"
		say "Unlocked Safe"
		openSafe+=1
		swap "emptySafe"
		say "{openSafe}"
		
	else
		
		sound "deniedEntry"
		openSafe = 0
		say "{openSafe}"
		
	end
	
end
// end safe combination piece

end

Any ideas on what I need to change? The say "{openSafe}" lines will be removed, that is just me trying to get confirmation that the variable is being updated. It stays stuck at 0. When the user unlocks the safe, an empty safe will replace the functional safe so the user cannot mess up on purpose after they open the safe and access the safe's reward (to be entered later) again.

I'm really new to this forum and to pulp dev in general, but I have a couple of decades of programming experience under my belt. Assuming I'm reading this code correctly, you need to change your three outer if statements to if-else statements. The current logic is written as follows:

  1. Let's say if openSafe==0 returns true. You're now in the inner loop!
  2. Let's say crankAngle==325. Go player! We're in the second inner loop
  3. openSafe+=1 so now the value of openSafe = 1
  4. The else statement does not run, and we are now done with the first if openSafe==0 statement
  5. The code now lands at if openSafe==1. This is TRUE because you just set it to one.
  6. Crank angle is not 75, it was 325, remember? So that inner if is false and we land in else
  7. openSafe is now 0 and we exit the entire outer if statement
  8. openSafe is not 2 so we ignore the third statement.

TLDR try switching the other 2 statements to elseif statements instead of if statements and see if that helps :slight_smile:

(and sorry if I'm totally off base, I just got here :slight_smile: )

1 Like

Without diving into the core logic itself (as @aashay has generously done), I’d also suggest considering a tolerance threshold to determine how precise the user has to be in entering the combination. Most common combo locks only have 35 ticks, which means your imaginary lock requires precision over 10x greater — and perhaps more, since most cheap combo locks usually have tolerances of one or two ticks themselves.

So, consider adding a tolerance, in degrees, within which you consider any given tumbler activated. You could check the difference between the current number and the target and see if it’s less than the tolerance to allow a bit of overshoot, or you could check against its absolute value to be close to it in either direction. That way, you wouldn’t have to be precise to the degree to make it work.

3 Likes

No, I think you are completely correct. I will give this a shot. I have been going back and forth with how to write this puzzle so it is not confusing, there's an NPC near the safe that tells the player what their current crank angle is. I also had to look at fractions of a degree where the degree might be 325.5 and essentially return false when it should be true but I missed what you are highlighting here. I'll go through and make these changes, I bet that will help. I am also new to Pulpscript so I am sort of making this up as I go along, it is a lot more adaptable than I thought it would be.

Just tested it, works great! Thanks again.

2 Likes