Activating container via script opens it and closes again

Post » Wed Apr 13, 2016 3:40 pm

Hello,


I'm trying to make script that shows messagebox with two options. If first is selected, it should open the container. I attach the script below. Problem is that when I go to the barrel in game and select I want to open it, the barrel opens for very short moment and closes again. I tried to remove the activate command, but then nothing happens at all. I also tried to add Messagebox right before the activate command to see if the code is executed really only once and it is. I would appreciate any help :)



Begin ariisiss_barrel

float timer
float swingTime
float swingSpeed
float startAngle

short button
short status

;float script part
set startAngle to GetStartingAngle, x

if ( MenuMode == 0 )

set swingTime to 1
;set swingSpeed to 30

set timer to ( timer + GetSecondsPassed )

;rotate up
if ( timer < swingTime )

Rotate x,2

;rotate down

elseif ( timer < (swingTime * 3) )

Rotate x, -2

;up again
elseif (timer < (swingTime * 4 ) )
Rotate x, 2

;reset timer to zero
else
set timer to 0
SetAngle X startAngle
endif

endif

;end of float script part

;barrel disabling and open script

if ( OnActivate == 1 )
if ( MenuMode == 1 )
return
endif
MessageBox "Do you want to open the barrel, or remove it?" , "Open", "Remove"
set status to 1
endif

If ( status == 1 ) ; button was pressed
set button to GetButtonPressed
if ( button == -1 ) ; if button not pressed do nothing
return
elseif ( button == 0 ) ; open the barrel
set status to 0
activate
elseif ( button == 1 ) ; remove the barrel
set status to 0
disable
setdelete 1
endif
endif

;end of barrel disabling and open script

End
User avatar
Amysaurusrex
 
Posts: 3432
Joined: Wed Aug 09, 2006 2:45 pm

Post » Wed Apr 13, 2016 5:33 pm

Since timer is always going to return less than swingtime * 3 or 4 the way it's written, I'd change these lines:



elseif ( timer >= (swingTime * 3) )

Rotate x, -2

;up again
elseif (timer >= (swingTime * 4 ) )
Rotate x, 2

If the container stays open too long, just reduce the swingtime multipliers.

User avatar
JUan Martinez
 
Posts: 3552
Joined: Tue Oct 16, 2007 7:12 am

Post » Thu Apr 14, 2016 3:53 am

The float script looks fine to me. It could be optimized a little bit, but It's just a copy/paste of the vanilla one IIRC.

As for your activate problem, it can be solved by not calling activate on the same frame that the messagebox UI is closed.
SetDelete generally shouldn't be called on the same frame as Disable either, so you could fix both problems at once like this:


Spoiler

begin ariisiss_barrel

float timer
float swingTime
float startAngle

short state
short button

if ( MenuMode == 0 ); float script
set timer to ( timer + GetSecondsPassed )

set swingTime to 1

;rotate up
if ( timer < swingTime )
Rotate x 2

;rotate down
elseif ( timer < ( swingTime * 3 ) )
Rotate x -2

;up again
elseif ( timer < ( swingTime * 4 ) )
Rotate x 2

else; reset
set startAngle to GetStartingAngle x
SetAngle x startAngle
set timer to 0
endif
endif

;barrel disabling and open script

; do nothing if not active
if ( OnActivate + state == 0 )
return
endif

; show choice
if ( state == 0 )
MessageBox "Do you want to open the barrel, or remove it?" "Open" "Remove"
set state to 1
endif

; await choice
if ( state == 1 )
set button to GetButtonPressed
if ( button == 0 ); open
set state to 2
elseif ( button == 1 ); remove
set state to 3
Disable
endif
return
endif

; apply choice
if ( state == 2 ); opened
Activate
elseif ( state == 3 ); removed
SetDelete 1
endif
set state to 0

end

User avatar
Cesar Gomez
 
Posts: 3344
Joined: Thu Aug 02, 2007 11:06 am

Post » Thu Apr 14, 2016 2:46 am

Yes, it is basically a copy-paste of the float script, with the exception of this line



SetAngle X startAngle

