Area2d repeated entering ( 5 times) then stop the player from entering the code Area2d again.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rodell2020

I have A Question block (an Area2D)
when I jump and hit it sends a body entered signal to my HUD
which runs this code

func _on_QB_body_entered(body: Node) → void:
var s = coininstance.instance()
call_deferred(“add_child”,s)
rng.randomize()
coins = coins + rng.randi_range(1,5)
_ready()
counter = counter + 1

I’m not sure I fully understand the question

var counter = 0

func _ready():
     counter = counter + 1 // always result = 1
      pass


func onQBbodyentered(body):
    if body :
           counter += 1
           if counter  < 6:
             var s = coininstance.instance()
             calldeferred("addchild",s)
             rng.randomize()
             coins = coins + rng.randi_range(1,5)

ramazan | 2022-03-23 13:05

lets try this
i am trying to create a question block that you can jump up and hit (only 5 times)
each time it sends a signal to HUD and runs a code
[which add a a random number (between 1 and 10) to coins]
the trouble is i can keep jumping up and hitting the block more than 5 times…

rodell2020 | 2022-03-23 13:18

doesn’t this happen?

func onQBbodyentered(body):
    if body :
       counter += 1
       if counter  < 6:
         var s = coininstance.instance()
         calldeferred("addchild",s)
         rng.randomize()
         coins = coins + rng.randi_range(1,5)

ramazan | 2022-03-23 13:28

if i have 1 question block yes
but not if i have more then 1 question block

rodell2020 | 2022-03-23 13:44

after it is done is there any way to reset counter so I could have more than 1 question block and use the same code on both of them.

the question block is an Area2D so when i enter the Area2d it goes to HUD and runs that
code
is i would like to copy the question block so that i could have more than 1 on my level (i.e question block run code, then jump into question block 2 run code then jump into question block 3 run code…

rodell2020 | 2022-03-23 13:49

var counter = -1
var numbers = ["question1", "question2", "question3", "question4"]
var it  = null

func onQBbodyentered(body):
  if body:
	counter += 1
	if counter < 4:
		it = numbers[counter]
		print(it) // question1
	elif counter > 3: // to switch to a new question block
		queue_free()
		counter = 0
   pass

//////////////
I don’t know where and how the question list is.
I just trying to show you the way.
There are of course many options.

ramazan | 2022-03-23 14:26

got it to work thank you thank you thank

rodell2020 | 2022-03-23 17:20

:bust_in_silhouette: Reply From: ramazan

You are welcome