Fruit Fall random(min, max) code not working

I was trying to make a game for a class I am in, and I was following the tutorial to do so. When I did the code of "random(min, max)" (having the numbers of min and max as the numbers I want) It doesn't seem to work or function properly. I run the code, and nothing happens. In the classroom and in the examples that were shown, the code for them working perfectly fine. Is my account bugged or is this a bigger issue?


For context: I am trying to make Fruit Fall, one of the tutorials. I want it to randomly spawn in the fruit and animals from the sky.

When I run the code, nothing happens. The basket moves around perfectly fine, but the items do not spawn or randomly come from the sky.


Link to the game here:

The picture of my code for more detail.

There is more code, but you get the idea. It follows the same pattern.


If anyone can help, that would be greatly appreciated. I have no idea why this is happening because it was working for everyone else.


Thanks,

veggie

Comments

  • ArmellineArmelline Member, PRO Posts: 5,331
    edited September 2021

    It's a little hard to read the screenshot, but let's run through what this is expected to do. I don't see any definite reason it wouldn't work, but perhaps we can compare what it should be doing to what you're trying to make it do.


    This will pick a random number, x, between 1 and 3. Every x seconds it will run the code. x is only picked once - it doesn't pick a new x every time. So your spawning will happen every 1, 2 or 3 seconds.

    Every 1, 2 or 3 seconds, it will pick a random number between 1 and 5.

    If that number is 1, it will spawn a banana. This banana will be spawned at an angle of 62 to 968 relative to the spawner's rotation. Angles only go from 1 to 360 (or 0 to 359, both ignoring decimals) so that 968 will "wrap around" making this essentially "any angle". It starts at 62 but 361-422 are also included, so 0-62 are in effect included. So your actor can spawn at any rotation.

    It will spawn the actor at 768 pixels ABOVE the spawning actor. This is most likely to be the cause of your problem, given the information provided. If your spawning actor isn't 768 pixels below the top of the screen, anything it spawns will spawn off the top of the screen. Everything it spawns is 768 pixels ABOVE it. If the banana isn't going to automatically move down, it might well never be seen. This is because you set the spawn to be "relative to actor" - if you change this to "relative to scene" then the spawns will all happen at 768 pixels on the Y.

    To complicate matters, GameSalad will DESTROY any actor that moves when it is 500px or more off the scene. (Something I really wish they'd change.) So if your spawning actor is at the top of your scene, and you are spawning 768 pixes up, it's possible that the moment your bananas move they get destroyed automatically.

    You then set the self.WhatItem actor back to zero.

    If the issue is NOT that the bananas are being spawned off the top of the screen and/or being destroyed automatically, we cannot rule out an order of operations issue. GameSalad does NOT guarantee it will execute your code in the order you write it. So it could be setting self.WhatItem to zero BEFORE it spawns the banana, making the rule no longer valid and so not spawning the banana at all. The only way to guarantee order of execution is to check the "run to completion" box in a timer. Realistically, though, it's VERY rare that GameSalad won't execute your code in order, so it's not likely to be this.

    My money is on your spawner being near the top of the screen and so when you spawn bananas 768px above it they are more than 500px off the scene and get destroyed. Try setting the Y value in the Spawn Actors to 0.

  • veggieRLveggieRL Member, PRO Posts: 2

    I have changed all of the "relative to actor" to "relative to scene." This has fixed the issue. I also moved the actor "spawner" down as well. The game is functioning as it should now. Thank you so much for the help, I was freaking out because everyone else's looked fine while mine wasn't functioning at all. I assumed it was the random(min, max) not working, but It turned out to be a lot simpler issue.


    Thank you so much for the help 👍️, this will help me get a good grade now 🤣

Sign In or Register to comment.