How exactly does spawning work?

0 favourites
  • 9 posts
  • Hello

    I'm working on a space invader game and I've ran into a couple of problems while trying to spawn ships and fill in values for their variables depending on the model. Here's a bit from my script.

    I have to spawn multiple models of ships, depending on what the player chooses but for now it's just one. I've set some commands to assign values to some of the ship's variables but only a few essential ones. The rest I want to set using the Ships | On Created event to save script but I've only added a few for now. Note that FighterShip is part of the Ships group.

    The problem is that the commands of the On Created event seem to trigger before the commands on the first event. Either that or the commands on the first event only trigger a frame after FighterShip is spawned. To test this I put in the NumTest command and event. In debugging I saw that even though NumTest had been set to 1 the ship's text variable "HeavyGun" was not set to "No". I take this to mean that NumTest was not 1 at the time when On Created was triggered but only later.

    Am I missing something here? Does anyone know exactly the order by which these events run? Does spawning a sprite require 1 frame before it can be accessed by Events and Commands?

    My other problem is making a dynamic script for spawning multiple types of objects. I wish I had a command where I could type in the name of the object to be spawned or put in a variable but there's clearly no such thing. Instead I have to do a separate event for each type of ship. If the variable that determines the ship model is FighterShip, spawn FighterShip, if the variable is set to TraderShip, spawn a TraderShip and so on. I wish I could just have a command that says Spawn Object "ModelVariable" to replace all the ifs because there will be a lot of models.

    I tried the trick of spawning a Ships family object instead but it only chooses a random object from the family to spawn and I can't control it. I tried to have an event select one FighterShip and then spawn a Ships object but it still spawned random models, not just the FighterShip. Does anyone have a better solution than making a long list of events, one for each model?

    Any help would be greatly appreciated as I still have a lot to learn.

  • The nickname plugin might help you.

    There are however some tricks you could do if you prefer the vanilla way. You could create a function named "CreateShip" that receive a string parameter, and using a long list of "if else if...", create the correct ship, and set the return value to the newly created ship. Whenever you want to create a ship, use that function, and use a "Pick by UID" on the ShipFamily with the return value to change its family instance variables.

    I once did an example of something similar.

    And as for the "On created" issue, the trigger is indeed called before the remaining actions. This is by design, so you will have to take that into account when you use this event.

  • The might help you.

    There are however some tricks you could do if you prefer the vanilla way. You could create a function named "CreateShip" that receive a string parameter, and using a long list of "if else if...", create the correct ship, and set the return value to the newly created ship. Whenever you want to create a ship, use that function, and use a "Pick by UID" on the ShipFamily with the return value to change its family instance variables.

    I once did an of something similar.

    And as for the "On created" issue, the trigger is indeed called before the remaining actions. This is by design, so you will have to take that into account when you use this event.

    I see, thank you. It seems that's what I will end up with, a long list of ifs. The problem is that I intend to have tens of models but I have split them into categories and each sprite will represent a category and hold every model of that category as a separate animation. We'll see if that strains memory too much. The plugin sounds good but I'm not fully aware of what using one implies. If the plugin stops being updated along with Construct, do I run the risk of it not being compatible with the latest versions at some point?

  • The plugin being made by rexrainbow you can safely assume it will never break, and even if it does, it'll get fixed in no time. So no worry there.

  • The plugin being made by rexrainbow you can safely assume it will never break, and even if it does, it'll get fixed in no time. So no worry there.

    That's good to know. I should give it a try. Thanks again.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I did something along these lines but instead of the

    if instance variable = x create xx

    if instance variable = y create yy

    etc

    I said

    required xx

    create family

    does family = xx then great

    else

    destroy and re call function

    so it loops through until it gets the required type might require several loop might get it first time...

    I can dig it out if you want to have a look ?

  • Dug out, tidied up and commented. This might be a terrible way of doing it because of looping through the code until it finds the correct member of the family but in my tests it appears to work quite well and saves quite a lot of code.

    https://goo.gl/GJSPIR

  • Dug out, tidied up and commented. This might be a terrible way of doing it because of looping through the code until it finds the correct member of the family but in my tests it appears to work quite well and saves quite a lot of code.

    Now that's creative . Thanks for the idea. I'll have to try each solution and see which works best for me.

  • Dug out, tidied up and commented. This might be a terrible way of doing it because of looping through the code until it finds the correct member of the family but in my tests it appears to work quite well and saves quite a lot of code.

    https://goo.gl/GJSPIR

    In an Utopian world of infinite memory and processing power, this would be the best solution. However, we're not quite there yet, so in the meantime it's better to limit the memory allocation to a minimum when you can. <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

    It could still be functional if the family has very few object types, but with each new type, the odds of NOT instantiating the wanted one increase drastically.

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