PDA

View Full Version : Script to cycle through cameras (or other commands)



quixote
July 11th, 2004, 12:19 PM
Can anyone please tell me what the correct script would be for this attempt that I've made? I want to be able to cycle through 4 cameras. The problem is that I cannot do a mathematical equation on a nil value.


if activecam = nil then
activecam = 1
activecam = activecam + 1

if tonumber (activecam) == 5 then
activecam = 1
end

if tonumber (activecam) == 1 then
TriggerEvent ("Camera1",18)
end

if tonumber (activecam) == 2 then
TriggerEvent ("Camera2",18)
end

if tonumber (activecam) == 3 then
TriggerEvent ("Camera3",18)
end

if tonumber (activecam) == 4 then
TriggerEvent ("Camera4",18)
end

I am quite a noob at lua and even with the tutorials I seem to get confused. :(

mhund
July 11th, 2004, 02:09 PM
Hi,

two things:

1. What kind of variable is activcam? Number or String? One hand you assign integer values, on the other hand you take the tonumber function.

2. Do I get your question right that you need a initial assignment of a value? You could define a separat command which is executed while the start of girder assigning a start value like 1

maybe I get your issue wrong because I am a LUA newbie too.

mhund

quixote
July 11th, 2004, 02:30 PM
Thank you for your response mhund. The variable activecam is an integer from 1 to 4. It's to tell girder which camera to turn on using X10. I have seperate commands called "Camera1" through "Camera4". Each command turns on the appropriate camera, but I want to be able to cycle through them using a remote (channel up and channel down).
with the script:

activecam = activecam + 1

if tonumber (activecam) == 5 then
activecam = 1
end

if tonumber (activecam) == 1 then
TriggerEvent ("Camera1",18)
end

if tonumber (activecam) == 2 then
TriggerEvent ("Camera2",18)
end

if tonumber (activecam) == 3 then
TriggerEvent ("Camera3",18)
end

if tonumber (activecam) == 4 then
TriggerEvent ("Camera4",18)
end



The logger tells me:

6:26:10 PM July 11, 2004 LUA: call main
6:26:10 PM July 11, 2004 LUA: --> call C _ERRORMESSAGE global
6:26:10 PM July 11, 2004 ALERT: error: attempt to perform arithmetic on global `activecam' (a nil value)
6:26:10 PM July 11, 2004 ALERT: stack traceback:
6:26:10 PM July 11, 2004 ALERT: 1: main of (none)
6:26:10 PM July 11, 2004 LUA: return C _ERRORMESSAGE global

mhund
July 11th, 2004, 02:51 PM
Hi,

I see some issues:

Your first If statement has the condition activecam = nil, but it should be activecam == nil. Thats the difference between a logical operator and an assigenment.

But in your situation I would abandon the LUA skript and use a multigroup, containing the initiating eventstring and four commands - one for each camera - applying the state function of girder (state count 4, begin state depending on the cam number). Thats what the states are for.

mhund

quixote
July 11th, 2004, 03:05 PM
well, I guess that would be the easier way, but if I use states then I cannot use the + and - channel buttons. :-?
I want to be able to pass a camera and then immediately go back to it without cycling through all of the others. I have removed most of the first part of the script. the problem is that I cannot perform a mathematical equation on a nil value, but if I immediately assign a value to it, then the whole thing is messed up and useless.

Promixis
July 11th, 2004, 03:54 PM
activecam = activecam or 0
activecam = activecam + 1

if activecam == 5 then
activecam = 1
end

if activecam == 1 then
TriggerEvent ("Camera1",18)
end

if activecam == 2 then
TriggerEvent ("Camera2",18)
end

if activecam == 3 then
TriggerEvent ("Camera3",18)
end

if activecam == 4 then
TriggerEvent ("Camera4",18)
end

Promixis
July 11th, 2004, 03:55 PM
OR....


activecam = activecam or 0
activecam = activecam + 1

if activecam == 5 then
activecam = 1
end

TriggerEvent ("Camera"..activecam,18)

quixote
July 11th, 2004, 04:08 PM
Cool, ok, I see what you did, but I don't understand how the first line works. Is there a simple explanation?
Once again, Mike C saves the day. :) too bad you don't live in my city, I could take ya to a bar once I've finished setting my place up and get you tanked. :lol:

Promixis
July 11th, 2004, 06:19 PM
:D

first line...

all variables start off with a value of nil which = not true.

so

x = x or 1

means x = x (not true if 0) or 1 therefore x = 1 on the first pass.

saves doing if not x then x =1 end