Variables syntax

Variables syntax

Anonymous
Not applicable
2,075 Views
6 Replies
Message 1 of 7

Variables syntax

Anonymous
Not applicable

Hi all,

 

I read (Max help) that in a block I can use "local" before the variable, or not (and Maxscript understands what kind of variable it is).

local variable1 = 10

or

variable1 = 10

 

BUT in my rollout, if I don't write "local", I get :

-- erreur de syntaxe: à =, attendu name

-- dans ligne: variable1 = 1

(mixed english and french)

 

So, I write "local",

BUT when I do a simple addition (with the same variable) I get another error :

local variable1 = 10

local variable1 = variable1 + 20

I get :

-- Non ""+"" fonction pour undefined

 

😩😩😩😩

 

Thanks for your help

0 Likes
Accepted solutions (1)
2,076 Views
6 Replies
Replies (6)
Message 2 of 7

miauuuu
Collaborator
Collaborator

Someone will exaplain it better, but the basic rule is:

(
	--	"you may not specify"
	var100 = 100
	var200 = 200
	local var300 = 300
	
	
	rollout rol_ ""
	(
		--	"YOU MUST SPECIFY LOCAL OR GLOBAL"
		local var1 = undefined
		local var2 = 10
		local var3 = 20
		--
		
		button btn_01 "My Button" 
		
		
		function M_FN a b =
		(
			--	"you may not specify"
			var6 = 12
		)
		
		on btn_01 pressed do
		(
			--	"you may not specify"
			var4 = 5
			var5 = 8
			
		)
	)
	createdialog rol_ 	
)
https://miauu-maxscript.com/
0 Likes
Message 3 of 7

Anonymous
Not applicable

Thanks  miauuuu

Ok for that.

 

But why (in the rollout) :

local variable1 = 10

local variable1 = variable1 + 20

I get :

-- Non ""+"" fonction pour undefined

 

However, if it is at the top of the script

variable1 = 10

variable1 = variable1 + 20

>>> 30

 

0 Likes
Message 4 of 7

istan
Advisor
Advisor

Since you declare the local variable 'variable1' twice. The scripting engine treats the 2nd 'variable1' like it was just declared (overriding the 1st declaration). As the variable is without any content yet, the only valid error is 'undefined'. I'd be lucky, when errors are shown in the proper code line at least! Most other interpreter errors show at the very end of functions or braces and you have to assume what it caused.

0 Likes
Message 5 of 7

Anonymous
Not applicable

Ok istan, thanks

 

But what is the way ?

 

What I do now :

local variable1 = 10

local variable2 = variable1 + 20

local variable1 = variable2

 

I find it stupid but that runs

0 Likes
Message 6 of 7

Swordslayer
Advisor
Advisor
Accepted solution

The 'local' here is to set the scope, you use it only once for any given variable - from that line on if the variable name is used, the innermost variable is used. The explicit 'local' is there to prevent  overriding your/someone else's variables.

-- someone else's code
fn showMessage msg = messageBox msg title:"CustomTitle"

-- your code
(
	showMessage = off -- variable already exists in outer scope, it is used instead of making a new local one
	anotherVariable = 1 -- variable doesn't exist yet, it's created in the scope where it was first encountered, which is here
)
showMessage --> returns 'off', it's a global even though you 'wanted' to make  a temporary local
anotherVariable --> undefined, querying this in global scope also creates a new global variable so next time you run the code, this variable will persist


Which is why it's better to be always explicit about locals:

-- someone else's code
fn showMessage msg = messageBox msg title:"CustomTitle"

-- your code
(
	local showMessage = off -- explicit scope, this is now a separate variable
	local anotherVariable = 1 -- variable doesn't exist yet, it's created in the scope where it was first encountered, which is here
	anotherVariable += 1 -- we've modified the variable
	::showMessage "Custom message" -- you can still access globals even when you have locals that shadow them
)
showMessage --> returns the function since the local didn't replace it
anotherVariable --> undefined, will still be undefined on successive runs
Message 7 of 7

Anonymous
Not applicable

everything is clear

 

thank you all

0 Likes