This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Error on Pong

SirBothersomeSirBothersome Posts: 7Member
edited July 2016 in 2D
Hi, I'm new to Godot, and In the process of of trying to learn it, I'm making the Pong project in the tutorial docs, however, I keep getting a bug. either a) <br /><br />
extends Node2D<br /><br /># member variables here, example:<br /># var a=2<br /># var b=&quot;textvar&quot;<br />var screen_size<br />var pad_size<br /><br />func _ready():<br />&nbsp;  screen_size = get_viewport_rect().size<br />&nbsp;  pad_size = get_node(&quot;left&quot;).get_texture().get_size()<br />&nbsp;  set_process(true)<br />&nbsp;  var ball_speed = 80<br />&nbsp;  var direction = Vector2(-1, 0)<br />&nbsp;  var PAD_SPEED = 150<br /><br /><br /><br />func _process(delta):<br />&nbsp; &nbsp; &nbsp; var ball_pos = get_node(&quot;ball&quot;).get_pos()<br />&nbsp; &nbsp; &nbsp; var left_rect = Rect2( get_node(&quot;left&quot;).get_pos() - pad_size/2, pad_size )<br />&nbsp; &nbsp; &nbsp; var right_rect = Rect2( get_node(&quot;right&quot;).get_pos() - pad_size/2, pad_size)<br />&nbsp; &nbsp; &nbsp; ball_pos += direction * ball_speed * delta<br />&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; if ( (ball_pos.y &lt; 0 and direction.y &lt; 0) or (ball_pos.y &gt; screen_size.y and direction.y &gt; 0)):<br />&nbsp; &nbsp; &nbsp; &nbsp;  direction.y = -direction.y<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; if&nbsp; ( (left_rect.has_point(ball_pos) and direction.x &lt; 0) or (right_rect.has_point(ball_pos) and direction.x &gt; 0)):<br />&nbsp; &nbsp; &nbsp; &nbsp; direction.x = -direction.x<br />&nbsp; &nbsp; &nbsp; ball_speed += 1.1<br />&nbsp; &nbsp; &nbsp; direction.y = randf() + 2.0 - 1<br />&nbsp; &nbsp; &nbsp; direction = direction.normalized()<br /><br />&nbsp; &nbsp; &nbsp; if (ball_pos.x &lt; 0 or ball_pos.x &gt; screen_size.x):<br />&nbsp; &nbsp; &nbsp; &nbsp;  ball_pos = screen_size * 0.5 # ball goes to screem center<br />&nbsp; &nbsp; &nbsp; &nbsp;  ball_speed = 80<br />&nbsp; &nbsp; &nbsp; &nbsp;  direction = Vector2(-1, 0)<br />&nbsp;  <br />&nbsp; &nbsp; &nbsp; get_node(&quot;ball&quot;).set_pos(ball_pos)<br />&nbsp;  <br />&nbsp;  # move left pad<br />&nbsp; &nbsp; &nbsp; var left_pos = get_node(&quot;left&quot;).get_pos()<br />&nbsp;  <br />&nbsp; &nbsp; &nbsp; if (left_pos.y &gt; 0 and Input.is_action_pressed(&quot;left_move_up&quot;)):<br />&nbsp; &nbsp; &nbsp; &nbsp;  left_pos.y += -PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; if (left_pos.y &lt; screen_size.y and Input.is_action_pressed(&quot;left_move_down&quot;)):<br />&nbsp; &nbsp; &nbsp; &nbsp;  left_pos.y += PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp;  get_node(&quot;left&quot;).set_pos(left_pos)<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp;  var right_pos = get_node(&quot;right&quot;).get_pos()<br />&nbsp; &nbsp; &nbsp; &nbsp;  if (right_pos.y &gt; 0 and Input.is_action_pressed(&quot;right_move_ip&quot;)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right_pos.y += -PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (right_pos.y &lt; screen_size.y and Input.is_action_pressed(&quot;right_move_down&quot;)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  right_pos.y += PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  get_node(&quot;right&quot;).set_pos(right_pos) <br />&nbsp; &nbsp; &nbsp; <br />&nbsp;  # Called every time the node is added to the scene.<br />&nbsp;  # Initialization here<br />&nbsp; &nbsp; &nbsp; pass
<br />Produces. "Parser error, Identifier not found: direction"<br /><br />or b) having PAD_SPEED as a const generates "error parsing expression: misplaced const". anyhelp?

