Scripts: Enable when disabled / Disable when enabled

Post » Thu Aug 25, 2011 12:06 pm

I'm trying to make some kind of a riddle. Shortly, we have 5 lights and 5 buttons. Each button turns off the light when it's enabled or turns on when disabled. The same rule applies to the surroundings lights on the left and right side, so, for a better view:
We have five lights on:
[x][x][x][x][x]
Then we press second button and we are turning off second light and surrounding ones (because all were enabled)
[ ][ ][ ][x][x]
Then we press third button and we turning on two lights (due to disabled status) and off only one (due to enabled status).
[x][x][x][ ][x]

Ok, so this is how it works ... in my mind.

I've made two scripts. The first one was pinned to every light:

begin cc_mar_s_litestatusshort stateif ( GetDisabled == 1 )	set state to 0endifif ( GetDisabled == 0 )	set state to 1endifend


The second script was added to button and it looks as follows:

begin cc_mar_s_litesbtn2if ( OnActivate == 1 )PlaySound, "Torch Out"	if ( "cc_mar_lite1"->GetDisabled == 0 )		Disable	else		Enable	endifendifif ( OnActivate == 1 )PlaySound, "Torch Out"	if ( "cc_mar_lite2"->GetDisabled == 0 )		Disable	else		Enable	endifendifif ( OnActivate == 1 )PlaySound, "Torch Out"	if ( "cc_mar_lite3"->GetDisabled == 0 )		Disable	else		Enable	endifendifend


I've also tried if ( "cc_mar_lite3".state == 1 ) instead of if ( "cc_mar_lite3"->GetDisabled == 0 ) but it doesn't work too. What's more, sometimes the button is able to disappear completely when we activate it. I don't see any mistakes in my scipts so, really, I don't have any idea how to get around this problem.
User avatar
asako
 
Posts: 3296
Joined: Wed Oct 04, 2006 7:16 am

Post » Thu Aug 25, 2011 1:02 pm

I wrote these two scripts to light fires in fireplaces, or put them out, but the principle should be the same. I know these work:

Begin On_OffScript;On and off like a lightswitch;'*********************************************;' This script attaches to the button;'*********************************************short switchcont*;FireOn is global but should have a unique identifier like ModIDFireOnIf ( MenuMode == 1 )		ReturnendIfIf ( OnActivate == 1 )		set switchcont to 1endIfIf ( switchcont == 1 )	If ( FireOn == 0 )			set switchcont to 2	Else		set switchcont to 3	endIfendIfIf ( switchcont == 2 )		set FireOn to 1		set switchcont to 0endIfIf ( switchcont == 3 )		set FireOn to 0		set switchcont to 0endIf	End On_OffScript;'***************************************************;'This script attaches to the light ;'***************************************************Begin On_Off2Script;On and off like a lightswitchIf ( MenuMode == 1 )		ReturnendifIf ( FireOn == 0 )		Disable		ReturnendIfIf ( FireOn == 1 )		Enable		ReturnendIfEnd On_Off2Script

User avatar
Sam Parker
 
Posts: 3358
Joined: Sat May 12, 2007 3:10 am

Post » Thu Aug 25, 2011 6:56 am

I don't see any mistakes in my scipts
:) sorry, could not resist
lights should be unique and have reference persist on if you want to refer to them from script, no need to script them. I'd try something like this
begin cc_mar_s_litesbtn2if ( OnActivate )	PlaySound "Torch Out"	if ( cc_mar_lite2->GetDisabled )		cc_mar_lite2->enable		if ( cc_mar_lite1->GetDisabled )			cc_mar_lite1->enable		else			cc_mar_lite1->disable		endif		if ( cc_mar_lite3->GetDisabled )			cc_mar_lite3->enable		else			cc_mar_lite3->disable		endif	else		cc_mar_lite2->disable	endifendifendbegin cc_mar_s_litesbtn3if ( OnActivate )	PlaySound "Torch Out"	if ( cc_mar_lite3->GetDisabled )		cc_mar_lite3->enable		if ( cc_mar_lite2->GetDisabled )			cc_mar_lite2->enable		else			cc_mar_lite2->disable		endif		if ( cc_mar_lite4->GetDisabled )			cc_mar_lite4->enable		else			cc_mar_lite4->disable		endif	else		cc_mar_lite3->disable	endifendifendbegin cc_mar_s_litesbtn4if ( OnActivate )	PlaySound "Torch Out"	if ( cc_mar_lite4->GetDisabled )		cc_mar_lite4->enable		if ( cc_mar_lite3->GetDisabled )			cc_mar_lite3->enable		else			cc_mar_lite3->disable		endif		if ( cc_mar_lite5->GetDisabled )			cc_mar_lite5->enable		else			cc_mar_lite5->disable		endif	else		cc_mar_lite4->disable	endifendifend
I will leave the scripts for button 1 and 5 to you, as I don't know if you want to change state of light 1/5 when pressing button 5/1
User avatar
Brandi Norton
 
