Can't I create Functions that apply to "picked"?

0 favourites
  • 10 posts
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • Hi all,

    I want to create a Function that can be applied to different objects whenever it is called.

    For instance, in pseudo code, here's something simple I can't seem to accomplish with Construct 2. First I would create a Condition->Action to call a Function using a specific object as a parameter:

    On Mouse Clicked Object

    \--> call Function "MyResizingFunction" (Mouse.clickedobject.UID)

    The Function would be something on the lines of:

    On [MyResizingFunction]

    |--> System.PickObjectByUID(Param0)

    \--> PickedObject.setsize = PickedObject.size*2

    ---------------

    So, I hope I explained it simply enough. Basically I want to click on any random object (Sprite) and have that object double size each time it is clicked on.

    I was excited about the possibility of using Functions, but after trying some stuff, I can't really say I get the point, if I can't apply Functions to different objects (Sprites).

    Please share any thoughts on this.

    Thank you very much!!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • gamekill - i made an example. i figure this could all be simplified using families but i don't (yet) have a license for C2 so this is another way to do it without using families. there are notes in the capx about families.

    hope this helps!

    dropbox.com/s/cw24j2xnngc24ub/sizeclicking.capx

  • Wretchedshark, thank you for your reply, I really appreciate it and your time. The problem is, this solution is not really what I need..

    You see, imagine I create a game with 125 different types of objects. It would be impractical to create one event condition for each one of them. What's worse, changing and adding objects to the project would be a pain.

    I guess in this specific case (simple size changing), you could use Families (as you already mentioned) to simplify the process.

    But my real problem lies with Functions.

    I intended to use Functions for more complex stuff, say for an example:

    If my character touches a magic statue, he temporarily gets a specific power, depending on the touched magic statue. Then, Every object in the world that he touches becomes the same color of the magic statue, and loses half the height of the previously touched object.

    Etc etc.

    This could all be achieved easily with traditional coding languages Functions.

    Any insights on this, anyone?

  • gamekill

    You could do that with families easily. Or you could use little sprite trick where you store different objects in different animations. Here is a little demo I made to demonstrate.

  • Here's an example project for you.

    http://imgur.com/0FjIfaT

    Example File

    dl.dropbox.com/u/403350/functionExample.capx

    Just like your pseudo code

    You store the UID of the clicked object, and pass that as a parameter to your function. Then you "Pick" all instances that match the same UID as the parameter you passed (originally clicked on) and only apply the actions to that specific matching instance

  • justifun

    That's just about the exact of what I need, but I guess there's no way to accomplish it in a simple manner if I don't use Families, right? I was trying to avoid them because I teach Construct at a local University, and not all my students have access to Construct 2 Full.

    Is there a way to reference "any Sprite"?

    That way, I could create a local variable for the objects I want to be double-sized when clicked, say a variable called canDoubleSize. And I could check for that variable on my mouse-click then call the double-size function.

    on mouse clicked on "any sprite whatsoever"

    \--> AnySprite.canDoubleSize = 1

    then do what your example does, but sending the UID to a global variable. Then the function searches for "any Sprite" with that UID and runs on it.

    I hope I explained it clearly..

  • justifun

    That's just about the exact of what I need, but I guess there's no way to accomplish it in a simple manner if I don't use Families, right? I was trying to avoid them because I teach Construct at a local University, and not all my students have access to Construct 2 Full.

    Is there a way to reference "any Sprite"?

    That way, I could create a local variable for the objects I want to be double-sized when clicked, say a variable called canDoubleSize. And I could check for that variable on my mouse-click then call the double-size function.

    on mouse clicked on "any sprite whatsoever"

    \--> AnySprite.canDoubleSize = 1

    then do what your example does, but sending the UID to a global variable. Then the function searches for "any Sprite" with that UID and runs on it.

    I hope I explained it clearly..

    Check the example I posted earlier, you could have all your sprites 'inside' same object to simulate families.

    To me it seems you don't really need UID's, functions or anything like that. It could be as simple as having that canDoubleSize variable, then on certain conditions you execute something like this:

    For each sprite

    sprite.canDoubleSize = 1

          Do the doublesize

    This is shown in the demo I posted as well.

  • vee41 Yes, I guess you are right.

    For the example I posted (double the size) we can do away without Functions etc.

    And sorry, I forgot to comment on your earlier post. Your idea about several objects inside animations of a single object is a really useful thing to do.

    But for complex actions I could use Functions. Like a function called "turn it into a nemesis" could double the object's size, restore it's HP, give it a powerful weapon and attach it to him, start showing it on the minimap, and point the player in its direction.

  • But for complex actions I could use Functions. Like a function called "turn it into a nemesis" could double the object's size, restore it's HP, give it a powerful weapon and attach it to him, start showing it on the minimap, and point the player in its direction.

    Absolutely, that kind of stuff is where functions become useful. As for calling function with picked UID's you could do something like this:

    text variable UIDs

    pick sprites you need

    for each picked sprite

         append sprite.uid & ";" to UIDs

    call "Function" (with UIDs as function parameter)

    "OnFunction"

    for 0 to tokencount(function.param(0), ";")-1

    pick sprite with UID tokenat(function.param(0), loopIndex ";")

        do your thing

    What this basically should do, is save all selected UID's into text variable separated by ; symbol. Then inside the function you can parse the string again to pick them one by one.

    I still think C2 offers much simpler ways to do stuff like this, it feels very 'coderish' way to do stuff! :) C2 allows for higher level of design and I really have to force myself out of the coder mold when using C2 to maximize it's potential.

    You'll still have a problem if you can't use families, so you'll need to have all the sprites in single object. Hope this helps :)

  • Yes this does help, never thought about using Tokenat for a loop.

    I guess I need to practice more on thinking differently on each new project, to come up with stuff like this.

    Thanks

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