Comments

  • existentiaexistentia Posts: 2Member
    You're defining var direction inside the _ready() function but calling it from _process(). Move that up with screen_size and pad_size and it ought to be OK.
  • SirBothersomeSirBothersome Posts: 7Member
    Fixed the indentation issues as well as the + instead of *'s. NOW, this code<br />
    extends Node2D<br /><br /># member variables here, example:<br /># var a=2<br /># var b=&quot;textvar&quot;<br />var screen_size<br />var pad_size<br /><br /><br />func _ready():<br />&nbsp;  screen_size = get_viewport_rect().size<br />&nbsp;  pad_size = get_node(&quot;left&quot;).get_texture().get_size()<br />&nbsp;  set_process(true)<br /><br />var ball_speed = 80<br />var direction = Vector2(-1, 0)<br />const PAD_SPEED = 150<br />&nbsp;  <br />&nbsp;  <br /><br /><br /><br />func _process(delta):<br />&nbsp;  var ball_pos = get_node(&quot;ball&quot;).get_pos()<br />&nbsp;  var left_rect = Rect2( get_node(&quot;left&quot;).get_pos() - pad_size/2, pad_size )<br />&nbsp;  var right_rect = Rect2( get_node(&quot;right&quot;).get_pos() - pad_size/2, pad_size)<br />&nbsp;  ball_pos += direction * ball_speed * delta<br />&nbsp; &nbsp; &nbsp; <br />&nbsp;  if ( (ball_pos.y &lt; 0 and direction.y &lt; 0) or (ball_pos.y &gt; screen_size.y and direction.y &gt; 0)):<br />&nbsp; &nbsp; &nbsp; direction.y = -direction.y<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp;  if&nbsp; ( (left_rect.has_point(ball_pos) and direction.x &lt; 0) or (right_rect.has_point(ball_pos) and direction.x &gt; 0)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; direction.x = -direction.x<br />&nbsp;  ball_speed *= 1.1<br />&nbsp;  direction.y = randf() * 2.0 - 1<br />&nbsp;  direction = direction.normalized()<br /><br />&nbsp;  if (ball_pos.x &lt; 0 or ball_pos.x &gt; screen_size.x):<br />&nbsp; &nbsp; &nbsp; ball_pos = screen_size * 0.5 # ball goes to screem center<br />&nbsp; &nbsp; &nbsp; ball_speed = 80<br />&nbsp; &nbsp; &nbsp; direction = Vector2(-1, 0)<br />&nbsp;  <br />&nbsp;  get_node(&quot;ball&quot;).set_pos(ball_pos)<br />&nbsp;  <br />&nbsp;  # move left pad<br />&nbsp;  var left_pos = get_node(&quot;left&quot;).get_pos()<br />&nbsp;  <br />&nbsp;  if (left_pos.y &gt; 0 and Input.is_action_pressed(&quot;left_move_up&quot;)):<br />&nbsp; &nbsp; &nbsp; left_pos.y += -PAD_SPEED * delta<br />&nbsp;  if (left_pos.y &lt; screen_size.y and Input.is_action_pressed(&quot;left_move_down&quot;)):<br />&nbsp; &nbsp; &nbsp; left_pos.y += PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp;  get_node(&quot;left&quot;).set_pos(left_pos)<br />&nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp;  var right_pos = get_node(&quot;right&quot;).get_pos()<br />&nbsp;  if (right_pos.y &gt; 0 and Input.is_action_pressed(&quot;right_move_up&quot;)):<br />&nbsp; &nbsp; &nbsp; right_pos.y += -PAD_SPEED * delta<br />&nbsp;  if (right_pos.y &lt; screen_size.y and Input.is_action_pressed(&quot;right_move_down&quot;)):<br />&nbsp; &nbsp; &nbsp; right_pos.y += PAD_SPEED * delta<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp;  get_node(&quot;right&quot;).set_pos(right_pos) <br />&nbsp; &nbsp; &nbsp; <br />&nbsp;  # Called every time the node is added to the scene.<br />&nbsp;  # Initialization here
    
    <br />      <br />The game loads, the pads work, but the ball just flies around everywhere. Hrmmmm<br />
  • BinaryOrangeBinaryOrange Posts: 244Member
    @SirBothersome:<br /><br />We do have code tags here, I have modified your posts to use them. Just click the hash (#) symbol in the post editor after selecting your code and it will automatically enclose the code with tags. Otherwise, you can manually do so by typing [ code ] (your code here) [ /code ], without the spaces in-between the square brackets.  ;)
  • AkienAkien Posts: 70Member Godot Leader
    &nbsp;  var left_rect = Rect2( get_node(&quot;left&quot;).get_pos() - pad_size/2, pad_size )<br />&nbsp;  var right_rect = Rect2( get_node(&quot;right&quot;).get_pos() - pad_size/2, pad_size)
    
    <br />Be careful about that, if your pad_size is a vector of integers, [tt]pad_size/2[/tt] would give you a rounded value (e.g. if the pad width is 13, [tt]pad_size.x/2[/tt] would be 6). The proper way to get the half size here would be [tt]pad_size*0.5[/tt]. This should be fixed in the official tutorial :)<br /><br />Check the official pong demo and compare it with your script, I see some indentation issues at least: https://github.com/godotengine/godot-demo-projects/blob/master/2d/pong/pong.gd<br /><br />Several users are having troubles with the way the pong tutorial is written it seems, if you have suggestions for improvements, that would be very welcome :)
  • gungnirindgungnirind Posts: 11Member
    on 1469771522:
    <br />Be careful about that, if your pad_size is a vector of integers, [tt]pad_size/2[/tt] would give you a rounded value (e.g. if the pad width is 13, [tt]pad_size.x/2[/tt] would be 6). The proper way to get the half size here would be [tt]pad_size*0.5[/tt]. This should be fixed in the official tutorial :)<br />
    <br /><br />Note that [tt]pad_size/2.0[/tt] gives a float result just like [tt]pad_size*0.5[/tt], but is perhaps a bit more intuitive, IMO :)
  • SirBothersomeSirBothersome Posts: 7Member
    func _ready():<br />	screen_size = get_viewport_rect().size # Get actual size<br />	pad_size = get_node(&quot;left&quot;).get_texture().get_size()<br />set_process(true)
    
    <br />Is missing {edit: placed near the beginning in}from the Godot.docs tutorial.  Also, the docs tut is confusing because the indentation isn't clearly marked, nor is there a final result of what the finished script should look like for comp/contr<br />
    if you have suggestions for improvements, that would be very welcome :)<br />
    Mmm. Perhaps later. I'm already working on devel on something else. GBAGD
  • SirBothersomeSirBothersome Posts: 7Member
    Copy/pasted code from GItHub. Aside from syntax error on unindented Set_process(true), game works fine now. But the GH code is different from that of the docs  >:(

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.