Silly question for you pros



  • Hi All - I am new to JS - as indicated from a previous post. I simply decided that I would fumble my way through the same, all the while taking the JS Codeacademy course.

    That said im running into the following issue when trying to commit 'Worker1' to memory, to be used in my role later.

    Game.creeps.Worker1.memory.role = 'harvester';
    harvester
    console.log(Memory.creeps.Worker1.role);
    [3:22:24 PM]harvester
    undefined

    how can it be 'harvester' AND undefined?

    ...again sorry for the seemingly trivial question, any help is very appreciated!



  • Try adding something to the string in console.log, like so: console.log("Creep role = " + Memory.creeps.Worker1.role);
    Just to see what it does. Hint: maybe only one of those lines is from the console.log. Btw, are you doing this in the console or code?

    But, keep in mind that this is a tick based system. Normally with software it runs one line after the other, but tick based might have some stuff not set in that tick. Like a transfer energy. The actual transfer will happen after your code was executed, so if you do a transfer energy and right after that check how much energy it has, it won't have changed. Same with moving creeps and a bunch of other stuff. Just keep in mind, what I just said has nothing to do with the above question, just a heads up 🙂



  • I believe what you are seeing is actually exactly as it should (if I have read this correctly) ..
    the console.log() command is putputting text to the console, and as expected, prints the role of your creep...
    the undefined is coming from the actual console.log() function call, ie if you put 1 into your console, you'll see 1 printed, if you put 1+1, you'll see 2 printed. its printing the result of the expression .. in your case console.log is returning undefined, as any function in javascript that does not explicitly return a value, will always return undefined (equiv to void in other languages)



  • You're not calling the memory.

    try

    console.log(Game.creeps.Worker1.memory.role);