How to use MOUSE _ MOTION ( if mouse cursor is moving) for starting and stopping emitting Particles2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bishop
:warning: Old Version Published before Godot 3 was released.

Hello,
I need help with this,everything is in the question…on the basis I need ON and OFF emit particles of whether the mouse cursor is moving.
cursor is moving = emitting on
cursor is not moving = emitting = off

Scene:

Node2D - script
[
Particle2D
Thanks for help

:bust_in_silhouette: Reply From: YeOldeDM
extends Particles2D

func _ready():
	set_process_input(true)

func _input(event):
	set_emitting( event.type == InputEvent.MOUSE_MOTION )

Maybe not the most efficient way, but probably the most direct way.

Thanks, but this isn’t working for me,I don’t know why?
maybe detecting mouse cursor speed?
mouse_speed = 0
mouse_speed = 1

Bishop | 2017-02-14 00:07

_input won’t do anything without input so you can’t detect a no-input.

Add a variable to control the emission and set to true inside _input the same way but set it to false at the end of each process (after using it for emission).

eons | 2017-02-14 00:36

@eons Thanks but only with a script solution I still not get it… where put variables?..you can show on the example?..i’m still a novice in GD script.

Bishop | 2017-02-14 01:58

It should be a control/flag variable on the script.

Just think (and write if possible) what you want to do:

-If moving, it must emit particles
-In any other scenario must stop emitting

It means that you need a variable to register that “moving” event but be always off when not moving.

var moving = false #the variable

func _input(event):
    if  event.type == InputEvent.MOUSE_MOTION: #is moving
         moving = true #should emit

func _process(delta):
    set_emitting(moving) #if moving emit, if not, don't
    moving = false #always false 
    #covers all the non moving situations, input will turn to true if moving

(you must turn on process and input, of course)

That is a simple way to turn on/off flags, useful in a many scenarios where no-events change behaviors.

eons | 2017-02-14 03:21

Thanks guys for your help…that’s very helpful.

… in the input (event) function must be at the end of the code … set _ emitting (true) for emitting particles
…We have two options 1) use animation player + script
2) only script… this option probably is better for framerate?

This works great:

    extends Particles2D

var moving = false #the variable

func _ready():
	set_process_input(true)
	set_process(true)
 

func _input(event):
	if  event.type == InputEvent.MOUSE_MOTION:
		moving = true
		set_emitting(true)
 
func _process(delta):
	var ms = get_global_mouse_pos()
	set_global_pos(Vector2(ms))
	set_emitting(moving)
	moving = false 

Bishop | 2017-02-14 14:57

:bust_in_silhouette: Reply From: Bishop

I solved…(animation way)…Godot is great,thanks to AnimationPlayer you can animate custom mouse cursor particles.

    extends Node2D

var beruska 
var b_anim


func _ready():
	set_process(true)
	set_process_input(true)
	beruska = get_node("beruska")
	b_anim = get_node("emitting")

func _process(delta):
	var ms = get_global_mouse_pos()
	beruska.set_global_pos(Vector2(ms))

func _input(event):
	if event.type == InputEvent.MOUSE_MOTION:
		b_anim.play("emitting")