original line was



SetAngle, x, GetStartingAngle, x

Unfortunatelly, when you try to use the original, or simply try to re-save original float script, the compiler will report error.



Thanks for the fixed script, Greatness7, I will try it at once :)



Calling setdelete in the same frame as disable causes trouble only if there is other function operating on the object as far as I know. But you are right that it is better to be on the safe side :)

User avatar
Kari Depp
 
Posts: 3427
Joined: Wed Aug 23, 2006 3:19 pm

Post » Wed Apr 13, 2016 4:25 pm

Something is still wrong with the script. I got error



Script Error: EXPRESSION in ariisiss_barrel

Left eval



The barrel is in game, but does not float and activating it does nothing. I will try to fix it.



EDIT: Error is gone when I replaced your float script with the original one, but activating still does nothing. Back to the drawing board!



EDIT2: In the end, I just altered my original script to activate in next frame and everything works fine now. Thanks for your help :)

User avatar
Stu Clarke
 
Posts: 3326
Joined: Fri Jun 22, 2007 1:45 pm

Post » Wed Apr 13, 2016 7:41 pm


That's interesting. MWEdit successfully compiles the script but the CS fails.



Here it is with fixed "CS-compatible" syntax:



Spoiler



begin ariisiss_barrel

float timer
float swingTime
float startAngle

short state
short button

if ( MenuMode == 0 ); float script
set timer to ( timer + GetSecondsPassed )

set swingTime to 1

;rotate up
if ( timer < swingTime )
Rotate x 2

;rotate down
elseif ( timer < ( swingTime * 3 ) )
Rotate x -2

;up again
elseif ( timer < ( swingTime * 4 ) )
Rotate x 2

else; reset
set startAngle to GetStartingAngle x
SetAngle x startAngle
set timer to 0
endif
endif

;barrel disabling and open script

; do nothing if not active
if ( OnActivate )
elseif ( state == 0 )
return
endif

; show choice
if ( state == 0 )
MessageBox "Do you want to open the barrel, or remove it?" "Open" "Remove"
set state to 1
endif

; await choice
if ( state == 1 )
set button to GetButtonPressed
if ( button == 0 ); open
set state to 2
elseif ( button == 1 ); remove
set state to 3
Disable
endif
return
endif

; apply choice
if ( state == 2 ); opened
Activate
elseif ( state == 3 ); removed
SetDelete 1
endif
set state to 0

end





User avatar
Luna Lovegood
 
Posts: 3325
Joined: Thu Sep 14, 2006 6:45 pm

Post » Wed Apr 13, 2016 4:38 pm

Thanks. I'm using this right now:




Spoiler


Begin ariisiss_barrel

float timer
float swingTime
float swingSpeed
float startAngle

short button
short status

;float script part
set startAngle to GetStartingAngle, x

if ( MenuMode == 0 )

set swingTime to 1
;set swingSpeed to 30

set timer to ( timer + GetSecondsPassed )

;rotate up
if ( timer < swingTime )

Rotate x,2

;rotate down

elseif ( timer < (swingTime * 3) )

Rotate x, -2

;up again
elseif (timer < (swingTime * 4 ) )
Rotate x, 2

;reset timer to zero
else
set timer to 0
SetAngle X startAngle
endif

endif

;end of float script part

;barrel disabling and open script

if ( status == 2 )
activate
set status to 0
return
endif

if ( OnActivate == 1 )
if ( MenuMode == 1 )
return
endif
MessageBox "Do you want to open the barrel, or remove it?" , "Open", "Remove"
set status to 1
endif

If ( status == 1 ) ; button was pressed
set button to GetButtonPressed
if ( button == -1 ) ; if button not pressed do nothing
return
elseif ( button == 0 ) ; open the barrel
set status to 2
return
elseif ( button == 1 ) ; remove the barrel
set status to 0
disable
setdelete 1
endif
endif

;end of barrel disabling and open script

End


User avatar
Jeneene Hunte
 
Posts: 3478
Joined: Mon Sep 11, 2006 3:18 pm


Return to III - Morrowind