Posts: 3334
Joined: Fri Feb 09, 2007 9:24 pm

Post » Thu Aug 25, 2011 7:37 pm

and none of you quoted your IDs. :angry:

Lights also don't always disable properly, you're often better off moving them (say, z -1000 units).
User avatar
Naomi Lastname
 
Posts: 3390
Joined: Mon Sep 25, 2006 9:21 am

Post » Thu Aug 25, 2011 2:37 pm

and none of you quoted your IDs. :angry:

Lights also don't always disable properly, you're often better off moving them (say, z -1000 units).


Wuhlll let me tell you, Pilgrim, I didn't quote my IDs cuz there aint no IDs in either script... Now go for your six shooter on the count of three... One,... one and a half... one and three-quarters... Wait, where wuz I? guess I'll have ta start over...
User avatar
Susan
 
Posts: 3536
Joined: Sun Jun 25, 2006 2:46 am

Post » Thu Aug 25, 2011 5:03 pm

Ok, first of all I made two, ridiculous mistakes. But before I write anything I would like to thank you for quick respond. Really, thanks!

So, in my script there was something like that:
if ( OnActivate == 1 )PlaySound, "Torch Out"        if ( "cc_mar_lite1"->GetDisabled == 0 )                Disable        else                Enable        endifendif

and there is no way to get any success by that, because Disable and Enabled refers to the button. I forgot to place ID before both functions and that's why my button was disappearing and I didn't know why.

The second mistake was in triple OnActivate functions. My overlook was enormous fault here. However, I used abot's script and it looks like that:

if ( OnActivate == 1 )PlaySound "Torch Out"if ( cc_mar_lite2->GetDisabled )cc_mar_lite2->enableelsecc_mar_lite2->disableendifendif


This example refers to cc_mar_lite2 but now it's very simple to include another lights (we have to copy internal IF-ENDIF part changing only ID of the light). Now everything works correctly, and riddle looks fantastic :)

Abot, I knew about reference persist and unique ID, but thanks for your hint anyway.
User avatar
Flutterby
 
Posts: 3379
Joined: Mon Sep 25, 2006 11:28 am

Post » Thu Aug 25, 2011 5:33 pm

and none of you quoted your IDs. :angry:
What can I say, never had a issue with not quoting proper identifiers (e.g. formed only by alphanumeric chars and underscore, not beginning with number or underscore), at least with a single operand ( id->function, no other parameters)
if function has parameters or there are more than 2 parts involved, I agree, it may be better to use them anyway (e.g player->GetItemCount "myProperID" or "player"->GetItemCount myProperID)
Lights also don't always disable properly, you're often better off moving them (say, z -1000 units).
Yes, you are right , I did not want to make script soon too complicated, I think a 1 pixel drift is enough to properly update collision/lightning after changing disabled/enabled state, e. g. (untested)
begin cc_mar_s_litesbtn3float z1	; main light Z pos temp storagefloat z2	; adiacent lights Z pos temp storageif ( OnActivate )	PlaySound "Torch Out"	set z1 to ( cc_mar_lite3->GetPos Z )	if ( cc_mar_lite3->GetDisabled )		set z1 to ( z1 + 1 )	; raise it a little to refresh ligtning/collision if mesh has it		cc_mar_lite3->enable		set z2 to ( cc_mar_lite2->GetPos Z )		if ( cc_mar_lite2->GetDisabled )			set z2 to ( z2 + 1 )			cc_mar_lite2->enable		else			set z2 to ( z2 - 1 )			cc_mar_lite2->disable		endif		"cc_mar_lite2"->SetPos Z z2	; here I think quoting identifier may be safer		set z2 to ( cc_mar_lite4->GetPos Z )		if ( cc_mar_lite4->GetDisabled )			set z2 to ( z2 + 1 )			cc_mar_lite4->enable		else			set z2 to ( z2 - 1 )			cc_mar_lite4->disable		endif		"cc_mar_lite4"->SetPos Z z2	else		set z1 to ( z1 - 1 )	; lower it a little		cc_mar_lite3->disable	endif	"cc_mar_lite3"->SetPos Z z1endifend

User avatar
James Rhead
 
Posts: 3474
Joined: Sat Jul 14, 2007 7:32 am

Post » Thu Aug 25, 2011 8:16 am

Now everything works correctly, and riddle looks fantastic :)
Nice :cool:
User avatar
Sheila Reyes
 
Posts: 3386
Joined: Thu Dec 28, 2006 7:40 am


Return to III - Morrowind