Randomly selecting a global variable

This forum is currently in read-only mode.
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Upon collision with an object I would like 1 added to a random global variable. I would like to choose which global variables 1 can be added to from the random selection as well. I have no idea how to start this.

    Thank you for the help.

  • You need to use a naming convention to organize your global variables so you can do something like set global variable Gvariable&str(random(10)+1) to random(1). All your global variables you want to be included should of course have the same beginning then you use a number to tell them apart (eg. v1, v2, v3 etc). Remember to right click on set global variable prompt and choose "use expression" so that you can set the variable to an expression. When you start using naming conventions more, you may want to start looking into the text manipulator so that you can use the useful "replace" action. I'm sorry I can't post an example cap for you.

  • What about just setting up an array and then randomly picking form one of the cells?

  • You need to use a naming convention to organize your global variables so you can do something like set global variable Gvariable&str(random(10)+1) to random(1). All your global variables you want to be included should of course have the same beginning then you use a number to tell them apart (eg. v1, v2, v3 etc). Remember to right click on set global variable prompt and choose "use expression" so that you can set the variable to an expression. When you start using naming conventions more, you may want to start looking into the text manipulator so that you can use the useful "replace" action. I'm sorry I can't post an example cap for you.

    I'm not sure that will work. I'd go with either a global array, or store a temporary value generated by random(), then have a series of subevents along the lines of: random value = 0, use global('abc'), random value = 1, use global('def'), etc. (Don't put random() in each line or it will generate a new random number for each line!)

  • I use naming conventions all the time and works perfectly. It makes eventing easy - just have to get use to strings, tokens and converting values.

  • Thank you all for your time. I am afraid this looks to be a bit too complex for me at the moment and I really need to finish up as much of the game as possible by May for a Game Developers Conference (I am really mainly just an art, music, and concept guy). If anyone can post an example cap so I can look at this further I would really appreciate it. Everyone who has helped is going into the credits of the game if they want to.

  • > You need to use a naming convention to organize your global variables so you can do something like set global variable Gvariable&str(random(10)+1) to random(1). All your global variables you want to be included should of course have the same beginning then you use a number to tell them apart (eg. v1, v2, v3 etc). Remember to right click on set global variable prompt and choose "use expression" so that you can set the variable to an expression. When you start using naming conventions more, you may want to start looking into the text manipulator so that you can use the useful "replace" action. I'm sorry I can't post an example cap for you.

    >

    I'm not sure that will work. I'd go with either a global array, or store a temporary value generated by random(), then have a series of subevents along the lines of: random value = 0, use global('abc'), random value = 1, use global('def'), etc. (Don't put random() in each line or it will generate a new random number for each line!)

    This will work the best. Trust me. It's not complex either!

  • >

    > > You need to use a naming convention to organize your global variables so you can do something like set global variable Gvariable&str(random(10)+1) to random(1). All your global variables you want to be included should of course have the same beginning then you use a number to tell them apart (eg. v1, v2, v3 etc). Remember to right click on set global variable prompt and choose "use expression" so that you can set the variable to an expression. When you start using naming conventions more, you may want to start looking into the text manipulator so that you can use the useful "replace" action. I'm sorry I can't post an example cap for you.

    > >

    > I'm not sure that will work. I'd go with either a global array, or store a temporary value generated by random(), then have a series of subevents along the lines of: random value = 0, use global('abc'), random value = 1, use global('def'), etc. (Don't put random() in each line or it will generate a new random number for each line!)

    >

    This will work the best. Trust me. It's not complex either!

    That procedure does seem the most simple, the only problem is that I have no idea how to implement a "Random()". How would I create it in an Event? Thank you all again.

  • While using an expression would be nice if you have a large number of variables to work with, I'm not able to get Construct to allow me to do that...

    Anyway, simple conditional statements would be fine in most cases. Here's a quick .cap with an example (made with v0.99.84): Random Global

    Also, here is a good page of the wiki to know of, if you don't:

    http://sourceforge.net/apps/mediawiki/c ... xpressions

    It explains things like the Random() function.

  • While using an expression would be nice if you have a large number of variables to work with, I'm not able to get Construct to allow me to do that...

    heck your PM.

  • While using an expression would be nice if you have a large number of variables to work with, I'm not able to get Construct to allow me to do that...

    Anyway, simple conditional statements would be fine in most cases. Here's a quick .cap with an example (made with v0.99.84): Random Global

    Also, here is a good page of the wiki to know of, if you don't:

    http://sourceforge.net/apps/mediawiki/c ... xpressions

    It explains things like the Random() function.

    Thank you all, it works perfectly for my item system and I understand how to implement it properly within Construct.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I am encountering a problem where "1" is added to a global variable constantly instead of just once. Below is the code:

    <img src="http://ofhell.com/examplesvar.png">

    Whichever Randvar gets "1" added to it, constantly has 1 added to it all the time, increasing its value. I am probably missing something easy, thank you again!

  • Hi. The reason that 1 is being constantly added to one variable is that you have the Random Var group of events executing every tick, instead of as a sub-event of the TreasureChest group. If you drag the Random Var group up to the bottom of the shaded area under event 47, it should indent the group to the right a bit, making it a sub-group. Then it will only execute when a chest is touched.

    Also, it should be noted that the global variables are being incremented each time that group is executed, so the Var Gold event and the other 3 like it will not work properly after the first time that their respective variables are incremented, because they are checking for a value of one.

    One way to remedy that is to set the global variables to 1, instead of adding to them, but you'd still have to set them back to zero after the action is taken on them, or you'd end up with the same problem as you have now, basically.

    From what i can see of the event sheet, it looks to me like you may not need the global variables at all. you could just move the actions taken in event 52 to the actions for event 48, and so on for the rest.

  • Thank you very much!

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