Jump to content

Recommended Posts

Posted

If I use const alone to declare a variable in a func there seems to be some behavior I don't quite understand.

When the variable is declared globally then this error is thrown: "error: $my_var previously defined as a non-'Const' var."   So it appears to check the global scope first.  Seems like it should check local first, right?

Global $my_var = "Hello World"

_my_func()

Func _my_func()
  Const $my_var = "Good Bye World" ; error: $my_var previously defined as a non-'Const' var.
EndFunc

Then this snippet makes it seem to me that the local scope is checked rather than global.

_my_func()

ConsoleWrite($my_var & @CRLF) ; warning: $my_var: possibly used before declaration.

Func _my_func()
  Const $my_var = "Good Bye World"
EndFunc

Is this all correct?

Posted

Because the variable $my_var has a Global scope. If you need to re-use the same name in your function set the scope by using

Local Const $my_var = "Good by World!"

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

The errors are correct. Const doesn't define/re-define scope.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 2/2/2014 at 4:11 PM, jaberwacky said:

OK, I can understand that now.  Thanks.

No problem. It's an easy mistake to make. At least you're using this keyword.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...