Best way to spawn multiple different objects types?

Get help using Construct 2

Post » Mon Oct 03, 2016 1:02 pm

Hello all!
I have recurrent doubt when i try to spawn multiple different object types from the same spawner object. Usually i end with a long If Statements similar to a Switch Case. Something like:

FUNCTION On "SpawnCharacter"
For Each Spawnpoint
If SpawnPoint |"SpawnThisCharacter" = "Character01" >> Spawn <Character01> on layer "1"
If SpawnPoint |"SpawnThisCharacter" = "Character02" >> Spawn <Character02> on layer "1"
If SpawnPoint |"SpawnThisCharacter" = "Character03" >> Spawn <Character03> on layer "1"

The SpawnPoint Object has a text instance variable "SpawnThisCharacter" with the name of the character to spawn. And then i have to start the long list of possible characters to spawn. If, say, i have 120 characters possible to spawn then this method starts to sound bad to me.

How do you solve this kind of situations? Maybe it's the only way. This long if chains happens in other areas too and i'm almost sure there's something better to do. Maybe pass that ID variable as a function parameter. I'm kind of lost on this one.

If anyone have a better way please tell me what you do.

See you all! Thanks in advance.
B
31
S
4
G
4
Posts: 110
Reputation: 4,593

Post » Mon Oct 03, 2016 8:09 pm

I had a similar issue and ended up just using 1 object as my enemy unit and then at the start of the game created a array and added each 1 of my enemies possible stats to it and then when they're created had a function to load their stats and animation based on their name or ID creating them on the games first start looks like this

Image

I did this from the start so it wasn't such a big deal, however, adding this into an already developed project might be annoying since, I built everything around this concept, However, now it's very convenient because I can load units based on their name and get their stats pretty easily

Just an idea good luck with your project :D
B
12
S
8
G
3
Posts: 33
Reputation: 3,483

Post » Mon Oct 03, 2016 8:55 pm

There is a behaviour and a plugin to deal with the creation problem (both work together). It allows you to create an object by name/variable.
plugin-nickname-behavior-nickname_t74522

Give it a try. Its another, awesome, always updated plugin by @rexrainbow.

For the variables. The above method from Brandon12hummer sounds rock solid to me. But, for my lazy world, it is still a lot of work.

You can also make base dictionaries (yeah 120) and bring them in a container with the object. Now, every time an object gets created, its Dictionary gets created. Same for destroying. Pick the object, and the Dictionary is auto picked. That is gonna make a lot of Dictionarys , but you do not need to keep track of them, the system does. The debugger tells you wich Dictionary goes with wich object. Just an idea.

From there, you can take it a step further. Add them as .json files to your project. And load them to the Dictionary during start up with Ajax. Balancing the game is now a matter of just editing the .jason files.

A Dictionary is pretty easy. It is a key and a assigned value. You dont need to remember indexes, as in the array. The values are located at a key. The key is a string like "Health".
B
33
S
18
G
28
Posts: 2,474
Reputation: 20,912

Post » Mon Oct 03, 2016 9:06 pm

Yes, I end up with a bunch of parameters parsing wich get nasty pretty fast. Since this engine do a lot of picking and hidden loops maybe there was a way to get a specific instance without checking all against a desired variable result.

The only thing i don't like about using a single object type as a "container" of different types of characters is that i'm forced to load into memory all sprites, for all animations, for all the characters i use them or not.

Thanks Brandon12hummer for your input on the matter.
B
31
S
4
G
4
Posts: 110
Reputation: 4,593

Post » Mon Oct 03, 2016 9:08 pm

I make you a example later, tomorrow, day after. And i promise you, it will not have a lot of code.
B
33
S
18
G
28
Posts: 2,474
Reputation: 20,912

Post » Mon Oct 03, 2016 9:16 pm

@99Instances2Go I used rexrainbow's plugin to death but i want to understand the "normal" way to deal with heavy comparisons. Not only with creation or spawning but in any comparison in general.
For example i'm trying to check who controls a certain character, if it's player 1 the character moves and acts with mouse inputs. If it's player2 who controls the character then he moves it with gamepad inputs. This translate to checks and checks and checks to see if a certain function runs one way or the other.

In the end it seems there's nothing else than trying to keep function's parameters as ids and pick those given the context.
B
31
S
4
G
4
Posts: 110
Reputation: 4,593

Post » Tue Oct 04, 2016 9:03 am

That is true, in the end it is all picking and a little logic.

But, for the beginning C2 has a whole pack things in place to reduce the amount of code from the start.
Instances, families, containers, animations .... functions.

Each one and every enemy can be 1 sprite and its instances. You will be surprised how much lines of code (including functions) you will not have to write. Although, it is mainly copy and past with replacing objects.
B
33
S
18
G
28
Posts: 2,474
Reputation: 20,912

Post » Tue Oct 04, 2016 6:25 pm

After some thought.. I've done it in one event here : https://dl.dropboxusercontent.com/u/495 ... awner.capx

It's very basic and won't work if two different spawn points are spawning the same character. Maybe someone who has more time can expand it though.
B
45
S
15
G
73
Posts: 2,021
Reputation: 43,424

Post » Wed Oct 05, 2016 3:19 am

Nice quick method @plinkie. I don't know why your method won't allow a second spawn object to spawn a character. I cloned your spawnpoint and it worked fine (with additional events for it), but maybe i'm burnt for today and i miss something.

All this thing about spawners in the end is a question about how to make less concatenated "if" when i have yo check a state. I know it's a problem of not passing a needed parameter in the function that runs the event (besides the needed uid). Let's see tomorrow after a refactoring of my prototype.
B
31
S
4
G
4
Posts: 110
Reputation: 4,593

Post » Wed Oct 05, 2016 4:53 pm

In the end what my original question was all about was to get the best solution to solve the same problem this topic talks about:
https://www.scirra.com/tutorials/4843/p ... m-a-family

I always have the same problem, sooner or later i hit this wall, then i try to get around it with the first method mentioned (using functions parameters to pick a desired uid). Eventually this method becomes impossible to maintain as the project grows.

All my solutions to keep a desired instance picked through many states and conditional changes eventually creates a hell of events just to keep track of it. I'm looking for the best solution to always keep my desired instance picked without relying on countless evaluations. The above link perfectly shows my problem and the provided solution is just a workaround as it says. I need the "profesional" solution.
Some headaches can't be avoided i know, but i'm sure there's a better way to pick the instance i want and keep hold to it through the whole loop of interactions.

Obviously this problem only presents when i use families and i need to affect an especific instances of an especific object of the said family.

I take any advice to better grasp this picking thing.
B
31
S
4
G
4
Posts: 110
Reputation: 4,593


Return to How do I....?

Who is online

Users browsing this forum: alextro, ivanski1983 and 12 guests