Search code examples
mit-scratch

How to choose a random set of X and Y Coordinates that have been stored into 2 lists


This is the code for "Opal Sword" of which I am trying to place on top of a random Dirt Block

I have been trying this code, but it does not do anything. I cannot for the life of me figure out how to choose a random number, and pull the X and Y coordinates out of the two separate lists based on that one random number. Here's some additional images of my code:

View of what happens after starting code

When I press the Green Flag, "(Dirt) Random X" and "(Dirt) Random Y" both stay blank, and "(Dirt) Random Number" switches from 0 to 1 randomly the more I click on the Green Flag

Logic to get the X and Y coordinates of each Dirt Clone into a List in order to use the coordinates for the Opal Sword placement

And this is the logic that takes the X and Y coordinates of my clones and stores them into the List for future use

All the websites online say this code should work, and Bing CoPilot (ChatGPT-4) cannot tell me any other way to do it and say that my code should work. And, logically, this SHOULD work, but it makes just absolutely no sense as to why it isn't working!

ADDITIONAL ATTEMPT:

I added this "(Dirt) Initialization Finish" Check to make sure it's only gathering the coordinates AFTER it finishes making the list; but I still have an issue with it. The "(Dirt) Random Number" does now actually change randomly as expected, however it does not transfer over that data into the "(Dirt) Random X" and "(Dirt) Random Y" (images below)

enter image description here

enter image description here

enter image description here

enter image description here

I found the resolution with help from the comments!


Solution

  • So the first problem I see is that you have TWO blocks "When green flag clicked", where one depends on another.

    When you click that flag, both blocks start executing. There is no saying of how many items get inserted into your lists by the time you use them in another block.

    I would strongly suggest to use a block called Init or something like that to generate all the values you need in a game, and only start the game after it is done.