Trying to understand George Functions & Returns

A forum dedicated to George scripting questions
Post Reply
blastframe
Posts: 8
Joined: 29 May 2016, 17:44

Trying to understand George Functions & Returns

Post by blastframe »

Hello,
I'm new to scripting with George and trying to load images from a text file. I have TVPaint requesting the import file correctly and then sending the resulting path to a function that reads the text file (based on this function from the George wiki). My issue is that when I try to store each line from the text file in a LOCAL array and return it, it doesn't print as I'd expect. If I check its values before the return, however, it prints correctly. Can George return an array? If so, can someone please help me understand how to return this array correctly? Thank you!

Code: Select all

tv_ReadUserString fileRequest 0
parse result path
tv_ReqFile "Select file to import|"path"||*.csv"
parse result file

if (cmp(file,"Cancel") == 0)

  array1 = file_readTextFile(file)

  //set to 2 for testing purposes
  rowLength = 2

  FOR i = 0 TO rowLength
    tv_warn "array1 " i " = " array1[i]
    //returns array1 0 = array1.0
    //returns array1 1 = array1.1
    //returns array1 2 = array1.2
  END

end

FUNCTION file_readTextFile(textFilePath)
	LOCAL myReturn fileExists i line text
	tv_WriteTextFile "Exists" textFilePath
	fileExists = result

	IF CMP(fileExists,textFilePath)==1
		loop = 1
		i=0
    //while loop iterates until end of file
		WHILE loop==1
			tv_readtextfile  i textFilePath
			PARSE result line text

			IF CMP( line, "EOF"  ) == 1
				loop=0
        rowLength = i
			ELSE
				length = LEN(text)
        // length of line of text
				length=length-2
				text = CUT(text,1,length)
        //'text' variable is a line of text

        myReturn[i]=text
			END
			i=i+1
		END
	END
  	print myReturn[0]
  	//prints correctly
	RETURN myReturn
END

User avatar
Hironori Takagi
Posts: 279
Joined: 14 May 2018, 10:15
Location: Tokyo, Japan
Contact:

Re: Trying to understand George Functions & Returns

Post by Hironori Takagi »

In my case, I would write this as:
(I am not good at using the procedure)
Is there any reason to return an array using a procedure?

Code: Select all

tv_ReadUserString fileRequest 0
parse result path
tv_ReqFile "Select file to import|"path"||*.csv"
parse result file

IF CMP(file,"Cancel") == 0
	
	tv_WriteTextFile "Exists" file
	fileExists = result
	IF CMP(fileExists,file)==1
		loop = 1
		linecount = 0
		WHILE loop 
			tv_readtextfile linecount file
			PARSE result line text
			IF CMP(line,"EOF") == 1
				loop=0
			ELSE
				text[linecount] = text
				linecount = linecount + 1
			END
		END
	END
	
	FOR i = 0 TO linecount
		tv_warn "text " i " = " text[i]
	END
END
TVPaint 11.7.2(Nov 14 2023), Windows11 Pro, HP Spectre x360 Convertible 14-ea0xxx / TVPaint 11.7.1(Dec 22 2022) Mac OS 11.6, Apple MacBookPro M1 2020
blastframe
Posts: 8
Joined: 29 May 2016, 17:44

Re: Trying to understand George Functions & Returns

Post by blastframe »

Thank you for the reply, Hironori. I tend to separate things that might get reused in a script (like loading) into functions, but since there will only be one text file in this case, you're right: it doesn't need to be its own function. I find it more readable & easier to debug when different phases of the script are broken out with returns.

So is George able to return variables other than strings and numbers? I haven't found a George script with this, so I'm assuming it cannot.
User avatar
Hironori Takagi
Posts: 279
Joined: 14 May 2018, 10:15
Location: Tokyo, Japan
Contact:

Re: Trying to understand George Functions & Returns

Post by Hironori Takagi »

To return an array using a procedure, I think that it will be a way to PARSE in the main program by doing a CONCAT with a space in between.
There are cases where the string contains spaces, so it is troublesome to consider the correspondence.
TVPaint 11.7.2(Nov 14 2023), Windows11 Pro, HP Spectre x360 Convertible 14-ea0xxx / TVPaint 11.7.1(Dec 22 2022) Mac OS 11.6, Apple MacBookPro M1 2020
Post Reply