0 votes

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))
in Engine by (40 points)
edited by

1 Answer

0 votes
Best answer

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.

by (709 points)
selected by

thank you very much, it worked

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.