How do I place a random sprite and retrieve it's value?

Just started using Construct 2? Post your questions here

Post » Fri Sep 12, 2014 7:28 pm

Hello Everybody,

Hi again a question from this newbie.
I guess the question is asked many times before, but I can't find anything about it.

This is my problem:
I want to place 'On Start of Layout' random (on the same X, Y spot in the game) only one sprite, from a collection, stored under the names K00, K01, K02 etc. (say in total about 30 sprites or so).
I guess I have to store all of them in a family?
I also want to retrieve (see, use) the Instance Variable 'Value' of each sprite that is shown in the game.

I can't figure it out. In all Events I tried I had to select one of the sprites by name.
I can't use the 'Choose (K00, K01, K02)' that one can use on the Action side of the Event Sheet.
I have the feeling I miss something simple, because nobody asked about it before.

Or is there a Plugin or Behaviour I should use and don't know about?

Sorry if it's too obvious, but I can't see the solution myself...

Many thanks in advance for the help,
be sure it's very much appreciated.

Philip
B
5
Posts: 14
Reputation: 260

Post » Sun Sep 14, 2014 3:15 am

Not sure I understand exactly what you want to do.

But if you make the 30 sprites that you need and add all of them to a family as you mentioned. Then you add a variable to the family itself called "Name" that will make it available for all the 30 sprites. Then you name all the sprites what you want "K00", "K01" etc.

Then you can select them using that name. Likewise if you want each of them to have a certain value variable you can add a value as well.

Condition:
Pick Family.name = "K00" - (Will select all sprites in the family that have this name.)

Action:
Set Result = Family.Value - (Will set something called Result to the value of the selected sprite (Family))

Regarding choose, im not certain if you can do the following as I haven't tried it, and I don't use choose that much.

Choose (Family.Name = "K00", Family.Name = "K01", etc.)
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sun Sep 14, 2014 4:14 pm

Dear Nimos,

Thanks so much for your time to help me, but I feel ashamed to say that it did not help me yet.
I am trying and trying and can't solve the puzzle yet.

I have the feeling I want to use a tool that's not in the box (yet).
But I also have the feeling that the missing tool I can't find is something that is very useful for everybody.
I guess in many games, game builders want to put a random sprite from a Family (or a list or a folder) of sprites on a certain place (X, Y)
in their game and also need to know it's Local Variable.

It could be useful for appearing random all kinds of things on a certain place to make the game more interesting,
as when every time the same object is showing up.
And of course the game builder needs to know the local variables of it too.

But I can't solve this, and again maybe I look over it and there is a simple solution for it.
But I am afraid I want something simple which is actually very complicated...

I Thought to choose on the 'Add Event' side: System, 'Pick random Instance'.
I could choose only one item so I choose Family.
One would expect it would pick a random item from the Family (the collection of my sprites),
but it always shows only the last one of the Family.

Than I tried 'Pick by evaluating' so I did choose Family again and filled in my Local Variable: Name.
But Nada.

So this is not the way...
I guess I am looking in the wrong direction...

Could anybody help me or just point me in the right direction?
Thanks again Nimos and everybody else who wants to explain this to me.

Philip
B
5
Posts: 14
Reputation: 260

Post » Sun Sep 14, 2014 4:39 pm

If your sprites are all basically the same, why not use animations instead of different objects and just set a random (or specific) animation after creating..

I do believe there is a third party plugin that does exactly what you are asking, I believe it's called nickname..
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 Sep 14, 2014 7:17 pm

Thanks a lot for the tip LittleStain, I downloaded it and will play with it. Great advise, now I feel I am moving to the right direction.
B
5
Posts: 14
Reputation: 260

Post » Mon Sep 15, 2014 3:14 pm

@Philip99 You could also use checks for a random UID (unique ID) or IID (instance ID; if you're spawning objects and run the check inside the condition they're generated, use this). If you're spawning multiples of one sprite, you can try:

System -> On start of layout
- (Sub-event) family_enemies -> Pick instance with UID (random(1,maxNumOfSpawnsHere))
---- Add events here for your power-up to apply to this picked instance.

If running the power-up check inside the main condition where the sprites are spawned, use IID and place the check for IID as a sub-event somewhere after spawning.
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests