Issue with assigning Math.random() to a variable (getting undefined).

  • at some reason yesterday Math.random(); stop working. Console shows 'undefined'. It was working yesterday.

    UPDATE. Math.random() is working by self. But I'm still getting undefined value when I'm trying to assign Math.random to a variable. I'm pretty sure it was working before.


  • So, I didn't change anything and now it starts working again. I'm not sure what was the issue. But we can close this report for now.

  • I use something similar but don't assign it that way, did you try it without toString?

    Ex of it working in mine:

    this.spawnCreep([MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], ('towTruck'+Math.floor(Math.random()*1000)),{memory:{home:, role: 'towTruck', cooldownTimer: 5}});

    or for some reason the variable isn't defined yet when console is executed but can't imagine why.

  • const is block scoped and won't show up on global, like you were attempting to use it. If you want to use it from the command line you either have to put it on global or type it on the command line itself.

  • Thanks, guys, for answers. It seems to be working now. I'm not sure what was the problem but I didn't change my code at all. @WarInternal Yeah, I tried to both const & let in the command line and always got the same issue. There is no scope blocking in the code. Everything is working 🙂

  • letgives us the same scope as const

    I personally still don't understand how this can work. I just did a simple test where I defined a variable with var at the scope of my main and not even that is working. Trying to use it from the console still gives an error indicating that the variable is undefined.

    You are using some kind of magic and I feel left out.
    Edit: I thought about it some more and I see now that the console command you tried in the screenshot still doesn't work for you either. Your issue was with your script and you tried to copy the statement to the console.

    Was the error you got in the script, temporarily, exactly the same as the one you got when you tried running the statement in the console window?

  • var is either module scoped, if declared outside of a function, or function root scoped, regardless of where it is declared inside the function. const/let have more granular scope, so if it's defined in a loop of if block, it will not exist outside of that scope. the module scope is inaccessible from the console, so you would need to use a global scope. to do that, you'd have to append it to the global object, as in global.SOME_CONST = 5;. at that point, you would be able to access SOME_CONST in the console.