Variables of type Scanner in GD?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RetroDan007
:warning: Old Version Published before Godot 3 was released.

Hello my name is Daniel. And I speak Spanish sorry if there are errors in my question.
I have this program in java

import java.util.Scanner;

public class SueldoOperario {

public static void main(String[] ar) {
    Scanner keyboard=new Scanner(System.in);
    int HoursWorked;
    float Hourly;
    float Salary;
    System.out.print("Enter the number of hours worked by the employee:");
    HoursWorked=keyboard.nextInt();
    System.out.print("Enter payment for hours:");
    Hourly=keyboard.nextFloat();
    Salary=HoursWorked * Hourly;
    System.out.print("![enter image description here][1]The employee must charge:");
    System.out.print(Salary);
}

}.

Scanner in godot??? ¿

:bust_in_silhouette: Reply From: gungnirind

Scanner looks like it gets values from the command line. Since we’re using a GUI, we’ll need to retrieve values from the text fields.

Assuming you have your nodes set up something like this:

- foo
  - hours_worked_input_box
  - payment_input_box
  - salary_box

You could do:

# foo.gd

func calculate_salary():
    # get values
    # get_text() returns a string, so cast to int or float so we can use it
    var hours_worked = int( get_node("hours_worked_input_box").get_text() )
    var hourly = float( get_node("payment_input_box").get_text() )

    # calculate salary
    var salary = hourly * hours_worked

    # show salary
    # salary has to be cast into a string
    get_node("salary_box").set_text( str( salary ) )

or, you could check that valid numbers are input:

# foo.gd

func calculate_salary():
    # vars
    var hours_worked
    var hourly

    # get values
    var hours_worked_str = get_node("hours_worked_input_box").get_text()
    var hourly_str = get_node("payment_input_box").get_text()

    # check that input values are valid
    # if an input value is not valid, show an error and abort
    if( hours_worked_str.is_valid_integer() ):
        hours_worked = int( hours_worked_str )
    else:
        get_node("salary_box").set_text("ERROR: invalid input in Hours Worked.")
        return

    if( hourly_str.is_valid_float() ):
        hourly = float( hourly_str )
    else:
        get_node("salary_box").set_text("ERROR: invalid input in Hourly Wage.")
        return

    # calculate salary
    var salary = hours_worked * hourly
    # show salary
    get_node("salary_box").set_text( str(salary) )

Here’s an example scene I put together (made with GitHub master, but should work fine in stable):

Thank you for your answer… Muchas gracias amigo

RetroDan007 | 2016-03-01 12:14

Dude your example does not work missing engine.cfg

RetroDan007 | 2016-03-02 14:10