Referencing a variable of any data type

So, I am trying to make a function that will display a certain picture OR play a video depending on whether the variable passed to the function as an argument is a picture (data type PImage) or a video (data type Movie). I am kinda stumped on how to let the function work with both data types. (Some context: I am making an instructions screen for my game and when the player presses a next button, the next game topic to be demonstrated is drawn. That can either be a short clip or an image.) Here’s what I have so far:

void newInstructionSlide(<< pic or vid variable>>, String filename)
{
  String type; // to be defined later

  // takes the extension of the input file
  String extension = filename.substring(filename.length()-3);

  if (extension=="mov" || extension=="mp4")
  {
    type="movie"; // distinguish media as video
  } else if (extension=="png" || extension=="jpg")
  {
    type="picture"; // distinguish media as picture
  } else
  {
    // give error to console
    println("error distinguishing file type; double-check filename");
  }

  if (type=="movie")
  {
    (draw video)
  } else if (type=="picture")
  {
    (draw picture)
  } else
  {
    (throw error)
  }

Any help is appreciated! :slight_smile:

You could just use both in different methods. Like

void sameMethod(Movie movie) {
loadMovie();
}

void sameMethod(Image image) {
loadImage();
}

it overrides the other method depending on the data type inputted.
And if it’s just used like a branch method, then thats basically all you’ll need…

Btw, using the .getClass() method would be easier to identify the class of the file :wink:

1 Like

Processing.org/reference/String_equals_.html

1 Like

Okay, I will try to implement that into my game tomorrow morning. Thanks!

possibly should SPLIT on the “.”

String filename="picture_filename.jpg";
String[] fnameparts;
fnameparts = split(filename, '.');

//println(fnameparts.length);
if ( fnameparts.length == 2 ) {
  println("full name: "+filename+" name: "+fnameparts[0]+" ext: "+fnameparts[1]);
  if (fnameparts[1].equals("jpg") || fnameparts[1].equals("JPG") )  println(" + + + got a JPG file");
}