[SOLVED] Randomized Beginning Setup

E

Edoran

Guest
Help! I can't figure out why this isn't working.

I am making a game where at the beginning, a board is created from a random selection of tiles in a random order. There can only be so many of each tile on the board at a time.

Here is my code for the first tile placement

///Tile 1
//Randomizer
if irandom(6)=0
{
global.ran1=0
}
else
if irandom(6)=1
{
global.ran1=1
}
else
if irandom(6)=2
{
global.ran1=2
}
else
if irandom(6)=3
{
global.ran1=3
}
else
if irandom(6)=4
{
global.ran1=4
}
else
if irandom(6)=5
{
global.ran1=5
}
else
if irandom(6)=6
{
global.ran1=6
}
//1st tile
if (global.ran1 = 0) && (global.p0 > 0) then
{
instance_create(192,160,obj_p0)
}
if (global.ran1 = 1) && (global.p1 > 0) then
{
instance_create(192,160,obj_p1)
}
if (global.ran1 = 2) && (global.p2 > 0) then
{
instance_create(192,160,obj_p2)
}
if (global.ran1 = 3) && (global.p3 > 0) then
{
instance_create(192,160,obj_p3)
}
if (global.ran1 = 4) && (global.p4 > 0) then
{
instance_create(192,160,obj_p4)
}
if (global.ran1 = 5) && (global.p5 > 0) then
{
instance_create(192,160,obj_p5)
}
if (global.ran1 = 6) && (global.p6 > 0) then
{
instance_create(192,160,obj_p6)
}


It comes back at me with this error


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_setup:

global variable <unknown built-in variable>(-1610512734, -2147483648) not set before reading it.
at gml_Script_scr_tile1a (line 42) - if (global.ran1 = 1) && (global.p1 > 0) then
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_tile1a (line 42)
called from - gml_Object_obj_setup_CreateEvent_1 (line 1) - script_execute(scr_tile1a)


Each instance of a certain tile created reduces the global.p(number of tile) by 1, so that when it = 0 it won't create any more.

I am quite new to Game Maker, so this might be the completely wrong way to go about it in the first place, any help appreciated :)

Thanks,
 

Fabseven

Member
first in a if statement you should use == to compare two values not =
second : I donot know if && is working like in C, use AND instead ? I do not think "then" is necessary, why not just
Code:
if ( condition)
{
  //your code
}
third : use randomize() function at the beginning of your code to generate a seed or the randoms functions will always output the sames values in each run.

fourth : use a switch instead of multiplie if
Code:
randomvalue = irandom(6)
switch(randomvalue)
{
       case 0 :
               //your code
              break;

       case 1 :
             //your code
            break;

 ......others values....

    default:
           //your code
         break;
}
 

Appsurd

Member
first in a if statement you should use == to compare two values not =
second : I donot know if && is working like in C, use AND instead ? I do not think "then" is necessary, why not just
third : use randomize() function at the beginning of your code to generate a seed or the randoms functions will always output the sames values in each run
1. No, using = is just fine. You may use both :)
2. No, using && is again fine. You may use either && or AND
3. When using GameMaker: Studio, yes, however, this won't solve his problem.
------------------------------------------------------------------------------------------------------------------

From what I understand, you want to continue to create object whenever global.p1 > 0. And if so, you want to create a random object choosing from obj_p0, obj_p1, obj_p2, obj_p3, obj_p4, obj_p5, obj_p6, obj_p7. So using this, we may construct the following:
Code:
if global.p1 > 0 // if we may create a new object
{
  obj = choose(obj_p0, obj_p1, obj_p2, obj_p3, obj_p4, obj_p5, obj_p6, obj_p7) // choose a random object
  instance_create(192,160,obj) // create your random object
  global.p1 -= 1 // lower the value of global.p1 by 1
}
Hopefully it's clear to you what I'm doing, if not, please ask!
 
E

Edoran

Guest
Hi,

Yes thanks to both of you! That clears it up great. I didn't know of the randomize() function so I was making everything waaay more complicated than I needed. (awkward beginner...)

Thanks!
 
Top