WHEN... to use Functions ?

  • Hello All,

    This may be a very stupid newbie question but, I feel like the NEW Functions is sitting there while I'm working on my project and I never touched it since... I don't really know WHEN I NEED TO USE IT.

    If I understood, I need to use it for Re-Use multi-events so I can "call" the function at anytime, but I'm not sure if that's the case.

    Can you please give me a few simple-to-follow and understand examples of WHEN I should use a Function?

    Thanks ahead!

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you don't need it, don't use it ^^

    Anyway you could use it for repetition in your code.

    Completely idiot, but :

    my_sum( parameter: a, parameter: b ) { Return.Value: a + b }

    In you code, you use Function.my_sum(12, 23), which return 25.

    You can use when you compare, like

    SpriteX < Function.my_sum(Boss.X, Friend.X)…

    Easier view ?

    A good use, is for reset your game for example : at startup, you Reset(); when you clic Cancel, you Reset(); when you test after a key pressed, you can call Reset(); again…

  • Hey Alon,

    I'm not an expert, but whatever you are trying to build, you need to consider the mechanic of your project and everything in it as object (as much as possible).

    Each object share individual properties specific to them and other properties that they share together with, potentially, different parameters

    That's where function becomes useful.

    For example and theorically, if you have 2 sprites that can move but dont move at the same speed, you can design a function that handle moving them instead of writing twice the same code just because they don't move at the same speed.

    You will associate a parameter with this function (like speed) and when you will need to move one pf the sprite, you would call the function handeling the move, and send the parameter speed to tell the function the speed to use.

    This way you have only one piece of code usable for all your sprites that do the same job but accept variation/parameters in the way it does it.

  • Suppose you have a sprite collision and you want to know if there was an explosion and create it if there was.

    if you write a function that accepts two UIDs and call it doExplode. Now anytime you add a family or a sprite to the game you just say

    on collision alpha with beta

    doExplode(alpha,beta)

    do other stuff etc.

    you don't have to write the code for the explosion for every family and sprite, you just have it onc in the doExplode function.

  • Thanks for the examples you guys!

    It seems like I can really use Functions to make my code shorter in some case, not specifically on my current project but on the way I use Construct while learning how to use it.

    So.. since I didn't use Functions yet my questions may sound very stupid as a newbie but why not asking anyway from more experienced users in our community, it's another way to understand things.

    When I create a Function (I'm talking about the NEW functions in c3 which I didn't try yet).

    Are Parameters only a way to direct a variable? or are they actually a Variable that I can use without making instance variable for example?

    Sorry about my bad English, I'll try to explain my confusion:

    The parameters that I create on a Function, do I use them to connect or "talk" with my other variables (instance variables and global) or I'm not even close and they are for a different use.

    Sorry for the confusion, I should probably just play with the Functions first and do some tests to see how they work but since I'm curious I had to ask first. :)

    Thanks ahead again!

  • Hello again !

    Example I use for my current game : creating different background programmatically :

    1. Can create another background! Ad lib
  • Function parameters are an easy way to perform the same calculation using different inputs.

    For example:

    Assume I have a function that adds parameter+13 and returns the answer.

    I can call that function and pass say 8 as a parameter and have it return 21. If I pass 85 as a parameter, it returns 98.

    Here's an example of how I use a function with a parameter to switch layouts.

    * On function 'menuFunction'

    * Parameter 'MenuRequested' (Number)

    ----+ System: MenuRequested = 1

    -----> System: Go to Layout 1

    ----+ System: MenuRequested = 2

    -----> System: Go to Skills Layout

  • Thanks everyone for the examples that's encouraging me to explore the Functions and use them when needed instead of just letting them sit there! :)

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