Shooting bullets base on random numbers


Please look at this game:

The bats should shoot the bullet based on random numbers.

Here is the code:

But the bats keep shooting!
Why does this happen? where is wrong?



I think it is working perfectly fine

Do you have any idea what is that variable actually?

Because in my eyes it is changing so fast it looks like bats are firing constantly

A trigger once doesn’t mean once, period. It’s once while the condition is true and then again after the condition(s) change from false and then back to true again. It’s choosing a random number about 60 times per second. The number 8 should come up frequently.

You could use a Boolean object variable or a timer if you want occasional bullets. Or when 1 bullet is deleted, another one could be created.

Maybe you could use a timer to create a bullet every x seconds and then pick a random bat to fire it from. Or maybe give each bat a timer either a fixed anount or randomize it everytime it fires.

Is there any way to decrease games tempo?

You can use the fire bullet extension or a timer to generate a number less frequently. Either a scene timer or give each bat their own timers. If you randomize the time then you wouldn’t need to continually generate random numbers.

scene timer

object timers

1 Like

I have a question about the first image:
Is the meaning of the pick a random bat is: It should pick a bat among several bats?

Is this correct?

yes. Bat can be an object with multiple copies or a group containing different objects that’s named bat. It would pick a random object from them.

Thanks for your replies.

The the game tempo be changed?

What do you mean by tempo? You can adjust the timer rate. If you replace the numbers with variables, you could decrease them every so often, that would decrease the time between the bullets. Either change both the lower and higher number in the random expression or use something like: RandomFloatInRange(variableName, variableName + 2)

I mean the spead of the game

The best way to control tempo is by adjusting things like firing rate or movement speed or adding more enemies or change the timer check value. Anything to compress or increase something.

If something is going to change over time then you use a variable instead of a fixed number. Then you can increase or decrease it at anytime.

As you can see in this code:

I have given two variables to object “Bat”.
One is “fireRate” and “bulletTimer”. But in the left part of the image, you can see a red line under “fireRate” and the engine says you should give a number instead of a string.

But this problem does not occur with scene timers. Why?

Are the variables in the setup screen. With the new variable system, the type isn’t as picky as long as the variable is declared. I don’t think it checks what the type is because that can change during runtime. I’m guessing the variable isn’t setup so it just knows that letters aren’t numbers.

Look at these images:

Uploading: g11.png…

What is your idea?

It looks like you’re using an object variable. It would be objectName.variableName. It looks like it’s looking for a scene variable.

In the code example, you sent me,
Do the variables and timer have to be written in the scene?

I have dedicated the variable and timer to the object but nothing happens!

Timers don’t need setting up. They’re automatic.

The variables? Yes, they need to be in the appropriate setup screen when using the simplified variable handling syntax, otherwise you would need to add the Variable() part which is a pain and makes things tougher to read especially long formulas. So, yes those needed to be setup or declared or whatever the term might be with GDevelop.

Edit: I think you started a scene timer instead of an object timer. Switch it to start the object timer(s) and cross your fingers.

I had dedicated a variable for the timer in the object bat, so I deleted it.

But the variable "fireRate " is inside the object bat.

Now I have used the timer in two places, you can see it below:

Would you like to send you the file?

I just looked at more of the code. The bats don’t start to be spawned until 2 seconds after the scene starts but the timers are attempting to be started at the beginning of the scene before they’re created.

Try moving the start object timer action to the add object event.

Create object
Start object timer

Sorry, but I didn’t understand what you meant.
Maybe because of my poor English!

I am sorry for the trouble I am giving you!