Generate random number for loot drop

Get help using Construct 2

Post » Sat Sep 07, 2013 2:41 am

I searched the forums/google and found several "advanced" examples, but none of them are written from a new user perspective.

I would like to have an enemy die, then have a chance to drop an item.

I was hoping this could be done at the local variable, so I set my enemy to have a LootVar of 0. When it is destroyed, I have it setting LootVar to round(random(1,3)).

Now how can I add in the next step of checking IF LootVar is 1 then create Loot Item here?
B
6
Posts: 12
Reputation: 274

Post » Sat Sep 07, 2013 2:48 am

Well there are many ways to do this. What I would suggest is that you set two variables. One could something like "ActionNeeded" and set it to true. So if "ActionedNeeded" is tru then check any other variables that may require attention.

For example, "Loot", "SecretDoor" "UserMessage" etc

if any action/event requires processing you set the variable "ActionNeeded" which gets check either, every tick or once every 0.5s3econds or whatever and then process accordingly.
B
37
S
9
G
5
Posts: 437
Reputation: 6,094

Post » Sat Sep 07, 2013 3:00 am

Thanks for the fast response, CZAR.
Having a hard time wrapping my brain around what you mean.

Make a global variable named ActionNeeded (or something), then set that action to 0 when something dies?

Then what?

Is there no way to just check a local variable? In the SYSTEM actions it says global and local variables, but when you try to use it to call or check a variable it only allows you to select globals.

It seems like an option within the "destroy" object action should have the loot roll and obj generation.
B
6
Posts: 12
Reputation: 274

Post » Sat Sep 07, 2013 4:59 am

I am trying to do something along those lines OP. see: http://www.scirra.com/forum/topic75112.html where I try to get help. So far, I'm just using what seems to be an inefficient workaround.

If you wanted a simple implementation, the way I would do it is like this:

Trigger: On Death
-> select DeadEnemy| Action: set lootVar to round(random(1,3)
        -> if lootVar = 1|Action: DeadEnemy: create Loot1
        -> if lootVar = 2|Action: DeadEnemy: create Loot2
        -> if lootVar = 3|Action: DeadEnemy: create Loot3

As in my own problem however, the more Enemy types you have and the more Loot types you have, you'll have to find another way of doing it or you'll end up with a whole lot of events. Anything beyond the simplest Function is beyond me right now, so can't help there. Also when you want different chances for different loot (common, rare, etc), as I did, things become even more complicated.
B
13
S
3
Posts: 17
Reputation: 984

Post » Sat Sep 07, 2013 5:40 am

[QUOTE=Gagaween]Is there no way to just check a local variable? In the SYSTEM actions it says global and local variables, but when you try to use it to call or check a variable it only allows you to select globals.[/QUOTE] Something's wrong in your event sheet. Might want to share it?

Here's an example using local variable:

[code]
+ System | On start of layout
    Local number lootVar = 0
    + (empty event)        -> System | Set lootVar to round(random(1, 3))
                             Text   | Set text to lootVar
    + System | lootVar = 1 -> Text | Set text to "loot"
[/code]

If your event block doesn't look similar, you've done it wrong somehow.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sat Sep 07, 2013 6:08 pm

Thanks for the replies, guys.

Norbingle, where is there an option to set "IF" commands?

Tulamide, I see no way for System to check local variables (as I said earlier). Could you elaborate your suggestion?
B
6
Posts: 12
Reputation: 274

Post » Sat Sep 07, 2013 6:29 pm

I did understand what you said earlier and therefor showed how local variables are used. The "check local variables" is the last line in my example (System | lotVar = 1). As soon as the local variable is created it can be used for comparison.
But it is crucial to understand the "philosophy" (sorry, I lack the right words here) of variables.

There are three different kinds of variables in C2. Global, local and instance. They differ mainly in their scope.
A global variable is valid all over the layout, visible and usable for every event and object.
An instance variable can be accessed from anywhere in the event sheet, but is not unique. It shares its name (and access rule) with all other instances, and lives with an object.
A local variable is only generable within subevents and only lives for the time the subevent is executed. So to access it you need to create a subevent in the same subevent level, where the local variable was created.

Start of layout
Local variable
Local variable = some value -> do something

Trying something like this:

Start of layout
Local variable

Enemy died
local variable = some value -> do something

is impossible. The local variable created in Start of layout doesn't exist anymore when "Enemy died" happens. Therefor you can't find any option to compare the local variable of "Start of layout" in the "enemy died" event.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sat Sep 07, 2013 6:39 pm

@Gagaween

Within the sub-event where you're randomly creating LootVar, you can use System > Compare Variable and the Local Variable LootVar should be selectable.plinkie2013-09-07 18:42:05
B
47
S
17
G
82
Posts: 2,230
Reputation: 48,607

Post » Sat Sep 07, 2013 6:57 pm

@plinkie
I'm jealous now. All of my novel summed up in one sentence.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sat Sep 07, 2013 7:27 pm

@tulamide

haha! we shall see. hopefully it's fixed now.
B
47
S
17
G
82
Posts: 2,230
Reputation: 48,607

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests