Jump to content

How to set up a timed switch light?


BarbyFN

Recommended Posts

I attempted to modify the autoclose door script from this tutorial to work with a light switch, but it doesn't work. This is my modification:

scn AutoClosingScript

ref light
float LightTimer

Begin GameMode
		if LightTimer > 0
			set LightTimer to LightTimer - getSecondsPassed
		elseif LightTimer == 0
			Light.Disable
		endif
	endif
End

Begin OnActivate
	set light to GetLinkedRef
	if light.GetDisabled
		light.Enable
		set LightTimer to 60
	else
		light.Disable
		set LightTimer to 0
	endif
	Activate
End

I don't know even if the script works because I can't test it at all, in game the switch that has this script disappear, it get disabled for some reason. What am I doing wrong?

Edited by BarbyFN
Link to comment
Share on other sites

I forgot to say that I want to be able to turn the lights on and off at any time, instantly, but if I forgot the lights on, I wish that they turns off itself after a period of time. I am stuck with this...

Link to comment
Share on other sites

Link to comment
Share on other sites

Thank you, those links were useful to understand more about stages and I managed to get it work as I said: I can turn on and off the lights by activating the switch at any time, but, if I forgot the lights on, passed “5 seconds” they turns off themselves.

 

The only problem is that when I load the cell I have those “5 seconds” to activate the switch before the switch ‘disappears’ itself and never appears again. If I load the cell and run to quickly activate the switch, the rest of the game works like a charm, but if I wait those “5 seconds” the switch get disabled in front of my and I can’t understand why.

 

I have this elements:

-A light flagged as “initially disabled” and “persistent reference”

-An activator (electrical switch) which has the script and linked reference to the light.

-This script:

scn SwitchAutoOffScript

ref Light
int Stage
float Timer

Begin OnActivate
	set Light to GetLinkedRef
	if Light.GetDisabled
		Light.Enable
		set Timer to 5
		set Stage to 0
	else
		Light.Disable
		set Timer to 0
		set Stage to 2
	endif
	Activate
End

Begin GameMode
	if Timer > 0
		set Timer to Timer - getSecondsPassed
	elseif Stage == 0
		set Timer to 5
		Light.Enable
		set Stage to 1
	elseif Stage == 1
		if Timer > 0
			; do nothing
		else timer == 0
			Light.Disable
			set Stage to 2
			Activate
		endif
	Elseif Stage == 2
		; do nothing
	endif
End

What could be wrong?

Edited by BarbyFN
Link to comment
Share on other sites

Yes, the activator has all checkboxes unchecked, and is linked to the light in the section “Linked Ref”.

 

The light is not linked at all; the only changes are the checkboxes “Persistent Reference” and “Initially Disabled” checked.

 

For the other question: The activator was not set as persistent ref, but I check it as persistent and nothing changes.

 

I don’t know why the activator gets disabled itself; it is the carrier of the script but the script did not make reference to it (except the line “Activate” that triggers the animation, I also remove this line to test but happens the same)... The most strange is that if I hurry up to quickly activate it after loading the cell, then the activator did not banished and all script works perfect as I said, the only instance in what the activator get disabled (passed xx seconds of the script) is the first time I load the cell. I’m stuck with this...

Link to comment
Share on other sites

Sorry, I'm very far from an expert, so I have no real answer. But I can search with you like I would do for myself : by questionning and by trial & errors.

 

- What happen if you take out the two lines about timer in your OnActivate block ?

- There are two lines where you set the timer to 5, to see wich one is problematic, I would change one (then the other) to say 10 and see if it changes the time before the switch disappear.

 

Or, hopefully, some qualified person will come and spot the problem...

Link to comment
Share on other sites

Just off the top of my head, without testing it all myself, I have a couple of ideas.

First, I would try making that "else timer == 0" near the end an "elseif" statement. Second, unless you need the timer for some reason, try using a "getdistance" function in the script instead or maybe a "GetInCell" function depending on your exact situation and desire. This way the light would disable when you are a certain distance from the light or if you left the interior cell itself.

Link to comment
Share on other sites

Well, after having placed a switch without linked reference I noticed that on activation the switch disappears too, so, that was the cause to my switch disappears at cell loading! Ha, it was really stupid but has given me two solutions:

 

One is to move the line “set Light to GetLinkedRef” from the “OnActivate” to the “GameMode” block. This is the best general solution to use the script in more than one activator. The second is simply remove the entire line and its reference “Light”, replacing them straight for the Reference name itself; this script is 2 lines shorter but I think is better the first one because it leaves the possibility to use the same script in more than one activator if needed. This is the script fixed and working:

scn SwitchAutoOffScript

ref Light
int Stage
float Timer

Begin OnActivate
	if Light.GetDisabled
		Light.Enable
		set Stage to 0
	else
		Light.Disable
		set Stage to 1
	endif
	Activate
End

Begin GameMode
	set Light to GetLinkedRef
	if Timer > 0
		set Timer to Timer - getSecondsPassed
	elseif Stage == 0
		set Timer to 60
		set Stage to 1
	elseif Stage == 1
		if Timer > 0
			; do nothing
		else
			Light.Disable
			set Stage to 2
			Activate
		endif
	elseif Stage == 2
		; do nothing
	endif
End

I must thank you the advices that makes me see the problem from different perspectives. Pixelhate, removing lines helps me to improve the script, making it smaller, and Vforvic your idea to use “getdistance” could be ideal, but in my case I will use this to a practice reflector and it isn't an important light, also the timer leaves me the ‘sensation’ of real timed clocks.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...