Programming help required (GDscript or C#)

Where you can discuss, chat about events or, organise member meetups, the world or life in general including movies, tv shows and more.

Moderators: Beex, Elgood81, Beerkeg, Ferefire

Post Reply
User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 17 Apr 2023, 20:34

Getting errors in this code.
 

Code: Select all

 
extends Area2D 

export -(int)-var-Speed
var velocity =Vector2 ()
var screensize = Vector2 (480, 720)
func get_input() :
velocity = vector2()
if Input.is_action_pressed ("ui_left") :
velocity.x-= 1
if input.is_action_pressed ("ui_right") :
velocity.x+= 1
if Input.is_action_pressed ("ui_up") :
velocity.y-= 1 
if Input.is_action_pressed ("ui_down") :
velocity.y+= 1
if velocity.length() > 0:
velocity velocity.normalized() * speed
Can anyone help me fix it?
 
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Beerkeg
Standard Member
Posts: 923
Joined: 05 May 2017, 20:51
Contact:
Estonia

Achievements

Programming help required (GDscript or C#)

Post by Beerkeg » 17 Apr 2023, 21:35

I've not written any GDscript or even heard of it, but this doesn't return any errors. Seems like some formatting issues, typos and indenting:

extends Area2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
if Input.is_action_pressed ("ui_right"):
velocity.x += 1
if Input.is_action_pressed ("ui_up"):
velocity.y -= 1
if Input.is_action_pressed ("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 18 Apr 2023, 07:00

Beerkeg wrote:
17 Apr 2023, 21:35
I've not written any GDscript or even heard of it, but this doesn't return any errors. Seems like some formatting issues, typos and indenting:

extends Area2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
if Input.is_action_pressed ("ui_right"):
velocity.x += 1
if Input.is_action_pressed ("ui_up"):
velocity.y -= 1
if Input.is_action_pressed ("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed

Thanks, I'll see if this works.
 

Code: Select all

 
extends Area2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
if Input.is_action_pressed ("ui_right"):
velocity.x += 1
if Input.is_action_pressed ("ui_up"):
velocity.y -= 1
if Input.is_action_pressed ("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed
Returns an error on the "if" after vector2()
 
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Beerkeg
Standard Member
Posts: 923
Joined: 05 May 2017, 20:51
Contact:
Estonia

Achievements

Programming help required (GDscript or C#)

Post by Beerkeg » 18 Apr 2023, 08:20

richardallen1976 wrote:
18 Apr 2023, 07:00
Beerkeg wrote:
17 Apr 2023, 21:35
I've not written any GDscript or even heard of it, but this doesn't return any errors. Seems like some formatting issues, typos and indenting:

extends Area2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
if Input.is_action_pressed ("ui_right"):
velocity.x += 1
if Input.is_action_pressed ("ui_up"):
velocity.y -= 1
if Input.is_action_pressed ("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed

Thanks, I'll see if this works.

Code: Select all

 
extends Area2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
if Input.is_action_pressed ("ui_right"):
velocity.x += 1
if Input.is_action_pressed ("ui_up"):
velocity.y -= 1
if Input.is_action_pressed ("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed
Returns an error on the "if" after vector2()

Sorry about that, didn't realize the forum gets rid of the formatting :P
You need to indent your code so 1 tab before velocity = Vector2() and all the if statements and 2 tabs before the if returns. Like in this image (if the image uploads properly)
Image

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 18 Apr 2023, 08:24

That's very helpful :)

I'll see if it works later.

Unfortunately the link doesn't work.

Well it does but I have to sign into the website to view the pic.

Would you be able to inbox me the code so I can copy/paste it into Godot?
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Beerkeg
Standard Member
Posts: 923
Joined: 05 May 2017, 20:51
Contact:
Estonia

Achievements

Programming help required (GDscript or C#)

Post by Beerkeg » 18 Apr 2023, 21:52

richardallen1976 wrote:
18 Apr 2023, 08:24
That's very helpful :)

I'll see if it works later.

Unfortunately the link doesn't work.

Well it does but I have to sign into the website to view the pic.

Would you be able to inbox me the code so I can copy/paste it into Godot?

Made a pastebin with it, it kept the formatting there

https://pastebin.com/U4rGBS8Y

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 19 Apr 2023, 06:04

Beerkeg wrote:
18 Apr 2023, 21:52
richardallen1976 wrote:
18 Apr 2023, 08:24
That's very helpful :)

I'll see if it works later.

Unfortunately the link doesn't work.

Well it does but I have to sign into the website to view the pic.

Would you be able to inbox me the code so I can copy/paste it into Godot?

Made a pastebin with it, it kept the formatting there

https://pastebin.com/U4rGBS8Y

Great stuff, you get a Gold star :D
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 19 Apr 2023, 06:52

Well I copied/pasted the above code and it gives the following error.

 
Attachments
Screenshot from 2023-04-19 07-50-51.png
Error messages
Screenshot from 2023-04-19 07-50-51.png (165.69 KiB) Viewed 404 times
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Beerkeg
Standard Member
Posts: 923
Joined: 05 May 2017, 20:51
Contact:
Estonia

Achievements

Programming help required (GDscript or C#)

Post by Beerkeg » 19 Apr 2023, 11:02

I did notice a mistake in my last pastebin, heres the new one https://pastebin.com/pWsMuWdk

also imgur link for what my output looks like https://imgur.com/a/kAPO1VJ

I think you're getting the error since you've only indented lines 7-9, but you need it for all of them. If my imgur link works, you can see what I did there. I don't really see anything else that would throw the error.

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 19 Apr 2023, 17:53

Beerkeg wrote:
19 Apr 2023, 11:02
I did notice a mistake in my last pastebin, heres the new one https://pastebin.com/pWsMuWdk

also imgur link for what my output looks like https://imgur.com/a/kAPO1VJ

I think you're getting the error since you've only indented lines 7-9, but you need it for all of them. If my imgur link works, you can see what I did there. I don't really see anything else that would throw the error.

Thanks @Beerkeg, I appear to have sorted it, with the help of a guy on the Godot Community.

But thanks for trying your best to help :)
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 20 Apr 2023, 07:02

Code: Select all

 
extends KinematicBody2D

export var Speed:int
var velocity = Vector2()
var screensize = Vector2 (480, 720)
func get_input():
velocity = Vector2()
if Input.is_action_pressed ("ui_left"):
velocity.x -= 1
 if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_up"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.x += 1
if velocity.length() > 0:
velocity = velocity.normalized() * Speed

Brings up an error, summat about the Area2D not being there.

HELP before I tear up the book and shove it somebody's arse.
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

User avatar
Beerkeg
Standard Member
Posts: 923
Joined: 05 May 2017, 20:51
Contact:
Estonia

Achievements

Programming help required (GDscript or C#)

Post by Beerkeg » 20 Apr 2023, 09:05

Okay, a few things...
1. Area2D and KinematicBody2D are not interchangeable, they're completely different classes for different purposes. So you can't just use the same bit of code for the other class.
2. What version of the godot engine are you using and what version is your book on? Several things change between versions, for example check out this doc https://docs.godotengine.org/en/stable/ ... -resources
3. Programming in high-level languages (like C#, GDscript and many others) sucks, because most of the time you're not programming, you're googling for methods and classes, syntax, because all the hard bits of code have been done for you. For example if your book tells you to use Area2D, but hasn't told you what it does, then you look it up, find godot's docs about it and read up on it. It'll tell you what parameters it has, it'll break down all the methods for you and it'll have code examples and tutorials. https://docs.godotengine.org/en/stable/ ... rea2d.html
Edit: Just noticed they also have example projects you can download where everything is already done and you can play around with the example to better understand what things do https://docs.godotengine.org/en/stable/ ... ea_2d.html (At the very bottom)

You don't learn to code from a book, you learn it by failing to get something to work again and again until you've learned from all your fails.

User avatar
Rich-Allen1976
Dark Blue Member
Dark Blue Member
Posts: 6293
Joined: 03 Sep 2022, 14:31
Great Britain

Achievements

Programming help required (GDscript or C#)

Post by Rich-Allen1976 » 20 Apr 2023, 14:07

Beerkeg wrote:
20 Apr 2023, 09:05
Okay, a few things...
1. Area2D and KinematicBody2D are not interchangeable, they're completely different classes for different purposes. So you can't just use the same bit of code for the other class.
2. What version of the godot engine are you using and what version is your book on? Several things change between versions, for example check out this doc https://docs.godotengine.org/en/stable/ ... -resources
3. Programming in high-level languages (like C#, GDscript and many others) sucks, because most of the time you're not programming, you're googling for methods and classes, syntax, because all the hard bits of code have been done for you. For example if your book tells you to use Area2D, but hasn't told you what it does, then you look it up, find godot's docs about it and read up on it. It'll tell you what parameters it has, it'll break down all the methods for you and it'll have code examples and tutorials. https://docs.godotengine.org/en/stable/ ... rea2d.html
Edit: Just noticed they also have example projects you can download where everything is already done and you can play around with the example to better understand what things do https://docs.godotengine.org/en/stable/ ... ea_2d.html (At the very bottom)

You don't learn to code from a book, you learn it by failing to get something to work again and again until you've learned from all your fails.

The book is for Godot 3, I'm using Godot 3.5.2.

Clearly the author's an idiot.
"He's not the Messiah, he's a very naughty boy!" (Michael Palin in Monty Python's "Life of Brian")
"I am speaking UK English in a Sheffield accent!" (Me most of the time I'm on the landline or mobile)





 

Post Reply