How do I confirm an object was created?

0 favourites
  • 11 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • In most engines there is a way to create an object and then immediately know if it was created or not. I.e., if (createObject("spaceship") != null) etc. In Construct you can reliably create a family or object instance if you set it explicitly, but if you create an object by name there seems to be no way to know for sure that the thing you tried to create actually got created (or even exists, for that matter.)

    I tried making my own function to mostly do this, but it doesn't work. The logic for it is like so. I did it in event sheet logic, btw; this is just pseudo code.

    Local number startingObjectCount = 0
    Local number finalObjectCount = 0
    
    startingObjectCount = customobjectfamily.count;
    System.CreateObjectByName("MyObjectName");
    finalObjectCount = customobjectfamily.count;
    if (finalObjectCount > startingObjectCount) {
    	PickLastCreated(customobjectfamily);
    	return customobjectfamily.UID;
    } else {
    	return 0;
    }
    

    Unfortunately this always returns 0, I'm guessing because the object isn't actually created until the next frame? Does anyone know if there's a simple way to test at runtime whether the object type you're trying to create got created. I love creating objects by name because it's so powerful, but man it's annoying when there's a typo or something gets renamed and not all the calls that create that object type are manually updated too. Thanks.

  • Put "Wait 0 seconds" after you create the object

    This will wait 1 tick/1 frame, which will give time for the object to be created

  • Noah1 Thanks, great tip! I tried it, and it does work in a vacuum, in terms of confirming that the function created an object when that function was just called once.

    Unfortunately, waiting a frame to do that test breaks downstream logic that depends on immediately getting a valid UID back from the object's creation. But worse, if I'm calling the creation function multiple times in a frame, passing in different object names, I can't say which function calls succeeded and which didn't. At least not if I'm using logic that simply checks how many objects exist after the function call(s) and comparing that to how many existed before them. Maybe there's a different method I don't know about, or maybe there's just no simple way to do this in C3 yet.

  • Create by name has limited use to me because of that. But one solution is to make an “on created” for each type to set a variable. For example something like this:

    Var uid = -1
    
    On sprite created
    — set uid to sprite.uid
    
    On sprite2 created
    — set uid to sprite2.uid
    
    Start of layout 
    — set uid to -1
    — createByName(“sprite”)
    — if uid=-1
    — — set text to “no object created”
    — else
    — — set text to “new object created ”&uid

    On a side note wait 0 is a popular hack in relation to being able to create newly created objects. But I avoid if at all possible other than in quick tests.

  • Ah, thanks R0J0hound, that's a great idea. It seems so obvious now that you handed it to me on a silver platter, haha. XD

    And yeah, I am using create by name as a half-baked way of composing more complicated objects in a way that the pieces can be swapped in and out dynamically (a very poor man's component system, I guess.) So, I'll add some name variables or an array of names to an object, and at runtime I'll spawn objects with those names and add them to a hierarchy or manually store their UIDs. If I ever want to change them to another type, I can delete the instances, change the names, and spawn new objects. Since we don't have object or family type variables in this engine, I used this workaround, but it's also fraught with peril. It's so error-prone.

    Anyway, thanks again, much-appreciated.

  • I played around and here's another way

    Add all objects you'd like to check to a Family

    Then just check the Family.Count before and after creation

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Noah1 that looks very similar to what I'd tried, but I'll copy what you have verbatim and see how it goes, thanks!

  • Here's another solution that doesn't need any counting. Just a family, so it's potentially limited if you need to create a variety of objects.

  • Another another way - run in debug mode - just check how many of that object you have while you create more

  • winstreak Thanks for the suggestion! That would work in a test where I know exactly when things are created, but I'm talking more about confirming object spawns using event sheet logic so I can log errors and otherwise respond to an object that failed to spawn. I'm often spawning at unpredictable (and very fast) rates, so it's not always possible to manually monitor which spawns succeeded and which failed.

  • WackyToaster Interesting, thanks for the idea! That is generally what I do already - create a family-specific function to spawn arbitrary members of the family by name, so this test could slot right in. Much-appreciated.

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