• 2D
  • declaring variable in a smart way

Hello I like to use simple variable names in my formulas so I want to define in the first part of my script things like object size or screensize. But it seems that some functions doesn't work if the ready function doesn`t executed. So If I declare them inside ready function, then those variables are not available in the other functions. What is the secret to solve this in an elegant way ? :) Now I reused the code in every scope of the functions.

func _ready():
  position.x = get_screen_halfsize().x
  position.y = get_screen_halfsize().y

func get_screen_halfsize():
  var screen_w = get_viewport().size.x / 2
  var screen_h = get_viewport().size.y / 2
  var c = vec2(screen_w, screen_h)
  return c

edit: good catch I forgot the brackets there.

doesn`t work yet only if I call that function everytime inside another function (in this case in the ready function)

Technicaly, the problem remain the same, but it is cleaner now :) I have to call those functions (vectors) first, in every new functions :)

If you have more improvements on this idea, let me know. Thank you for this solution anyway. It is good enough !!

Personally, I would just define the variables I need outside of the _ready function and then set their values in _ready. Like this:

extends Area2D
var screensize
var gun_size
var gun_x

func _ready():
	# Assign the class variables so they can be used throughout the script:
	screensize = get_viewport().size
	gun_w = $CollisionShape2D.shape.extents * 2
	gun_x = position.x
	
	# Do whatever is needed in _ready below:
	position.x = screensize.x / 2.0
	position.y = (screensize.y / 2.0) - gun_h

Func _process(delta):
	if (Input.is_key_pressed(KEY_LEFT)):
		if (gun_x > gunsize.x/2):
			gun_x-= 5
		else:
			gun_x=gunsize.x/2
	if (Input.is_key_pressed(KEY_RIGHT)):
		if (gun_x < gunsize.x/2):
			gun_x += 5
		else:
			gun_x = screensize.x - gunsize.x / 2

The code above has not been tested. However, the code should, in theory, work just fine.

The important part is that the variables are defined outside of any functions, making them class variables, and then these variables are populated in the _ready function. Because _ready is called before any of the other functions, when you access the class variables in functions like _process the variables will be populated with the values we assigned in _ready.

Another thing you can do is use the onready keyword when defining the class variables. Personally I don’t use onready, but if you want to use onready then you can write the class variables like this:

extends Area2D
onready var screensize = get_viewport().size
onready var gun_size = $CollisionShape2D.shape.extents * 2
onready var gun_x = position.x

Like before, the code above has not been tested.


Hopefully this helps!

Nice :)) Bouth methods are working. I prefer the onready one, more pythonish aspect. Is there a motive you avoid onready? Thank you guys!!!

@gaby424 said:

Nice :)) Bouth methods are working.

Great! I’m glad they both ended up working! While I was fairly confident they would work, I have had several times where what I thought would work with the GDScript interpreter was different from what actually works.

I prefer the onready one, more pythonish aspect. Is there a motive you avoid onready?

There is no particular reason, I just like to be explicit in my variable definitions.

I add semicolons at the end of statements and enclose my if statements in circular brackets despite neither really being necessary for GDScript. I suppose I’m just old fashioned in regards to my programming style :lol:

The only reason I could think of for that could make a difference with using onready or not is that it might make it a bit easier to transition from GDScript to a static typed language, like C++ for example. That is one reason I add semicolons and use enclose my if statements when I’m writing GDScript, but honestly I have no idea if it helps or not.

So yeah, there is no particular reason. I’d suggest using whichever style you like best!


Side note: As mentioned in this Godot QnA answer, onready assigns a value to the variable before the _ready value is called. It is basically short hand for assigning the variables in _ready, so either style essentially does exactly the same thing.