I'm very new to Lua as well as coding in general. This code is part of a script that runs a timer to check for keypresses. I'm having trouble getting this part of the script to work. The first part works fine - it prints the message and sets the local to 1 - but despite having set the local to 1 when I press N it always returns the "no saved camera position available" message. What am I doing wrong?
local campossaved
--(...other code that doesn't access the local "campossaved"...)
-- save camera position (hotkey B) --wip
if (isKeyPressed(VK_B)) then
print("Saved camera position.")
campossaved = 1
end
-- load camera position (hotkey N) --wip
if (isKeyPressed(VK_N)) then
if (campossaved == 1) then
print("Loaded camera position.")
else
print("No saved camera position available.")
end
end
you say it's run in a timer loop. I'm guessing it's a scope issue, and campossaved is getting reinitialized as nil
every time it runs that timer loop, so pressing B only sets it for that 1/60th of a second, or however fast your game loop runs. you'll need to make sure that local campossaved
forward declaration is created before your timer is called
local campossaved
--function timer(delta)
-- save camera position (hotkey B)
if isKeyPressed(VK_B) then
print("Saved camera position.")
campossaved = true
end
-- load camera position (hotkey N)
if isKeyPressed(VK_N) then
if campossaved then
print("Loaded camera position.")
else
print("No saved camera position available.")
end
end
--end