How to return variable after calculation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 0sait05

How do I return variable to the other if statement?
I want x z to be zero and y to be five for all if statement if "reset_camera" is pressed

var y = -5
var z = 0
var x = 0
export (NodePath) var camera_reset_cupdown
onready var building = get_node(camera_reset_cupdown)
func repeat_me():
	if Input.is_action_pressed("reset_camera"):
		building.set_translation(Vector3(0,0,-5))
	if Input.is_action_pressed("reset_camera"):
		Bitemnode.set_translation(Vector3( 0, 0 ,-5))
	if Input.is_action_pressed("pos_up"):
		y += 0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	if Input.is_action_pressed("pos_down"):
		y-=0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	if Input.is_action_pressed("pos_left"):
		x+=0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	if Input.is_action_pressed("pos_right"):
		x-=0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	if Input.is_action_pressed("pos_high"):
		z+=0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	if Input.is_action_pressed("pos_low"):
		z-=0.1
		Bitemnode.set_translation(Vector3( x, z ,y))
	
:bust_in_silhouette: Reply From: gmaps

You don’t have to “return” it, just set the variables like this:

func repeat_me():
if Input.is_action_pressed("reset_camera"):
    building.set_translation(Vector3(0,0,-5))
    Bitemnode.set_translation(Vector3( 0, 0 ,-5))
    x = 0
    y = 5
    z = 0
if Input.is_action_pressed("pos_up"):
    y += 0.1
    Bitemnode.set_translation(Vector3( x, z ,y))
if Input.is_action_pressed("pos_down"):
    y-=0.1
    Bitemnode.set_translation(Vector3( x, z ,y))
if Input.is_action_pressed("pos_left"):
    x+=0.1
    Bitemnode.set_translation(Vector3( x, z ,y))
if Input.is_action_pressed("pos_right"):
    x-=0.1
    Bitemnode.set_translation(Vector3( x, z ,y))
if Input.is_action_pressed("pos_high"):
    z+=0.1
    Bitemnode.set_translation(Vector3( x, z ,y))
if Input.is_action_pressed("pos_low"):
    z-=0.1
    Bitemnode.set_translation(Vector3( x, z ,y))

But I suggest that you refactor your code to this, or something like that:

var y = -5
var z = 0
var x = 0
export (NodePath) var camera_reset_cupdown
onready var building = get_node(camera_reset_cupdown)
func repeat_me():
    if Input.is_action_pressed("reset_camera"):
        building.set_translation(Vector3(0,0,-5))
        x = 0
        y = 5
        z = -5
    if Input.is_action_pressed("pos_up"):
        y += 0.1
    if Input.is_action_pressed("pos_down"):
        y-=0.1
    if Input.is_action_pressed("pos_left"):
        x+=0.1
    if Input.is_action_pressed("pos_right"):
        x-=0.1
    if Input.is_action_pressed("pos_high"):
        z+=0.1
    if Input.is_action_pressed("pos_low"):
        z-=0.1
    Bitemnode.set_translation(Vector3(x, z ,y))

Note: the function is just an example how to do it, I didn’t test it or anything. But I think that your code has an issue; why do you set Bitemnode translation after each sentence, you should doo that at the end, once. Otherwise every next set_translation will rewrite the previous one. The best way is to calculate the final x, y, z translation, and apply it at the end, once.

thank you very much, it worked

0sait05 | 2019-10-04 00:33