How do I ... Function(param)

Get help using Construct 2

Post » Fri Jan 17, 2014 2:11 am

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:

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

...

- Function On "Buy"
- GOLD => param(Varcoast) >> Subtract (VarCoast) from GOLD


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

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?
B
24
S
4
G
2
Posts: 96
Reputation: 2,467

Post » Fri Jan 17, 2014 4:40 am

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)
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Fri Jan 17, 2014 4:54 am

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



B
90
S
18
G
9
Posts: 2,455
Reputation: 15,028


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, Phacanu, richcoz81, Txchaser and 34 guests