OpenSimplexNoise error

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By verbaloid

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)
:bust_in_silhouette: Reply From: exuin

You can’t assign variables like that outside of functions. Neither can you call functions like “print” outside of functions.

:bust_in_silhouette: Reply From: DaddyMonster

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:

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

verbaloid | 2022-01-15 10:49

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.

DaddyMonster | 2022-01-15 11:00