Spawn another object (by name)

Get help using Construct 2

Post » Sun Dec 22, 2013 8:43 am

There is a way to make you go to another layout by just selecting the layout you want to go to in the code. There is also an option to type the name of the layout instead of selecting it. But, you can also type a variable in there, so you can change the layout where you want to go to using a variable. Is there a way to do it with Spawn another object?
This is useful when killing an enemy. When an enemy is destroyed, it spawns the object name that is written in an instance variable, aka it drops an item. Is there a way to do that or is it on the to do list
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

Post » Sun Dec 22, 2013 9:30 am

At the moment this is only possible with the third party plugin nickname.

You could also add a variable check on destroyed and based on the variable spawn an object, but if you want to directly spawn by name, the nickname plugin it is.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Dec 22, 2013 10:11 am

The only way I can think to do it would be like this (https://dl.dropboxusercontent.com/u/108799982/Capx%20Demos/Spawner.capx) by using a number, because as LittleStain said you can't default spawn from name.
Using R243 for all examples
B
69
S
20
G
6
Posts: 379
Reputation: 9,701

Post » Sun Dec 22, 2013 10:39 am

Another solution, from the manual:

====
Create object
Create a new instance of an object type on a layer at a given position. If a Family is chosen, a random object type from the family is picked, and an instance created for that.
====

So, add all your enemies to a family, and then spawn a new family member ;-)


G.
B
18
S
3
G
3
Posts: 83
Reputation: 3,736

Post » Sun Dec 22, 2013 10:48 am

@CJK

That will help spawning a random object, but I was under the impression topic-starter wanted the possibility to spawn a specific object by referencing it in an instance variable.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Dec 22, 2013 11:09 am

@LitteStain

True :-)

So, thinking about how I'd approach the "enemy drops an item" problem, I would try to:

1) Create an "Item" sprite, with separate animations for each item type (e.g. coin, dagger, bomb), and naming the animations appropriately
2) put the item type as an instance variable in an enemy
3) upon killing the enemy, spawn "Item" object, then set the animation to whatever I need - e.g. "coin".

Then, if your "Item"s have different functionalities, you can easily code these by checking what animation your "Item" is playing.

This allows easy adding new item types, just by adding new animations.

Cheers,
G.
B
18
S
3
G
3
Posts: 83
Reputation: 3,736


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests