Create a Random Variable:

Game Editor comments and discussion.

Create a Random Variable:

Postby d-soldier » Mon Jun 11, 2007 3:07 am

Is it possible to script an object the randomly create a number, and then use that number to do something? ... For example, Using a timer event, every ten seconds an object will create one of four possible objects (powerups in this case) and the possibility of creating nothing too... does that make sense?
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby pixelpoop » Mon Jun 11, 2007 11:19 am

on a create actor event put this:

int n;
n=rand(100);
if (n==0){put code here that will exacute 1% of the time
}
if (n<=10 && n>0){put code here to exacute 10% of the time
}
Last edited by pixelpoop on Mon Jun 11, 2007 10:08 pm, edited 1 time in total.
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby d-soldier » Mon Jun 11, 2007 2:57 pm

Thanks a bunch! Exactly what I needed!
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby d-soldier » Mon Jun 11, 2007 8:07 pm

.. I originall put this script on a timer for a filled-region actor, and it didn't do anything... so I then moved it to the create-actor script (as instructed) amd still doesn't do anything... Did I do something wrong here?

int n;
n=rand(100);
if (n=0)
{
CreateActor("pu_1up", "pu_shield00000", "(none)", "(none)", 0, 0, false);
}
if (n<=50 && n>0)
{
CreateActor("pu_gun", "pu_gun00000", "(none)", "(none)", 0, 0, false);
}
if (n<=100 && n>50)
{
CreateActor("pu_shield", "pu_shield00000", "(none)", "(none)", 0, 0, false);;
}
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Sgt. Sparky » Mon Jun 11, 2007 10:01 pm

d-soldier wrote:.. I originall put this script on a timer for a filled-region actor, and it didn't do anything... so I then moved it to the create-actor script (as instructed) amd still doesn't do anything... Did I do something wrong here?

int n;
n=rand(100);
if (n=0)...

n ==, not n =. :D
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby pixelpoop » Mon Jun 11, 2007 10:11 pm

thanks Sgt Sparky,

Just to clarify,
in the if statement it should read n==0
when declaring what n is equal to you would use a single equals

note: I have edited my original code with this correction.
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby Sgt. Sparky » Mon Jun 11, 2007 10:13 pm

pixelpoop wrote:thanks Sgt Sparky,

Just to clarify,
in the if statement it should read n==0
when declaring what n is equal to you would use a single equals

note: I have edited my original code with this correction.

that is what I said. xD :lol:
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby d-soldier » Mon Jun 11, 2007 11:51 pm

Right you are.. should have caught that myself... :cry: .. speaking of random variables, what if I wanted my actor to move to a random location (within the view) on a keydown event?
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Sgt. Sparky » Tue Jun 12, 2007 12:08 am

events->add->keydown->whateverkey->
Code: Select all
xscreen = rand(view.width);
yscreen = rand(view.height);

:D
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby Game A Gogo » Tue Jun 12, 2007 12:11 am

maybe somethign like:
Code: Select all
x=rand(view.width)+view.x;
y=rand(view.height)+view.y;

Calculates a random number whithing the views size and add the position to it.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby Game A Gogo » Tue Jun 12, 2007 12:14 am

sparky was quicker then I D:
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby d-soldier » Tue Jun 12, 2007 12:40 am

You guys are smarter then I know what to do with... :D
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Game A Gogo » Tue Jun 12, 2007 12:46 am

smarty party :D
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby Sgt. Sparky » Tue Jun 12, 2007 2:36 pm

Game A Gogo wrote:smarty party :D

Weeee!!! *grabs a bag of smartys and runs in circels with it*
eventually runs into a wall and gets knocked out*
Weeee!!!
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby Fuzzy » Tue Jun 12, 2007 8:47 pm

Code: Select all
switch( var holding a rand value)
{
    case 0:    createactor;
                 break;
    case 1:    createactor;
                 break;   
    case 2:    createactor;
    default : break;
}


Use as many cases as you like.

The break statements bails out from the test, otherwise it might ring true for several cases and execute their code. Default will run after all the others. Especially if you dont break before then. Note that I left a break out after the case 2, because the default handles in. I dont like redundancy.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron