0 votes

I'm using a shader and I need to add OpenSimplexNoise, but the script gives me error one Line 2, on noise.seed = randi():
'Unexpected token: Identifier:noise'

What am I doing wrong?

var noise = OpenSimplexNoise.new()
noise.seed = randi()
noise.octaves = 4
noise.period = 20.0
noise.persistence = 0.8

# Sample
print("Values:")
print(noise.get_noise_2d(1.0, 1.0))
print(noise.get_noise_3d(0.5, 3.0, 15.0))
print(noise.get_noise_4d(0.5, 1.9, 4.7, 0.0))

tool
extends Sprite

func _ready():
set_notify_transform(true)

func _notification(what):
  if what == NOTIFICATION_TRANSFORM_CHANGED:
    get_material().set_shader_param("scale", scale)
in Engine by (53 points)

2 Answers

+1 vote
Best answer

Just to flesh out the previous answer. I'm guessing you, like me, have a python background where this sort of thing is valid. In gdscript, think of the script you're writing as being within a class.

Outside a method, all you can do is declare member variables which have a wide scope. Everything else goes within methods. The convention is to put tool, then extends, then member variables, then methods. In this case, it could look something like this:

tool
extends Sprite

func make_noise():
    var noise = OpenSimplexNoise.new()
    noise.seed = randi()
    noise.octaves = 4
    noise.period = 20.0
    noise.persistence = 0.8

    # Sample
    print("Values:")
    print(noise.get_noise_2d(1.0, 1.0))
    print(noise.get_noise_3d(0.5, 3.0, 15.0))
    print(noise.get_noise_4d(0.5, 1.9, 4.7, 0.0))

func _ready():
    make_noise()
    set_notify_transform(true)

func _notification(what):
  if what == NOTIFICATION_TRANSFORM_CHANGED:
    get_material().set_shader_param("scale", scale)

Take a moment to read this intro in the docs to get yourself up to speed on the essentials:

https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html

by (2,156 points)
selected by

Thank you so much! Yes, I'm getting used to new code structure)

Stick with it, you'll get there! Once you get a handle on it gdscript is a pleasure to work with and you'll be flying.

0 votes

You can't assign variables like that outside of functions. Neither can you call functions like "print" outside of functions.

by (8,528 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.