Passing information into functions

0 favourites
  • 7 posts
  • So I'm struggling a bit with how to make versatile functions by passing them different information types.

    For example, consider the following function:

    updateNumberAndText(x,y,z)

    step 1: add y to global parameter x

    step 2: update text element z to display the value of x

    I know how to pass the y value by using a local numerical variable, but how do I pass which global parameter (x) to change? and how do I then pass which text element (z) to update?

    (Clarification: I want to be able to change x and z every time I call the function from within the game)

    Thanks! :-)

  • I can see why you are struggling with this. There really isn't an easy way to reference a global variable indirectly. There is 1 way I was able to come up with for you however. Try this out:

    drive.google.com/file/d/1HXei-9dZKNK7auwO9zIhSobVDIKQ4U_y/view

    I believe this will do what you are asking for. However, it could very quickly get out of hand if you have a bunch of global variables. You need 1 event per global you want this function to change.

    On a side note, why are you using global variables for this? Do you need the values to persist across multiple layouts? I only ask because I see many newer users using global variables like they were local variables. If these values don't really need to be persisted from 1 layout to the next, you could cut down the number of events necessary. For instance, instead of using globals, you could store the values in an array. Then, you would pass the index of the value you want to modify instead of the name of the variable. This would take the above example from 4 events in the function to just 1. In fact, this would even work if you do need these values to be persisted because the array is considered global if I remember right.

  • An alternative is to return the total and set it back to the global passed in.

    FunctionReturningTotal_MUTAJON.zip

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • also if you are using C3 you can have the function return a value in the actual System Compare Event like this:

  • You can access variables and objects by string name using scripting.

  • dop2000

    Thanks! and how do I reference elements using scripting?

  • I believe it's runtime.objects["objectname"].getFirstInstance()

    Or you can add all objects into a family and pick Family instance by evaluating Family.ObjectTypeName="objectname" using "System pick by evaluate" condition

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