Jump to content

How to make one script that responds to activation of multiple push buttons?


Recommended Posts

Good evening everybody!

 

So, usually if you want to make a pushbutton do something you would use a script like this:

Event OnActivate(ObjectReference akActionRef)
    if akActionRef == Game.GetPlayer()
    
    DO SOMETHING
    
    endif
EndEvent

That scirpt would be attached to the push button.

 

But what if I want to have one script (attached to a quest or whatever) that handles multiple push buttons?

So something like this:

 

If pushbutton1 is pushed, do something.

If pushbutton2 is pushed do something else.

 

So something like pushbutton1."OnActivate" if there is such a thing ...

 

Or how else shoudl I do this?

I want to avoid having small scripts all over the place, it would be nice to have it all in one scirpt.

Link to comment
Share on other sites

You could make the buttons stateful. Maybe have them light up when pushed and unlight when pushed again. You give each light-up button the same script and maybe a bool property to say if the state is pushed or not. Then a master script that queries the states of all the buttons is references before deciding what to do.

 

That's not all in a single script, but it's got it down to two.

Edited by DocClox
Link to comment
Share on other sites

You could make the buttons stateful. Maybe have them light up when pushed and unlight when pushed again. You give each light-up button the same script and mayeb a bool property to say if the state is pushed or not. Then a master script that queries the states of all the buttons is references before deciding what to do.

 

That's not all in a single script, but it's got it down to two.

Yes, that's what I did before ...

Having the buttons enable an invisible xmarker.

And the "MasterControlScript" would then check the enable state of those markers, and after doing what it needs to do it would disable that xmarker again, so it could respond to another button push.

Link to comment
Share on other sites

 

You could make the buttons stateful. Maybe have them light up when pushed and unlight when pushed again. You give each light-up button the same script and mayeb a bool property to say if the state is pushed or not. Then a master script that queries the states of all the buttons is references before deciding what to do.

 

That's not all in a single script, but it's got it down to two.

Yes, that's what I did before ...

Having the buttons enable an invisible xmarker.

And the "MasterControlScript" would then check the enable state of those markers, and after doing what it needs to do it would disable that xmarker again, so it could respond to another button push.

 

Why not

 

scriptname ButtonMcButtonFace extends Objectreference

bool property is_pressed = false auto

event OnActivate()
    is_pressed = !is_pressed
    master_script.button_handler()
endevent

And then:

 

script master_script extends whatever

ButtonMcButtonFace property button1 auto
ButtonMcButtonFace property button2 auto

function button_hander()
    if button1.is_pressed 
		if button2.is_pressed
			do_something()
			return
		else
			do_something_else()
		endif
	else
		if button2.is_pressed
			do_some_third_thing()
		else
			release_the_hounds()
		endif
	endif
endfunction

No markers needed, and you can have as many buttons as you like

Link to comment
Share on other sites

 

 

You could make the buttons stateful. Maybe have them light up when pushed and unlight when pushed again. You give each light-up button the same script and mayeb a bool property to say if the state is pushed or not. Then a master script that queries the states of all the buttons is references before deciding what to do.

 

That's not all in a single script, but it's got it down to two.

Yes, that's what I did before ...

Having the buttons enable an invisible xmarker.

And the "MasterControlScript" would then check the enable state of those markers, and after doing what it needs to do it would disable that xmarker again, so it could respond to another button push.

 

Why not

 

scriptname ButtonMcButtonFace extends Objectreference

bool property is_pressed = false auto

event OnActivate()
    is_pressed = !is_pressed
    master_script.button_handler()
endevent

And then:

 

script master_script extends whatever

ButtonMcButtonFace property button1 auto
ButtonMcButtonFace property button2 auto

function button_hander()
    if button1.is_pressed 
		if button2.is_pressed
			do_something()
			return
		else
			do_something_else()
		endif
	else
		if button2.is_pressed
			do_some_third_thing()
		else
			release_the_hounds()
		endif
	endif
endfunction

No markers needed, and you can have as many buttons as you like

 

Interesting idea!

So I can use a function definded in one script in other scripts?

Useful!

Link to comment
Share on other sites

Okay there's a thousand ways to do this but have you thought about using Ints or even GlobalVariables, maybe using a quest with repeatable stages.

 

Basically using a conditional script with a conditional variable. Each button press is a "+1" and when the variable reaches 2 or 3 different things will occur

Link to comment
Share on other sites

Okay there's a thousand ways to do this but have you thought about using Ints or even GlobalVariables, maybe using a quest with repeatable stages.

 

Basically using a conditional script with a conditional variable. Each button press is a "+1" and when the variable reaches 2 or 3 different things will occur

Yeah, that might work too ...

Queststage == 0: Door is closed.

Queststage == 1. Door is opened.

 

The buttons will just "setstage" to 0 if it is 1 and to 1 if it is 0.

(If a certain condition is true ...)

 

I actually like the "quest stage" approach.

I think I am going to use that.

Link to comment
Share on other sites

Ok, I am getting this error:

cannot call the member function GI_RotatingWall_ButtonHandler alone or on a type, must call it on a variable

When trying to call the function from the "master script" in the "button script".

Yeah, you need an instance. If you want to call it from the class, the function needs to be marked global.

 

 

I actually like the "quest stage" approach.

I think I am going to use that.

 

If it's a good fit for the problem, then why not?

Link to comment
Share on other sites

I actually like the "quest stage" approach.

I think I am going to use that.

 

 

If it's a good fit for the problem, then why not?

 

Yes, I am going to use it.

I just thought maybe anohter way would be better.

 

But quests are pretty much "premade state machines", so why not use them for what they are designed to do, right?

Link to comment
Share on other sites

  • Recently Browsing   0 members

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