Jump to content


Photo

is there anything wrong with this code?


  • Please log in to reply
9 replies to this topic

#1 st4k3r 4 lyf

st4k3r 4 lyf

    GMC Member

  • GMC Member
  • 114 posts

Posted 04 March 2012 - 12:42 AM

is there anything wrong with this code? this is not generating as well as it should, there should be lots of stone blocks thats being generated increasing commoness as the further down you go, but this.. just don't seem to be working.. here is my script

//on create event of object just being made
//change some gen blocks to stone
with (self){
casenum2=0;
switch y
{
case 128 or 144: casenum2=1;
case 160 or 176: casenum2=2;
case 192 or 208: casenum2=3;
case 224 or 240: casenum2=4;
case 256 or 272: casenum2=5;
case 288 or 308: casenum2=6;
case 320 or 336: casenum2=7;
case 352 or 368: casenum2=8;
case 384 or 400: casenum2=9;
case 416 or 432: casenum2=10;
case 448 or 464: casenum2=11;
}
xrand1=random_range(0, 15)
xrand1=round(xrand1)
switch xrand1
{
case 1-casenum2: instance_change(obj1,1)
}
}

and here's the results..

Posted Image




EDIT: the objects that should be changing into the stone is invisible, also, i purposely made a rule in some other script to always have stone at the bottom y row.

Edited by st4k3r 4 lyf, 04 March 2012 - 02:13 AM.

  • 0

#2 thegame

thegame

    Flying Penguin

  • GMC Member
  • 1405 posts
  • Version:GM:Studio

Posted 04 March 2012 - 12:52 AM

I am pretty sure that multiple cases can't be done like that... use
case 128: case 144:
case 160: case 176:
etc.
Another thing is that you need to put a break after each case, so your code should in the end look like
switch y
{
case 128: case 144: casenum2=1;break;
case 160: case 176: casenum2=2;break;
case 192: case 208: casenum2=3;break;
case 224: case 240: casenum2=4;break;
case 256: case 272: casenum2=5;break;
case 288: case 308: casenum2=6;break;
case 320: case 336: casenum2=7;break;
case 352: case 368: casenum2=8;break;
case 384: case 400: casenum2=9;break;
case 416: case 432: casenum2=10;break;
case 448: case 464: casenum2=11;break;
}

Edited by thegame, 04 March 2012 - 12:54 AM.

  • 0

#3 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 04 March 2012 - 12:54 AM

You are missing break statements. It should look like this:

//on create event of object just being made
//change some gen blocks to stone
with (self){
casenum2=0;
switch y
{
case 128 or 144: casenum2=1; break
case 160 or 176: casenum2=2; break
case 192 or 208: casenum2=3; break
case 224 or 240: casenum2=4; break 
case 256 or 272: casenum2=5; break 
case 288 or 308: casenum2=6; break 
case 320 or 336: casenum2=7; break 
case 352 or 368: casenum2=8; break 
case 384 or 400: casenum2=9; break 
case 416 or 432: casenum2=10; break 
case 448 or 464: casenum2=11; break }
xrand1=random_range(0, 15)
xrand1=round(xrand1)
switch xrand1
{
case 1-casenum2: instance_change(obj1,1)
}
}

It needs them to distinguish the cases from one another.

Edited by Anzkji, 04 March 2012 - 12:55 AM.

  • 0

#4 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 04 March 2012 - 12:55 AM

First of all, try out some debug code. Right after the second switch do

(show_message(string(casenum2))

if you get any 0s, you screwed that up.

Also, for the second switch, do not use a switch, there is no need. Instead do

if (xrand1 < casenum2) //you may need <=
{
instance_change(obj1,1)
}

There's also (probably) no need for with (self) just do all the code out of a with block.

Edit: also to make an "or" work in a switch change, e.g.,

case 128 or 144: blah blah; break;

to

case 128:
case 144: blah blah; break;

Edit: And of course the breaks are needed, with the rest of this stuff.

Edited by greep, 04 March 2012 - 12:58 AM.

  • 0

#5 @Alex@

@Alex@

    Retired GMC Reviewer

  • Reviewer
  • 3071 posts
  • Version:Unknown

Posted 04 March 2012 - 12:55 AM

You can't use or in case statements and you also don't end each case with break meaning that each case after the one that triggers will be executed. with(self) is completely and utterly pointless. Your x case system falls short its values are 1 through -14 but Xrand is 0 through 15 meaning it can only trigger twice.
  • 0

#6 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 04 March 2012 - 12:56 AM

Umm, he just needs the break statements.

I think...
  • 0

#7 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 04 March 2012 - 12:58 AM

It's not just the breaks, there's a few things, like everyhting in my post :P. If it were JUST breaks, then 2/3 of the things would be stone. They are not.
  • 0

#8 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2747 posts
  • Version:GM:Studio

Posted 04 March 2012 - 01:03 AM

Two problems:
1. The cases are not terminated with break.
2. You are intuitively reading the or (||) operator. This is without doubt one of the most common novice errors. In the case of switch statements, two matching cases can be chained together:
//Example case
case 128: case 144:
  casenum2=1;
break;
A rule of thumb is to NEVER use and or or unless you're sure both sides evaluate directly to true or to false.

Also, other notes:
1. with(self) is useless, just drop it.
2. Use irandom_range(), rather than random_range() and then floor().

GameGeisha
  • 0

#9 st4k3r 4 lyf

st4k3r 4 lyf

    GMC Member

  • GMC Member
  • 114 posts

Posted 04 March 2012 - 02:11 AM

Omg! thanks everyone :smile: i guess the major problem was the 2nd switch statement, i didn't know you could'nt use
case 1 or case 2:
or

case 1 - variable: bla bla, so basically i did what most of you said, i added the break statements, i made them all seperate cases, and set up an

if xrand <=11
{
instance_change(obj1,1)
}
instead, now it actually works.. it still looks like the far lands generated, but thats just the algoritim, maybe make the stone more likely to spawn near other stone, but thanks :smile:
  • 0

#10 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 04 March 2012 - 02:12 AM

2. Use irandom_range(), rather than random_range() and then floor().


Not always. irandom_range() is the equivalent of random_range() and then round(). Not exactly the same - but I doubt it matters.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users