Jump to content


Photo

What's wrong with this code?


  • Please log in to reply
11 replies to this topic

#1 Maxamilius291

Maxamilius291

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 03 March 2012 - 05:21 PM

I'm trying to learn how to use GML rather than d&d, so I'm following the basic platformer tutorial, but trying to do as much as I can using code instead. Seems I've fallen at the first hurdle... this is the step event for my character, just setting the gravity if he's not standing on anything, and setting it if he is.

if place_free (x,y+1) then (gravity=0.5) else (gravity=0)
if (vspeed>12) then (vspeed=12)

It says error at line 1 pos 46 (just before the else), assignment operator expected. What do I do?

Edited by Maxamilius291, 03 March 2012 - 05:25 PM.

  • 0

#2 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 03 March 2012 - 05:34 PM

Parentheses )(()()())()( are for functions and grouping validity checks. Curly brackets, {}{}}}}{}{{} are for blocks. I'd personally not use all those 'then's, this is the way I'd write it:

//Collision checking
if (place_free(x,y+1)) { gravity = 0.5; } 
else { gravity = 0; vspeed = 0; }

//Speed cap
if (vspeed > 12) { vspeed = 12; }

I use a lot of spaces, comments, tabbing, brackets, and semicolons to keep things organized. When you have code like this:

//Colorizing bgs
background_blend[0] = global.color_background;
background_blend[1] = global.color_background;
background_blend[2] = global.color_background;

//Making sure that the jets don't vanish
if (instance_number(blackhole) = 1 && instance_number(jet) = 0) { instance_create(blackhole.x,blackhole.y,jet); }

//Multiplier effects
if (global.counter > power(global.difficulty*2,global.multiplier)*10) { 
	global.multiplier += 1; global.counter = 0; 
	//Particles 
	emtr = part_emitter_create(global.ps)
	trail = part_type_create()
	part_type_sprite(trail,Fireballtrail,true,true,false)
	part_type_color1(trail,global.color_multiplier);
	part_type_alpha2(trail,1,0)
	part_type_life(trail,10,40)
 
	flyer = part_type_create()
	part_type_sprite(flyer,Fireballtrail,false,false,false)
	part_type_direction(flyer,0,360,0,0.1)
	part_type_color1(flyer,global.color_multiplier);
	part_type_speed(flyer,10,10,0,0)
	part_type_step(flyer,1,trail)
                                         	
	part_emitter_region(global.ps,emtr,blackhole.x-1,blackhole.x+1,blackhole.y-1,blackhole.y+1,ps_shape_ellipse,ps_distr_linear)
	part_emitter_burst(global.ps,emtr,flyer,10)
	part_emitter_destroy(global.ps,emtr) 	
}

//View rotation
global.view_shake[0] /= 1.05; 
if (global.view_shake[0] < 0) { global.view_shake[0] = 0; }
view_angle[0] = rot_smooth(global.view_change[0],view_angle[0],5,-1) + global.view_shake[0]*random_range(-2,2);

//loop 
if (!sound_isplaying(n_background)) {
	n_background = choose(nBackground_0,nBackground_1,nBackground_2);
	sound_play(n_background); }
n_background.sound_volume = global.view_shake*2;

You'll need it!

BTW: make sure you set vspeed = 0 after landing, or you'll continue falling though the floor!

Edited by Anzkji, 03 March 2012 - 05:41 PM.

  • 0

#3 _226266

_226266

    GMC Member

  • New Member
  • 28 posts

Posted 03 March 2012 - 05:35 PM

the codes in gml use () for the if function and {} for whats gonna happend
like this.

if(Question){Action}
else{Action}

if(place_free(x,y+1)) {gravity=0.5}
else {gravity=0}

if (vspeed>12){vspeed=12}

  • 0

#4 Maxamilius291

Maxamilius291

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 03 March 2012 - 05:37 PM

Ok, thanks for the help guys :D
  • 0

#5 ArcturusGaming

ArcturusGaming

    GMC Member

  • New Member
  • 25 posts

Posted 03 March 2012 - 06:51 PM

if place_free (x,y+1)
{
gravity=0.5
}
else
{
gravity=0
}

if vspeed > 12
{
vspeed=12
}


like that :)
  • 0

#6 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4686 posts
  • Version:GM8

Posted 03 March 2012 - 08:43 PM

Am I the only one seeing a space between "place_free" and "(x,y+1)"?
  • 0

#7 Jakyl11

Jakyl11

    GMC Member

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

Posted 03 March 2012 - 08:47 PM

a very simple gravity code can be done like this

gravity=place_free(x,y+1)*.5;

EDIT:
@

TheouAegis spaces in that doesnt matter it still reads it correctly tho it could be confusing to do this.


Edited by Jakyl11, 03 March 2012 - 08:48 PM.

  • 0

#8 RangerX

RangerX

    GMC Member

  • GMC Member
  • 907 posts
  • Version:GM8.1

Posted 03 March 2012 - 08:53 PM

Related suggestion for the OP:

Following tutorials for learning GML before following tutorials for learning GameMaker could save you alot of hassle plus make you understand code way better. It will especially help you when you'll start composing your own code parts.

Edited by RangerX, 03 March 2012 - 09:02 PM.

  • 0

#9 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 03 March 2012 - 09:00 PM

I hope you mean hassle... Posted Image But yeah, that is really true. GML is my first language, though, so I didn't really have much of an option.
  • 0

#10 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4686 posts
  • Version:GM8

Posted 03 March 2012 - 10:36 PM

English is mine.

*ducks tomatoes*
  • 0

#11 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 03 March 2012 - 11:36 PM

Throws splosive tomatas at your FEET!
  • 0

#12 DivideBy2

DivideBy2

    GMC Member

  • New Member
  • 64 posts
  • Version:GM8

Posted 04 March 2012 - 05:51 PM

GML is actually very versatile, you can write code in a lot of different ways. Personally, I do things like this:
if tomato && !tuna
    pizza = 1;
else if !tomato && tuna
    pizza = 0;
else {
    pizza = -1;
    ragequit();
}

Edited by DivideBy2, 04 March 2012 - 05:51 PM.

  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users