How exactly does spawning work?

Get help using Construct 2

Post » Wed Nov 04, 2015 5:02 pm

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.

Image

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.
B
7
S
1
Posts: 29
Reputation: 483

Post » Wed Nov 04, 2015 5:21 pm

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Wed Nov 04, 2015 5:31 pm

Magistross wrote: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?
B
7
S
1
Posts: 29
Reputation: 483

Post » Wed Nov 04, 2015 6:34 pm

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Wed Nov 04, 2015 6:49 pm

Magistross wrote: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.
B
7
S
1
Posts: 29
Reputation: 483

Post » Wed Nov 04, 2015 9:39 pm

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 ?
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Thu Nov 05, 2015 9:46 am

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
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Thu Nov 05, 2015 2:15 pm

RamPackWobble wrote: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 :D. Thanks for the idea. I'll have to try each solution and see which works best for me.
B
7
S
1
Posts: 29
Reputation: 483

Post » Thu Nov 05, 2015 2:44 pm

RamPackWobble wrote: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. ;)

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 19 guests