Problem reading file

Hi I have a rollout with a few buttons and editText. The buttons is for getting a path and the editText is to display the path. The problem is when I press the button to get a CSV-file I get an error the first time. But if I run the script again there is no error. I would be realy greatfull if someone could help me!

Here is part of the code that is causing the error:

button setCsvPath "CSV" pos:[7,5] across:2
editText csvPathText pos:[65,8] fieldWidth: 200

on setCsvPath pressed do
(
csvPath = getOpenFileName caption:"Select CSV" types:"List(*.csv)|*.csv"
if csvPath != undefined then
(
global csvFile= openFile (csvPath) mode:"r"
csvPathText.text = csvPath
countLines (csvFile)
collectObj()
)
)

fn countLines file =
(
orgFilePos = filePos file
seek file 0

while not eof file do
(
numLines += 1
skipToNextLine file
)
seek file orgFilePos
)

Here is the error:
countLines: undefined
>> MAXScript Rollout Handler Exception:
-- Type error: Call needs function or class, got: undefined <<

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Andreas G's picture

That is really good pointers!

That is really good pointers! I'm learning by my self so I appreciate this kind of help. Thank you so much!

Have a great day!

miauu's picture

.

This error occurs because the CountLines fn is defined after the on setCsvPath pressed do

When the code in the on pressed is executed there is no CountLines variable defined, and the error pops up.

Move the fn countLines file =
before the on setCsvPath pressed do and the error will disappear.

By the way, it is good to write scripts this way(good but not necessary):

(
	global rol_myRollout
	try(destroyDialog rol_myRollout)catch()
	rollout rol_myRollout "rollout name"
	(
		--	variables first
		local myLocalVAr = undefined
		--	filter functions (if you need them here)
 
		-- UI controls
		button setCsvPath "CSV" pos:[7,5] across:2
		editText csvPathText pos:[65,8] fieldWidth: 200
 
		--	functions
 
		--	events
		on setCsvPath pressed do ...
 
	)
	createdialog rol_myRollout 	
)

This way, when you start writing the events for the UI all variables and functions will be defined.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.