Utility function to simplify picking

0 favourites
  • 10 posts
  • Here is an experiment to allow faster access to objects, arrays and instance variable without having to pick them.

    As a simple example say you have the uid of a sprite you want to get the x value of. You'd have to do:

    sprite: pick by uid 33

    sprite: x=200

    --- do something

    you could do

    system compare function: call("", 33, ".x") = 200

    --- do something

    Or maybe you have something more complex. like an array with uids of sprites, and each sprite has a hat object whose uid is stored in a hat variable. How do you compare the hat color of the sprite in array index 1 with the one in array index 5? and if they are different colors, make both 50 pixel tall?

    The solution probably involves a family or local variables to pick things one by one. It can be fairly complex and the readability of events is reduced.

    Anyways instead of doing that picking the normal way here's how you could do it with the utility function i came up with.

    System: compare function.call("", array.uid, "@", 1, ".hat", ".color") = function.call("", array.uid, "@", 5, ".hat", ".color")

    --- function.call("", array.uid, "@", 1, ".hat", ".height", "=", 50)

    --- function.call("", array.uid, "@", 5, ".hat", ".height", "=", 50)

    Or it even works well for something like arrays of arrays. Say you create it like this:

    global number rootArray=0

    start of layout

    --- set array size to (10,1,1)

    --- set rootArray to array.uid

    --- repeat 10 times

    ------ create array

    ------ function: call("", rootArray, "@", loopindex, "=", array.uid)

    It is a benefit to be able to avoid picking rules. Then you can access the 3rd element of the 2nd array with

    function: call("", rootArray, "@", 2, "@", 3)

    It's basically shorthand picking to get and set variables or values in an array.

    *Currently it doesn't work with text variables.

    *Error checking is minimal.

    * you need to add events to the function for every object type and property you want to support.

    here: the bare initial version.

    https://www.dropbox.com/s/igu4kum3t67nx ... .capx?dl=1

    Could be useful for complex data structures.

  • I got excited until I read this part:

    * you need to add events to the function for every object type and property you want to support.

    I see how this function can be useful in some projects, but I was hoping it would be more universal. (I.e. be able to access any property of any object, but I know this is not possible)

  • It certainly isn’t ideal to have to handle each object type and property, but the events to do that are very simple. Plus this is all in evens so changes can be made easily.

    A plugin probably can be made to access stuff too. The minifier is an issue though. Things like variables would be automatic and other properties like xy would need to done manually as well. Also it makes it tedious to make changes to the plugin. I actually have an idea for such a plugin but it’s way on the back burner.

  • One method you could try is something like the opposite of an object pool.

    You use the features of object creation with its implicit picking.

    Obviously it wouldn't work for everything, as you would need to destroy objects to make it viable, but you can manipulate as many objects as you need, one at a time.

  • I don’t think I follow your idea.

    I’m not going for something complex to use or implement. I only wish it could be made simpler.

  • When you create an object it is picked, so you can go on in and meander making things, and talking about them without having to pick them.

    It works well when you need to create a lot of new objects at one time, like in tile games, or bullet hells, or even particle like fx.

    Yes I agree that we could use something more to deal with objects that aren't picked by conditions. We do have indexing, but its still limited, as it can only work on certain aspects.

    Perhaps something similar to containers with an index, but without the disadvantages of only existing with the other objects.

    Like the family trick with indexing maybe. In that primis I think that it would be better served if such a thing had no conditions like a family, just references in actions.

  • newt

    You can create stuff without picking it by doing it in a function. This takes it a step further to provide a way to manipulate the object without picking, although it is often preferred to pick the object if you want to do a lot to it.

    Creating a lot of objects at once is better done with normal events than this, but it helps support it if you throw the uids in an array.

    Here is a second version of this with a test where it would possibly be useful.

    https://www.dropbox.com/s/ihkveelb5ljqw ... .capx?dl=1

    * It now works with text values as well as numbers.

    * Array indexes can be negative to access elements from the right instead of left.

    * The error checking was reworked so no if a property can't be accessed it will return "nil". That applies to arrays as well.

    That is better than silently returning 0 like C2 does by default.

    * It has a shortcut way to create arrays and dictionaries.

    I guess the next useful addition would be to add a function to load json data, or something similar.

    Edit:

    Next version is in the works and I’ve found a bunch of issues with this version. Debugging isn’t very trivial at this point, which makes it not super useful for others. It’s a fun pet project, I’ll see how far I can push it.

  • New version. Added a json parser that just creates a bunch of c2 arrays and dictionaries.

    Also fixed some mistakes and made a console with better error feedback.

    https://www.dropbox.com/s/0lq0i3diuz3db ... .capx?dl=1

    I guess the biggest disadvantage of this is the programmer has to keep track if a value is a uid or just a number.

  • Dude..... rojohound I know if Mr Rojohound is doing something it has to be interesting ...... and usually I can just about wrap my head around what you are doing.....

    but my brain cells are rapidly deteriorating in recent years due to excessive alcohol consumption and I've looked through the events and run the cap but this one has me stumped ....

    i know you have explained to newt but if you dont mind .... for the layman .... what am I looking at, what can it be used for, why cant this be done with normal events? I feel like I/we am/are missing something special here.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • NetOne

    No worries. It’s basically a way to access objects like a programming language such as JavaScript or c++ would.

    Maybe useful when using some data structure more complex than a single array or dictionary.

    I need to attempt using it for something to see if it helps or is more tedius than using a bunch of picking tricks.

    For now it’s kind of cool that I can create a bunch of arrays and dictionaries linked together by uids and be able to access things with a single line.

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