I'm trying to condense my code and make it as clean as possible.
I currently have three different objects that display recoil/shaking when shot by the player's bullets. Instead of having three events each with the same set of actions (but each changed to 1 of the 3 objects), there must be a way of condensing it (into a function?). I did try putting the 3 objects into a family and having the function affect the family, but it turns out this affects every member of that family. Here's a more concrete example with sine behavior providing recoil (could be done with shake or other behaviors):
When sPlayerBullet collides with sTree:
--Store sTree's X and Y positions in instance variables
--set sTree sine cycle position to 0
--set sTree sine inactive
--set sTree sine active
--set sTree sine magnitude to 3
--set sTree sine period to 0.2
--set sTree sine movement to horizontal
--wait X seconds
--Set sTree sine inactive
--Restore sTree to the stored X and Y positions
The same event is copy/pasted for sFlower and sBush, (but with the correct object) so it's the same set of code 3 times in a row. Is there a way that I can have this set of actions only once in the event sheet for efficiency? Even using function parameters, it seems that I would still have to copy paste the code 3 times, and a simple "or" condition would still have to specify which objects to act on. But ideally, it would look like this:
When sPlayerBullet collides with (sTree, sBush, or sFlower)
--Call function "Recoil"
On function "recoil"
--Store tree, bush or flower x and y positions in instances variables
--(same as above example)
--restore tree, bush or flower to x and y positions
I did try putting the 3 objects into a family and having the function affect the family, but it turns out this affects every member of that family.
Assuming that you pick the right object. Call the function with a parameter, in this case send UID of picked object in a parameter to the function call.
In the function you just pick family number by UID .. UID = function.Parameter(0)
Would this work even when I have multiples of each? For example, 10 bushes, 10 trees, and 10 flowers?
Develop games in your browser. Powerful, performant & highly capable.
On sPlayerBullet colliding with sTree (if sTree = de family)
.... this is a trigger .. meaning that it can fire several times in 1 tick and it should have the right sTree in the picklist.
So yes that would work. If you dont trust it (even my mind says that there are 1000 collissions happen at the same time) then do this.
(2 conditions in 1 event)
For each sTree
Call the function (with param)
'For each' loops trough the picked objects.
Then again. Fuctions are heaven to condens amount of code lines and (especialy) when you need to pick from scratch without destroying the current picklist. None of those 2 'rules' are in play now.
You condensed the code by using a family. So. Just.
All the shaking actions.
That is even less code because you dont need the code to call the function.