As a developer, I often need to output some value when working with the command bar, however this is inconvenient because I need to explicitly put print(...) around the command I just wrote to get any value from the expression.
It would be great if there was an option so that Studio automatically printed the value of the expression in the command I just wrote. This saves on clutter in the command bar (don’t need to put print(…) around the thing I just wrote) and makes it much quicker to get the value I want to see.
Examples:
> 4 + 5
9
> workspace:FindFirstChild("Baseplate")
Baseplate
> game.CollectionService:GetTagged("...")
{ ... }
> a = 1 b = 2 c = a + b
-- this is not a single expression, do not automatically print any values
This would be reminiscent of “interactive shells” that exist for other popular scripting languages.
An advanced extension of this feature could be to automatically print the last expression / last assignment / last statement in the command bar (for example, print 3 for that last example above, since c becomes 3 with that last statement). However, the design for that isn’t as clean / unopiniated as with single expressions, so I’m not suggesting that here for now.
If Roblox would implement this suggestion, it would simplify any work I do that uses the command bar and would improve how fast and convenient I am able to get my commands done.
That’d be a nice addition for sure, but it doesn’t really solve wanting to type less boilerplate not related to what I’m trying to get out of my command. (instead of print(…), it’s now return …) I feel like the command bar should be smart enough to figure out that I want something printed when I put a single expression that returns some value.
There is a similar feature I appreciate a lot when I’m working on JavaScript in the browser. In Chrome the devtools console will automatically evaluate and show the result of whatever I type into the bar (without me pressing enter) which makes it very quick to check calculations.
I very frequently start typing something into the command bar and then have to go back over when I’m done to wrap it in a print statement. As a recent example, I was converting Color3 values from the properties widget to HSV values by using the expression print(Color3.toHSV(Color3.fromRGB(255,255,255))). The print statement makes it harder to parse this one-liner because it adds another layer of parenthesis, and is really just another annoying thing to remember to add. Other times I’ve wanted the Color3 of BrickColors, or the result of some math I’m working on with hard-coded in numbers for the sake of rapid testing. Adding the print statement to all of these is an annoying detail that I never ever remember or ever appreciate.
Because it would be a pain to change between wanting to use it as an interactive shell and actually write a quick small script (e.g getting the objects in a plugin to inspect the code before using it).
Also how would it work in cases where a function returns values but you don’t actually need them?
And in a case like this?
wait(5) print("hi")
Would it output the delta time and the other result by wait, and then print "hi"?
What would happen when the command can be interpreted as one statement or as one expression.
math.cos(1)
This is both a valid statement and expression, so should it print the result as if it were an expression?
One way this could be done is by printing all the return values and testing if return %s; (where %s is the command bar input) is valid before loading the string normally.
Yes, it should print the result. See the two examples in OP with functions/methods that return a result.
If someone writes “math.cos(x)” in the command bar then you can reasonably assume they meant to find out the value of that expression, so printing it totally makes sense.