GameMaker: Studio

GameMaker: Studio

View Stats:
How to assing random variables in an objects create code?
Yeah I tried just puting

custom_ID = random(100)

but it kept choosing the same number over and over for ALL created instances

so if I spawned them in another object with unique IDs like

object1 = instance_create(x, y, thingobject)
object2 = instance_create(x, y, thingobject)

and I put in the creation code of thingobject (assuming to mean, on create for each one) create variable custom_ID = random(100)

it then sets that variable to one number and never changes.
I still cant figure out what randomize does, or how you use it. My question is does anyone know fo any good examples with this? I just want to manipulate objects via random ID codes and ranges.

See this is the code I'd use to manipulate it:

if masterobjct.triggervar == on
{
with thingobject
{
if thingobject.custom_ID >= [A number greater than a number or a number in range]
{
do something to that object with that custom_ID
}
}
}


thing is they all spawn with the same custom_ID. So how do you randomize a variable per instance? Do I have to give them the variable after theyre created from another object or what? I cant just have them do it using random? I thought it'd launch its create code on itself when created individually?
< >
Showing 1-15 of 17 comments
SaltySamurai Apr 4, 2015 @ 5:39am 
I just had a look at the page for this, it says:

"NOTE: This function will return the same value every time the game is run afresh due to the fact that GameMaker: Studio generates the same initial random seed every time to make debugging code a far easier task. To avoid this behaviour use randomize at the start of your game."

This needs to run once at the start of your game:

randomize();

edit: no guarantee, I've never done this but I might need to so let me know how it goes.
Last edited by SaltySamurai; Apr 4, 2015 @ 5:40am
Yeah no luck the value wont randomize per object, I place randomize(); in the creation event and tried multiple objects and it still wont create a unique variable ID per object using the randomize event. I'll probably end up not doing this way if I cant figure it out.
Well, it looks like Game Maker lacks basic Unique Variables with random value functions.... Dissapointing.
Daynar Apr 4, 2015 @ 4:31pm 
if thingobject.custom_ID will only check the first instance of thingobject. You already have it in a with so all you need to do is check custom_ID
Last edited by Daynar; Apr 4, 2015 @ 4:31pm
Wow thanks alot Daynar, I guess theyre not kidding when syntax gets you every time.

in case anyone sees this you type

with (thing)
{

and anything inside here is connected to it.

}

ex.

with (thisthing)
{
variablecustomID >= <a value of your choice>
{
do this thing here now
}
}

and it'll attach any below variables to the object declared at the top of the with or within the with <object>
{
<its stuff and actions to do>
{
Last edited by why are we here? just to suffer?; Apr 4, 2015 @ 7:29pm
SaltySamurai Apr 7, 2015 @ 5:17am 
and now I need this, just gotta work it out.
Daynar Apr 7, 2015 @ 8:49am 
Originally posted by memoryman18:
and now I need this, just gotta work it out.
randomize() // changes the default rng seed from which all rand numbers are derived
random_number = random(100) // give you a random number between 0 and 100
Thew Apr 7, 2015 @ 10:23am 
Also note that if you want to assure an integer value for your random number, use irandom() or irandom_range()

And if you want to manually set the seed (instead of using a random seed), use: random_set_seed()
Last edited by Thew; Apr 7, 2015 @ 10:25am
SaltySamurai Apr 7, 2015 @ 10:44am 
Originally posted by Thew:
Also note that if you want to assure an integer value for your random number, use irandom() or irandom_range()

And if you want to manually set the seed (instead of using a random seed), use: random_set_seed()

Thanks, I'll take note of that.

I'm using this for a shotgun spread though, I used random_range to get a value for the spread, just because it's more convenient for what I'm doing.

Also the bullets were shooting in the same direction for a while (so they merged together and looked like one bullet) but that just randomly stoped happening, don't know how I fixed it.
Daynar Apr 7, 2015 @ 11:51am 
Originally posted by memoryman18:
Originally posted by Thew:
Also note that if you want to assure an integer value for your random number, use irandom() or irandom_range()

And if you want to manually set the seed (instead of using a random seed), use: random_set_seed()

Thanks, I'll take note of that.

I'm using this for a shotgun spread though, I used random_range to get a value for the spread, just because it's more convenient for what I'm doing.

Also the bullets were shooting in the same direction for a while (so they merged together and looked like one bullet) but that just randomly stoped happening, don't know how I fixed it.
I do shotgun spread a little different, although your way may be better.
//NOTE: this assumes you have x/y values for the barrel of the gun and the direction the gun is pointing var bullet = instance_create(barrelx,barrely,oBullet); /*acc_range is the max inacuracy of the spread a gun with an acc range of 30 can shoot bullets 30 degrees above or below the aim angle */ repeat(bullets_per_shot) { bullet.direction = aim_angle + random(acc_range * 2) - acc_range bullet.speed = bullet_speed bullet.image_angle = bullet.direction }
SaltySamurai Apr 7, 2015 @ 1:24pm 
That repeat function is very handy, take a geuss how I was doing it.

I'm writing the script in a way that makes it easy to convert it for use in any weapon. So it can spawn multiple bullets in one shot like shotguns, sigle fire and fast rate like a pistol or full auto lower damage like an assault rifle.

So far it's going well, maybe you could help optimize it? I started scripting about a month ago.
SaltySamurai Apr 7, 2015 @ 2:01pm 
One issue I just noticed on the shotgun is that the spead is very varied, as an example, if I set the spread to 40 (making a posable cone of 80*) but sometimes (but still too often) the spred will only be about 8* or even put all the bullets right down the middle, merging them together.
Daynar Apr 7, 2015 @ 3:31pm 
Originally posted by memoryman18:
One issue I just noticed on the shotgun is that the spead is very varied, as an example, if I set the spread to 40 (making a posable cone of 80*) but sometimes (but still too often) the spred will only be about 8* or even put all the bullets right down the middle, merging them together.
There may be something wrong, or it could simply be the nature of rng. If you wanna make sure the bullets are spread out, you could give them each different starting angles and limit the random spread to a specific zone.
var bullet = instance_create(barrelx,barrely,oBullet); for(var i = 0; i < bullets_per_shot; i ++) { bullet.direction = aim_angle + (acc_range/(bullets_per_shot + 1) * i - acc_range/2) + random(acc_range/bullets_per_shot * 2) - acc_range/bullets_per_shot bullet.speed = bullet_speed bullet.image_angle = bullet.direction }
This might do the tricked but it's untested
memoryman, you're code logic might be way more complicated than I can understand but for randomization I place this above the random variable im tryin to use randomize(); in the step event. And I use irandom like Thew says to get something noticable. Big difference.

SaltySamurai Apr 8, 2015 @ 3:57am 
I fixed it, I did somthing dumb:

I created a random variable and then used that for the min and max of the random variable for spread in the bullets script. I must have been tired when I wrote that, or just not thinking.
< >
Showing 1-15 of 17 comments
Per page: 1530 50

Date Posted: Apr 3, 2015 @ 11:59pm
Posts: 17