How do I return multiple values in a function?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
Game "Little Dino Adventure Returns" with complete Source-Code (Construct3 / .c3p) + HTML5 Exported.
  • I would like to return X/Y coordinates in a function, but C3 only seems to allow one return value per function. How do I return pairs, vector2? Or is it possible to return an array or list?

  • Alternatively, you can concatenate all the required values into a delimited string. And then parse and convert to a number.

  • You could also have the function return nothing and set some global variables instead that you’d use after calling the function. For example:

    Global number retx=0
    Global number rety=0
    
    Function rotate90(x,y)
    — set retx to -y
    — set rety to x
    
    Every 1 seconds
    — function: rotate90(sprite.platform.vectorX, sprite.platform.vectorY)
    — sprite: platform: set vectorX to retx
    — sprite: platform: set vectorY to rety

    You can do that with anything. Instead of returning a value you just save the result somewhere. Or if you want it to be more dynamic you could say, create an array, fill it with values and return the uid of the array. There are tradeoffs for readability and ease to use though.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Using global variables for setting individual instances seems like a bad practice, prone to bugs. It would work only with one object, or rest of the code must be in sync very well.

    Your solution works better if the global variables were instance variables instead, and instance ID is passed to function. Doable.

    I also thought about parsing string, but I have had to done this before and this can be come very messy really quick, ugly.

  • I will suggest, that encoding multiple values in a string just creates more problems (parsing and extracting types).

    The suggested methods are common ways to return multiple values in a language without simple tuple syntax (tuple means arbitrary number of grouped terms, not just 2). I would suggest the standard way of returning a complex typed object.

    MyTypedObject returnVal = {

    int x: 0,

    int y: 2

    };

    This gets messy when you have lots of functions trying to return multiple values, because now you have a whole bunch of types to namespace and modify when you want to make changes.

    A tuple substitute is implementing a kind of intentionally-formed complex object. This gives birth to the "wheel" or whatever you want to call it.

    Let's say we have another function where we want to return a string and an int. Modify the wheel to allow those values to be carried.

    // MyTypedObject becomes Wheel - or ad-hoc tuple

    Wheel returnVal = {

    int x,

    int y,

    string z

    };

    Now I can use the wheel to return multiple values across multiple functions. You do have to be careful to ensure that only set returned value fields are used, but it makes the code MUCH cleaner when used sparingly. Generally, if you are returning more than a handful of fields, you want a dedicated SomeTypedObject, but for simple things the Wheel will do.

    };

  • How about setting both values in one function, then calling each with its own function? Something like this:

    Here I put everything in a group, sort of encapsulating the functions together. Then you can call the first function once without a return value, followed by each of the return values:

    A silly example but one that hopefully illustrates the idea.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)