Standalone Expression, Can't Figure Out Why

Godot Version



The image below is of my script, which is shown on the left as being attached to an Effect.tscn, which in this case is itself a child of HitEffect.tscn.

This script has two variables which are intended to drive if the Effect’s AnimatedSprite flips horizontally sometimes for variety.

  1. The first variable is an export, called FLIPPABLE, which you can see in the inspector on the right. It’s an export just in case there is a case where I don’t want an effect to be able to be flipped horizontally by random (I use this script across multiple scenes).
  2. The second variable is an onready, called randDir, which sets a random integer between 0 and 1 (so…just 0 or 1) to itself on ready.

Then, also on ready but just after the variables are assigned, the flip() function runs, which says if FLIPPABLE is active and randDir is 1, flip the animated sprite horizontally.

Or at least that’s what I want it to do. It doesn’t work, and I’m not sure why. I’m also getting an error saying the flip.h line has no effect.

I’ve tried taking one of the ='s out from the function, making it flip_h(), making it self.flip_h, and making it self.flip_h(), all of which either don’t work or aren’t valid. Other flip_h’s in my project work fine, so I’m not sure what’s going on here. As far as I know, the script should be treating the “Effect” node as “self,” and not the “HitEffect” node, as the script is attached to the former.

To be precise, I’m getting this error, which I’ve seen before but been able to fix for other things:

W 0:00:00:0841 Standalone expression (the line has no effect).


flip_h is a variable and you can change it like this.

flip_h = not flip_h
1 Like

You can call a function (or you understand it as “to let it run some commands”) by typing the function’s name and a pair of parentheses:

my_function() # maybe parameters inside?

You can change a variable by typing its name and an equal sign, then followed by your value:

my_variable = true # or other values

So typing just flip_h actually makes no sense to Godot. Just think about it, is it running some commands or changing any variable?

You should actually name FLIPPABLE to flippable for it’s just an ordinary variable even if it’s an export variable, only an unchangeable const variable names like that.

And for the connect code at line 23, you should really type it as:

animation_chaged.connect(Callable(self, "_on_animation_finished"))

This form has less possibility of typing wrong.

Yes typically I would agree, though since the node type that the script is attached to is a CanvasLayer, I would think that it would know to flip_h on self. And I did also try doing self.flip_h, which had the same results.

Capitalization is a preference, there are no rules. I choose to make my export variables UPPER_CASE, my constants Title Case, and my normal variables thisCase

That script did not work when I changed it unfortunately, it did not run my function. As I have it already in the image above, it works as intended.

I’ve found the issue to be a simple misunderstanding of the randi line near the top at my variable, along with needing to make the “flip_h” line into “flip_h = !flip_h.”

I had the former as “& 1” thinking that this meant from 0 to 1, but this actually meant from 0 to 0. Upon re-reading the documentations, “% 2” is what truly gives me 0 to 1. Making this slight change along with the latter fix suggested by grulps here solved my issue.

AKA, same code as above but change lines 13 and 30 to:
@onready var randDir = randi() % 2
flip_h = !flip_h

Thanks guys!

Just a reminder: GDScript recommends using the not keyword instead of the ! exclamation mark symbol.

While I’m accustomed to coding with C++ and C#, I still code it as the recommended style, and I use snake_case for GDScript and C++, instead of camelCase and PascalCase which are preferred in C#. As you mentioned, these are just preferences, so aligning with the most recommended style would be advantageous.