How do I have functions execute in the same context they're called?

0 favourites
  • 13 posts
From the Asset Store
Medeival & Viking War Horns Sounds / 22 Unique Calls / 2:40 minutes of audio
  • When I call a function from within an event, it doesn't use the event's filtering context and so selects all types.

    https://drive.google.com/file/d/1NI-hMiXB-6pyWLQ3UQFIbpZn9nSx7UnC

    How should I call functions?

  • If you need to pick the same object instance in the function, you need to pass instance UID (or some other identifier) in function parameters:

  • I really hoped that there was a better solution than that. That means it needs to be called in a for-each loop to have it operate over a set of sprites.

    Thanks though, I'll stop looking for the correct way to deal with them.

  • It's actually quite useful sometimes that the function doesn't pick the same scope of instances. Imagine when one Text instance is clicked you want to make it larger and all other Text instances smaller. You can do this:

    On Text clicked 
    	-> Call Function "MakeTextSmall"
    	-> Text set font size to 20
    
    Function "MakeTextSmall"
    	-> Text set font size to 12
    
  • That could be achieved regardless though, couldn't it?

  • In that simple example - yes, but there are situations when you need to pick one set of instances in an event and then do something with a different set of instances without losing the first picked scope, and it's easier to do with a function.

  • If there's multiple instances you want to target then a function can't do that anyhow because it can only accept string, number and boolean types. Wouldn't you have to put it in a loop and call it multiple times?

  • You can avoid a loop by setting an boolean instance variable that can be used to pick the objects in a function. A caveat is you have to set the boolean to false for everything first.

    every tick
    --- sprite: set picked to false
    
    some conditions to pick a certain group of sprites
    --- sprite: set picked to true
    --- function call "foo"
    
    on function "foo"
    sprite: is picked
    --- do something
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • At this part:

    some conditions to pick a certain group of sprites
    --- sprite: set picked to true
    --- function call "foo"
    

    You are using a loop, isn't this just a longer way of writing:

    some conditions to pick a certain group of sprites
    --- sprite: get UID
    --- function call UID
    

    If the function kept filtering context, you could still do this because the filtering doesn't apply to the Pick by UID statement.

  • There isn't a loop. Here's a specific example. It passes the filtered sprites via an instance boolean. The function is called once.

    every tick:
    --- sprite: set picked to false
    
    sprite: x>100
    sprite: x<200
    --- sprite: set picked to true
    --- function call "foo" ()
    
    on function "foo"
    sprite: is picked
    --- sprite: rotate 100*dt degrees clockwise

    The uid and loop way to do the same thing. The function is called once per instance. Any if you want to pass a newly created instance you're pretty much forced to use this way.

    sprite: x>100
    sprite: x<200
    for each sprite
    --- function call "foo" (sprite.uid)
    
    on function "foo"
    sprite: pick by uid function.param(0)
    --- sprite: rotate 100*dt degrees clockwise

    With such a simple example it's irrelevant which way to use. I can see the value of being able to pass what's been picked, but that's pretty much a valid way to do it. Functions only pass values for the foreseeable future from what I can tell.

  • I see what you mean now. You're right, there isn't a loop, everything stays set based. It's not as horrible as I first thought.

    Still, I'd like to be able to pass the current filtered context into the function if I wanted to.

  • It's something that could indeed be useful, you ought to suggest it on the C3 suggestions & ideas platform, if it's not already there.

    construct3.ideas.aha.io/ideas

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