Executing a block of code once [Solved]

Edge123

  • Posts: 62
The title is a bit confusing, but what I'm trying to say is, is that in my "updating" code, I want to execute a chunk of code only once, but have a code inside that chunk constantly updated so it can meet my true or false statements.

https://imgur.com/a/0Sqlevf

The true or false statements I am talking about is inside the "do every 1.4 seconds" block. What can I do to make it so its constantly checking in that block of code so "get current frame = 4" and "shoot = false" can become a true statement?

« Last Edit: September 30, 2019, 07:26:50 pm by Edge123 »

Luyren

  • *
  • Posts: 2782
Just place that piece of code in the "Update" event, outside of any other ifs or "do every" or what have you.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Edge123

  • Posts: 62
Wow it was that simple, I don't know why I didn't think of that. Unfortunately a new problem came up where for some reason the enemy won't shoot on a certain frame of their animation.

https://imgur.com/a/XwTAp36

What exactly is the problem here?

Luyren

  • *
  • Posts: 2782
You have:
If playerX > X center of self and "In Range" is true

You then have an otherwise if. The "otherwise" part means that it negates the entire IF above. So it is checking for:
If NOT player X > X center and NOT "In Range" (In Range = false)

And the IF part of your otherwise IF, you are checking if "In Range" is true. You cannot have a boolean true and false at the same time, so that is why it "won't shoot at certain frames of your animation", most likely when firing towards the left.

My suggestion: have all that code inside a "If In Range as boolean", which will check if "In Range" is true.
Then, have "if player X > X center of self" and a simple "otherwise" beneath it.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Edge123

  • Posts: 62
Sorry for the late response but I did follow your advice and it still isn't spawning my actor. Any other suggestions on solving this?

Luyren

  • *
  • Posts: 2782
Post your code again, and try to be more specific with your description of when the problem happens.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Edge123

  • Posts: 62
https://imgur.com/a/6RVZGMG

From the looks of it, it seems to stop at "if get current frame for self = 4" and "Shoot = false". In game, the actor does execute their animation, but doesn't shoot their projectile at the desired frame. Do you know why that is?

Luyren

  • *
  • Posts: 2782
Both your "do afters" should be inside the "if frame = 4 and shoot = false."
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Edge123

  • Posts: 62
Alright well after doing that it didn't seem to change anything. One thing I noticed is that if I remove the "if current animation for self = x" from the code it will spawn the projectile on the desired frame, but it only does it once. This is very strange because I follow a similar code to another actor that does this method and it works fine. Do you see why it only shoots once?

https://imgur.com/a/fpQksvW

Luyren

  • *
  • Posts: 2782
The image on your last post has a "do after" outside the loop. And in your previous image, you are also setting the animation and immediately checking for the current animation, that seems redundant. If your actor is only firing once, that means one of the if statements above are failing, either "Shoot" is not being set to false, or your actor value "In Range" is being set to false for some reason.

You should use the print block to print relevant information on the log viewer (the value of "shoot" and the actor's animation and current frame") to see exactly what parts of the code are executing and what a values you are working with, that should help you debug your issue.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Edge123

  • Posts: 62
So I printed and saw the status of each attribute. For some reason, it took a long time for shoot to be set back to false, so I decided to do a "do after x seconds" block to set shoot to false and it worked! I appreciate your help.