How do I create a random "add" to variable?

Get help using Construct 2

Post » Sat Jan 14, 2017 12:57 am

Hey all, would anyone know of a string I can use that would add to a variable when the player attacks? I want to set up a mining simulation, so when the player attacks and the object loses hp, it will add or not, to the variable.

So for example the player could attack 3 times and get nothing, but the 4th it would give, but completely random as and when the variable gets added to.
B
13
S
7
G
2
Posts: 65
Reputation: 2,307

Post » Sat Jan 14, 2017 8:44 am

Hi @SpyDaniel, there are multiple ways to do this, some very complex (but also flexible, great for turnbased strategy or RPG gameplay with lots of stats defining units or characters). But the basic structure would be:

[construct 2 expressions are in brackets]

Conditions:
On event trigger (attack animation end or frame X of animation, or on a function triggered by the attack):
Pick the player object (not needed if there is just one)

Actions:
add to variable - [choose(0,0,0,1)]
^ choose picks a random choice out of the defined possibilities, in this case 3 zeroes and 1 one value. So you have 25% odds of 1 getting added to the variable.

If you want more complex logic (but easier to maintain and also change while the game is running thus more dynamic), you could do for example an event block as below with the same initial triggers but two sub-event blocks:

Conditions - same as above.

Subevent 1 conditions - nothing (leave it empty, this is needed only so that the number can be checked by the conditions in the second subevent)
Subevent 1 local variable - number type [Roll = 0]
subevent 1 actions - set variable Roll to [random(0,100)]
end subevent 1 block

subevent 2 conditions - if Roll < 25
subevent 2 actions - add to variable [1]
End subevent 2 block

^The above is probably the best structure if you want to have flexibility. Basically Roll can be set in any way you wish, you can use a random number within a range, add another variable to it (static player bonus to mining or a multiplication bonus like [Roll*player.miningefficiency] object variable). You can set up a custom function for dice rolling to determine the roll value setting Roll etc.

Also in subevent 2 conditions you can test to see if roll is below another variable, for example if Roll<player.miningskill (mining skill being a variable that increases with better gear or experience). That would effectively translate to increasing player odds of adding to the mystical variable (like mining experience? Or maybe loot?).
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Sat Jan 14, 2017 3:47 pm

@Pulstar thanks for the detailed examples, this has helped me a lot! I'm going to try it out now that I understand how to do what I wanted.
B
13
S
7
G
2
Posts: 65
Reputation: 2,307


Return to How do I....?

Who is online

Users browsing this forum: NadeemAbbass, plinkie and 10 guests