playMaker

Author Topic: Basic global triggers / switch  (Read 1916 times)

adami

  • Playmaker Newbie
  • *
  • Posts: 8
Basic global triggers / switch
« on: August 09, 2018, 03:34:41 AM »
Hi all,

I'm attempting to create the basic functionality but seem to be uncertain if this is a matter of global events and variables or something to do with broadcast all.

I need a basic global on/off state. When a player enters a trigger it sets every light in the scene to off. If I got to another trigger, it should know that the lights are off and when I interact with that it should switch all the lights back on.

It also needs to be something that I can turn the triggers/switches and lights into prefabs so that I can duplicate them as much as I like and they'll always work together universally in different scenes.

any suggestions or examples?


daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: Basic global triggers / switch
« Reply #1 on: August 09, 2018, 03:59:26 AM »
* Every light has an FSM that checks a Global Int

* The int starts at 0, so that can be the starting position (light On in this example)

* Your triggers (not the lights themselves) start out waiting for their trigger event

* When they trigger it goes to the next state which Int Adds the sum of 1

* In that same state have it do an Int Compare to see if it's 2 (make sure this is AFTER the int add, so lower down the action list).

* If it's Equal or More Than, tell it to go to a specific state. For this description I'll call it Reset

* If it's Less than, send it back to your first state where you are waiting for the trigger event

* In the Reset state (that it goes to if it's 2 or more) then you do a Set Int for your variable to be 0. You only want it to be 1 or 0 so this is my convoluted way to switch between them.

* The Reset state then goes back to the waiting for trigger event state.

* Now back to your light FSM's. They are all the same which is just waiting to see if the global int is a 1 or 0. In the first state where it is doing an Int Compare to see if it changes to 1 (set that to Every Frame) have it also Set Property to enable your light (NOT every frame). Just to clarify, each light FSM is only dealing with itself, but all of them are watching the same global int.

* When the Int Compare equals 1, make it go to the next state

* The next state does a Set Property to turn the light off. It also then does an Int Compare again, but this time waiting for it to be Equal to 0

* When it's equal to 0, go back to your first state (which turns the light on and waits for 1)


This is probably a weird way to do it but it will work.
« Last Edit: August 09, 2018, 04:20:44 AM by daniellogin »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Basic global triggers / switch
« Reply #2 on: August 09, 2018, 06:51:58 AM »
Hi.
@daniellogin
Yeah, this is not really a good way to do this.
You should always try to minimize using global variables.
Also you should try to minimize using every frame for better performance.

There are a few ways to do this depending on your project.

Have an fsm and make a state with 2 trigger events (on for Lights on and on for lights off)
Make a transition for both of them and connect then to a "On" State and a "Off" state. transition both of them back to the state with the 2 trigger events.

Now you can do different things.
I will show some examples.

Example 1 :
If you can place all your lights in a empty parent (call it for example 'Scene Lights')
You can parent also lights that where made @runtime if needed (use 'Set Parent' after Creating or pooling the object)

In the "On" State you can use 'Activate Game Object' and 'check' activate.
In the "Off" State also 'Activate Game Object' and 'UNcheck' activate.

Example 2 :
In the "On" State do a send event and set it to broadcast all and make a global event called (for example) 'Lights On'
In the "Off" State do the same but then make a 'Lights Off' event.

Then on all your lights object you would need an fsm with a 'On' state and a 'Off' state.
and set the 'Lights On' and 'Lights Off' event as a global transition.
and turn on/off the light for each object there.

Example 3:
You could place all your light object into an array and make a loop for each 'On' and 'Off' after the trigger state.
Then in the loop you can get each object in the array and use 'activate game object' for each light.
OR
You can use a 'send event by name' to send a event instead of broadcasting event to all.

I think example 1 would work best but not always possible (depending on your game)
Example 3 would be the next best thing but a bit more complicated to setup.
Example 2 would be the less best think but also easier to setup.

adami

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Basic global triggers / switch
« Reply #3 on: August 09, 2018, 08:06:20 AM »
Thanks for both responses. Djaydino, re: your second option. I've attempted to use assign two global events to my light fsm, upon mouse left click, cube sends a global event and it should activate my light. The cube fsm works fine but it does not seem to send the event despite checking that the event is Global. Any clues?
Hi.
@daniellogin
Yeah, this is not really a good way to do this.
You should always try to minimize using global variables.
Also you should try to minimize using every frame for better performance.

There are a few ways to do this depending on your project.

Have an fsm and make a state with 2 trigger events (on for Lights on and on for lights off)
Make a transition for both of them and connect then to a "On" State and a "Off" state. transition both of them back to the state with the 2 trigger events.

Now you can do different things.
I will show some examples.

Example 1 :
If you can place all your lights in a empty parent (call it for example 'Scene Lights')
You can parent also lights that where made @runtime if needed (use 'Set Parent' after Creating or pooling the object)

In the "On" State you can use 'Activate Game Object' and 'check' activate.
In the "Off" State also 'Activate Game Object' and 'UNcheck' activate.

Example 2 :
In the "On" State do a send event and set it to broadcast all and make a global event called (for example) 'Lights On'
In the "Off" State do the same but then make a 'Lights Off' event.

Then on all your lights object you would need an fsm with a 'On' state and a 'Off' state.
and set the 'Lights On' and 'Lights Off' event as a global transition.
and turn on/off the light for each object there.

Example 3:
You could place all your light object into an array and make a loop for each 'On' and 'Off' after the trigger state.
Then in the loop you can get each object in the array and use 'activate game object' for each light.
OR
You can use a 'send event by name' to send a event instead of broadcasting event to all.

I think example 1 would work best but not always possible (depending on your game)
Example 3 would be the next best thing but a bit more complicated to setup.
Example 2 would be the less best think but also easier to setup.
« Last Edit: August 09, 2018, 08:12:00 AM by adami »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Basic global triggers / switch
« Reply #4 on: August 09, 2018, 09:28:43 AM »
Hi.
Have you set the event as global?


adami

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Basic global triggers / switch
« Reply #5 on: August 10, 2018, 01:11:01 AM »
Hi Djaydino, I just tried this again but I think I may have misunderstood how activate object works. I switched the action for something like change light colour and it appears to be working. I've also PMed you. Thanks for your help

Hi.
Have you set the event as global?