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

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.

  • 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?).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)