Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Choosing random values inside a loop (was: In a hole in the bottom of the sea... Send help...)

edited October 2016 in Help! with 2.0
So, I'm working on creating a randomly generated map in Sugarcube 2 for my game. It's slow, but I'll get there. I've run into my first issue however. What I'm trying to do is place a loop withing an array, and that loop contains a set function that will select a random number from a predetermined list. Here, the code will explain itself, or well better I I can at least.
[
<<for $i = 0; $i < 10; $i++>>
<<set $i to either (0, 0, 0, 1)
<</for>>
]

If someone could tell me where I went wrong or what I need to do would be great. Thank you in advance.

Comments

  • (I've edited the subject line of this post to be clearer as to what it's about.)
  • edited October 2016
    I think it's this line here:
    <<set $i to either (0, 0, 0, 1)

    I'm pretty sure you need to add a >> to close off the set macro and (possibly) remove the space between either and its args (...)

    Also, you "can't" use $i in both places unless you want an infinite loop because $i will never be greater than 10 if you set it to either(0, 0, 0, 1) on every pass. You'll want to use different variable names for your index and for your map's... wall assignment?
  • You cannot place a <<for>> loop within an array—that's not syntactically valid. Also, your loop index variable should probably be a temporary variable, not a story variable (see: Variables).

    You may have been looking for something like the following:
    <<set $listOfNumbers to []>>
    <<for _i = 0; _i < 10; _i++>>
    <<run $listOfNumbers.push(either(0, 0, 0, 1))>>
    <</for>>
    
    That will populate $listOfNumbers with ten randomly chosen numbers, whose values will be either 1 (3/4 chance) or 0 (1/4 chance).

    Based on your example, I'm assuming that is the result you were attempting to achieve.
  • I have run into the following error: error: <<for>>: bad conditional expression: Cannot read property 'length' of undefined. Naturally, this is probably due to my idiot self trying to edit TheMadExile's code.
    <<set $line1map to []>>
    <<for _i = 0; _i < 10; _i++>>
    <<run $line1map.push(either(0, 0))>>
    <</for>>
    <<set $line2map to []>>
    <<for _i = 0; _i < 10; _i++>>
    <<run $line2map.push(either(1, 1, 1, 1, 2, 1))>>
    <</for>>
    

    Also JackWinters, my map has no walls, as I am just trying to get a basic map down, I'll get borders and slow uncovering of the map eventually.
  • @Strangedog: Two things:

    1. Both of your for loops are doing the same number of iterations so you can combine them into one.
    2. Your first either() function is randomly choosing between two zero's so it is not needed.

    Try the following:
    <<set $line1map to []>>
    <<set $line2map to []>>
    
    <<for _i = 0; _i < 10; _i++>>
    <<run $line1map.push(0)>>
    <<run $line2map.push(either(1, 1, 1, 1, 2, 1))>>
    <</for>>
    
  • Strangedog wrote: »
    I have run into the following error: error: <<for>>: bad conditional expression: Cannot read property 'length' of undefined. Naturally, this is probably due to my idiot self trying to edit TheMadExile's code.
    <<set $line1map to []>>
    <<for _i = 0; _i < 10; _i++>>
    <<run $line1map.push(either(0, 0))>>
    <</for>>
    <<set $line2map to []>>
    <<for _i = 0; _i < 10; _i++>>
    <<run $line2map.push(either(1, 1, 1, 1, 2, 1))>>
    <</for>>
    
    The code you've shown cannot be giving the error you're seeing. So, the error is either from some other code you're using or from a different version of the above code.

    Also, the shown code is less than optimal—greyelf has already pointed that out and offered suggestions to improve it, so that's all I'll say about it.
  • edited October 2016
    Yep, you're right. It's in another of my passages. Thank you! I'm just inexperienced and dumb. I got what I need, and thank you so much for your help.
Sign In or Register to comment.