We need a "Function call behavior"

Discussion and feedback on Construct 2

Post » Sat Jan 03, 2015 3:15 pm

I'm really wanting to add methods to my objects. I'm certain you are too :) For now I'm faking it by creating appropriately named functions that take an instance UID as the first param. Is there a better way to fake this?

What we really need is a "object function" or "object method" behavior that we can add to any object that will call a specific function and automatically pass in a reference to the calling instance in a variable/param called "this". Now we can add this behavior to our Sprites (or anything really) and give it a reference to an existing function which act like instance methods. Then in code we call Sprite.ChangeAppearance(param1, param2, param...) and the function "ChangeAppearance" gets called and is passed a reference to the calling instance object as the first param, as well as each specified custom param.

This seems like such an obvious behavior. Has anyone coded this yet? I'll search a bit and hope...
B
14
S
4
Posts: 300
Reputation: 1,643

Post » Sat Jan 03, 2015 4:47 pm

The function object can already do this.

For instance you could say:

Player-on collision with enemy.
Call function takeDamage(enemy.strength)


On function takeDamage(param1)
PlayerHealth -= param1
PlayerAnimation=hurt

Also, iirc a function should remember the objects picked when it was called. I don't think you necessarily need to pass in uid's as long as you have correctly picked the object(s) you need.
B
28
S
8
G
1
Posts: 226
Reputation: 2,865

Post » Sat Jan 03, 2015 5:09 pm

@Tylermon

I don't think function call will remember picked objects (SOL), in C2. Each function call will start with picking ALL.
B
110
S
28
G
279
Posts: 4,487
Reputation: 156,066

Post » Sat Jan 03, 2015 5:22 pm

locohost wrote:Then in code we call Sprite.ChangeAppearance(param1, param2, param...) and the function "ChangeAppearance" gets called and is passed a reference to the calling instance object as the first param, as well as each specified custom param.


I honestly fail to see how this would be any different from simply calling a function "changeAppearance" and using the UID as the first parameter. It seems like you're asking for C2 events to be more similar to coding languages you appear to be familiar with.

So unless I'm sorely mistaken about some aspects of what you've brought up, all of this is already possible with C2 functions. It's a matter of it working differently than you are used to. Also don't forget you can always use the SDK and go ahead creating a behavior that might provide the usability you seek.

rexrainbow wrote:@Tylermon

I don't think function call will remember picked objects (SOL), in C2. Each function call will start with picking ALL.


As usual rexrainbow is right! :P
B
23
S
6
G
11
Posts: 1,047
Reputation: 8,065

Post » Sat Jan 03, 2015 5:46 pm

@PixelRebirth

seems like you're asking for C2 events to be more similar to coding languages you appear to be familiar with.

Uhhh yes 8-)

Aren't many of us Javascript programmers? If we're building any C2 plugins we sure need to be right? ;)

I will agree with you right now if the first line in my C2 onFunction, which is always SomeObject.PickFromUID, is super, super, instantly fast and does not have to search all SomeObject instances to find that one UID to Pick the instance. If the latter is true, then my suggested behavior will save eeeenormous amounts of time by passing a calling instance reference into the function. Follow? :)
B
14
S
4
Posts: 300
Reputation: 1,643

Post » Sat Jan 03, 2015 6:02 pm

@locohost

I have no idea how C2 handles "pick by UID" internally. But I will say that it would surprise me if this resulted in meaningful performance issues in most situations, even if it had to search all instances. Of course I can see how it wouldn't be optimal in any way.

I do recall though asking for a variable type that would hold an object instead of a string or number when C2 was still very young. The response basically was to use the UID to reference specific objects. That makes me hope that "pick by UID" is actually well designed and doesn't have to do unnecessary work.

And yes, if you're building plugins you sure need to be aware of javascript to a certain extent at least, hehe. But I don't think this applies to the majority of C2 users.
B
23
S
6
G
11
Posts: 1,047
Reputation: 8,065

Post » Sat Jan 03, 2015 6:09 pm

How can we find out how the Object.PickByUID action specifically works? If it has to iterate every object instance, it's pretty bad. And I agree, being able to pass object refs into functions would work perfectly. If it iterates all instances, I'll be forced into building some kind of behavior like I described in OP.
Last edited by locohost on Sat Jan 03, 2015 6:35 pm, edited 1 time in total.
B
14
S
4
Posts: 300
Reputation: 1,643

Post » Sat Jan 03, 2015 6:26 pm

locohost wrote:How can we find out how the Object.PickByUID action specifically works? If it has to iterate every object instance, it's pretty bad. And I agree, being able to pass object refs into functions would work perfectly. If it iterates all instances, I'll be forced to building some kind of behavior like I described in OP.


one solution would be to try to pick it (no associated actions) with an awful large amount of instances, and compare that to not doing anything.

even though I think it was pretty much direct IIRC.I would think that is what C2 actually does behind the scène everytime a picking is doen: get the list of selected objects, and for each pick by uid.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
22
G
18
Posts: 2,123
Reputation: 17,150

Post » Sat Jan 03, 2015 7:03 pm

Can we ask Ashley some how?

If Object.PickByUID gets a reference to the instance without iterating them all, then @PixelRebirth is right, just keep using onFunctions with the PickByUID action on param0. Otherwise, someone (me I guess) needs to look into building the behavior described in OP. Which will be time consuming.
B
14
S
4
Posts: 300
Reputation: 1,643

Post » Sat Jan 03, 2015 9:14 pm

Ultimately im a little confused what the issue is.
I haven't run into too many cases where I need to pass in the reference of the object to a function. Not to say the scenario doesn't exist but I think it is avoidable.

An example would be nice of when you need something done in a function and would have to pass the objectId.

The way I see it, many things object specific can be handled with families and events such as on collision, or on key pressed or on data comparisons etc. No functions needed.


Typically when I need to use a function it is dealing with data rather than objects. Either working with arrays and dictionaries or other things. Increasing experience, changing health. Changing how much money a player has, setting the game scale/size etc. For this functions work perfectly.
B
28
S
8
G
1
Posts: 226
Reputation: 2,865

Next

Return to Construct 2 General

Who is online

Users browsing this forum: tumira and 3 guests