0 Favourites

We need a "Function call behavior"

  • 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...

  • 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.

  • Tylermon

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

  • 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.

    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!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • PixelRebirth

    [quote:2ouv2zg6]seems like you're asking for C2 events to be more similar to coding languages you appear to be familiar with.

    Uhhh yes

    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?

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • Tylermon

    There isn't really an issue, not exactly, just some coding and C2 objects functionality Q&A

    I was hoping for a way to have instance methods in C2. There isn't such a thing right now. I was hoping for an existing behavior that might provide this functionality. It doesn't exist. PixelRebirth made the point that calling onFunction events, passing in the instance.UID as first param pretty much duplicates the behavior I described in OP.

    The issue, if we in fact have one, is does the Object.PickByUID action iterate every instance to locate/return the one instance I want by it's UID. Or does it somehow directly select/Pick an instance in memory with the UID. If the latter, than onFunction(inst.UID, param1, parm2..) will be fine for now or maybe forever. If the former, I need to start thinking about building a custom behavior.

    Which of the following illustrates how Object.PickByUID works...

    // 1
    Monster.PickByUID(paramUID) {
        foreach(allMonsterInsts as m) {
            if (m.UID = paramUID) return m
        }   
    }
    
    // 2
    Monster.PickByUID(paramUID) {
        return allMosterInsts[paramUID]
    }
    [/code:24ajdh7c]
    
    If PickByUID action works like 2, then I'm fine with just using onFunction and passing in the UID. If it's 1, then we need something better like instance methods as I described in the OP. Or perhaps a way to pass an actual object instance reference into onFunction like 

    PixelRebirth said.

  • locohost

    For the most part, I feel pickByUID can be completely avoided by other events instead of using a function. I struggle to think of a case where pickByUID is necessary. Most animations can be determined by collisions, keypresses, or data values(health,stamina, etc). And a similar reasoning goes with many other concepts. Most, if not all pickByUID cases can be avoided altogether.

    My thoughts on object id's

    I would imagine they are sorted at O(n log n) merge sorted, heap sorted, or something similar. in which case finding and picking the correct uID would be rather quick. Although a bubble sort or insert honestly wouldn't be bad either O(n^2).

    If objects are not sorted though and construct is using something linear, I can see how we might come across an issue. An easy way to test this would be creating a project with a very large amount of objects then time how long it takes to get the first object and last object.

    Do this about 3 times with different object counts, and we should be able to tell if it is linear or not.

    Unless Ashley wants to tell us if object id's are sorted/picked faster than that

  • 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.

    Clearly he will say you are wasting time on micro optimization.

    The events are intentionally a subset of a traditional programming language. That is why they advertise "no coding required". For those that want to "do coding" that is what the JavaScript SDK is for.

  • locohost

    I requested object oriented methods 2 years ago. Ashley isn't convinced yet that doing so is worth while. Me I believe that object methods would improve coding efficiency and reduce poor event coding. Which is my main request why. However that's not enough.

    And I also suggest the blog post should be taken with a grain of salt. I know of a game where the basic optimizations Ashley listed just didn't cut it. However when we went into micro optimization for mobile. The 5fps mobile game jumped up to 60 after a month of micro optimization. Then there was a discussion of why Spaceblaster runs descently on mobile where as newb game X does not. Ashley went on to list numerous micro optimizations based on knowing how canvas and webgl render. And yet there are claims to not to bother to micro optimize.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
157 12,690
megatronx's avatar
megatronx
Unread hot topic
0 Favourites
[Plugin] Call Javascript
50 35,602
jailce's avatar
jailce
Unread hot topic
0 Favourites
I need a beta tester (or two)
69 5,950
alspal's avatar
alspal