0 Favourites

How do I ... Function(param)

  • Hi,

    Anyone can help me with a function?

    I try create function to buy buildings in my prototype game.

    For example:

    I have a global variable called GOLD, then I have buildings, each have a VarCoast variable ...

    Building1 - VarCoast = 1000

    Building2 - VarCoast = 500

    Building3 - VarCoast = 2000

    ...

    I want to create a function to subtract gold from my GOLD global variable.

    I try this:

    <font color=blue>

    • Mouse On left button Clicked on Bulding 1 >> Function Call "Buy"(VarCoast)

    ...

    • Function On "Buy"

    - GOLD => param(Varcoast) >> Subtract (VarCoast) from GOLD

    </font>

    <font color=red>

    BUT... How I get VarCoast to subtract to my global variablo GOLD?

    </font>

    I make this easily without make function. But I have to repeat the code for each building. I think a function let the code cleaner.

    Got the point?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Just add the COST variable inside the function call

    Mouse on left button   --> call buy(VarCost)

    Buy on call: COST set value to self - function.param(0)

  • Depends on if you used families are using a single sprite. If your using multiple sprites then coalesve them into a family. Then use a family attributes to store the cost. The worst thing you want to to do is use seperate sprites and seperate input.

    So assuming your using families

    On Click Object Building Family

    Function.Call("buy", building.uid)

    On Function "buy"

    Get Building by UID = Function.Param( 0 )

    Function.setReturn = -1 // just to note a failure to buy

    --if building.cost < GOLD

    ---- GOLD - building.cost

    ---- Function.setReturn = 1 // to know it's purchased

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