Welcome to %s forums

BrainModular Users Forum

Login Register

Starting with Scripts...

I need help on a Patch
Post Reply
Clearscreen
Member
Posts: 482
Location: Australia
Contact:

Unread post by Clearscreen » 10 Jun 2010, 12:52

I've finally started to try and learn some programming and going through the Usine Scripting Tutorial PDF I can't get the example on page 44 to work.

The script compiles but I get a 'ErrorProcess' message in Usines console so I must have something wrong in the Procedure Process, but I can't work out what it might be... here's what I've got:
Procedure Process;
begin
if GetLength(ArrayIn) > 0 then //to prevent the procedure running when there's no input
for i := 0 to (len-1) do
Arraytemp := GetDataArrayValue(ArrayIn, i);
SetDataArrayValue(ArrayOut, i, Arraytemp);
end;

anybody care to take a look and tell me what I'm doing wrong? I'm EXTREMELY new to programming so I'm expecting this to be something really basic, and I apologise in advance for my ignorance...

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 10 Jun 2010, 13:09

First I think you should set the length of both ArrayOut and Arraytemp to (at least) the same length as ArrayIn before assigning values to them. From this snippet I can't tell whether you've done that earlier in program, for instance in Init?

Another thing is that you shouldn't really have code like this within the Process procedure, as it will be executed every block. Use the Callback procedure to trigger things when input changes.
Bjørn S

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 10 Jun 2010, 15:40

I've start with script too and I'm in the same situation ( never program anything..)
i've try the example p44 and i've also problem with :
compile but error and don't work..

Code: Select all

//////////////////////////
// 
/////////////////////////
// parameters declaration
var arrayIn   : Tparameter;
var arrayOut  : Tparameter;



// initialisation : create parameters
procedure init;
begin 
 ArrayIn := CreateParam('array In',ptArray);
 SetIsOutput(ArrayIn,false);// 

 ArrayOut := CreateParam('array Out',ptArray);
 SetIsInput(ArrayOut,false);
end;

// Callback procedure

Procedure Callback(N:integer); 
begin


end;

// Global variables
Var I, len: integer;
var ArrayTemp : array of single;

//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
Len := GetLength(arrayin);
SetLength(ArrayOut, len); 
For i:= 0 to len-1 do // loop for all indexes
  Begin
ArrayTemp[i] := GetDataArrayValue(ArrayIn, i); // store the ith element of ArrayIn to the ith element of ArrayTemp
SetDataArrayValue(ArrayOut, i, Arraytemp[i]); // send the same element to ArrayOut
  End; 
end;
purpose is we simply want to pass through an array from ArrayIn to
ArrayOut.

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 10 Jun 2010, 15:53

I can take a closer look tonight - must go now.
Bjørn S

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 10 Jun 2010, 16:25

cool, no problems..
I've add this line in the main proc

Code: Select all

If GetLength(arrayin)>0 then
begin
( cause the Callback procedure is not really explained in the pdf , )

so the problem is not here

Code: Select all

//////////////////////////
// 
/////////////////////////
// parameters declaration
var arrayIn   : Tparameter;
var arrayOut  : Tparameter;



// initialisation : create parameters
procedure init;
begin 
 ArrayIn := CreateParam('array In',ptArray);
 SetIsOutput(ArrayIn,false);// 

 ArrayOut := CreateParam('array Out',ptArray);
 SetIsInput(ArrayOut,false);
end;

// Callback procedure

Procedure Callback(N:integer); 
begin


end;

// Global variables
Var I, len: integer;
var ArrayTemp : array of single;

//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
If GetLength(arrayin)>0 then
begin
Len := GetLength(arrayin);
SetLength(ArrayOut, len); 
For i:= 0 to len-1 do // loop for all indexes
  Begin
ArrayTemp[i] := GetDataArrayValue(ArrayIn, i); // store the ith element of ArrayIn to the ith element of ArrayTemp
SetDataArrayValue(ArrayOut, i, Arraytemp[i]); // send the same element to ArrayOut
  End; 
end;    
end;

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 10 Jun 2010, 21:13

Good mates if joining the script classroom, i won't be alone asking always bsork hehe ;)

i think it comes from the fact that the variable array used to temp store had not is length set as well as arrayOut.

for parameters we use Setlength(arrayOut,len);

for variable array must use SetArrayLength(ArrayTemp,len);

i would try to add this second line when setting sizes.

btw this is a perfect exemple of a wonder I had:

do we really need to have a variable array, can't we direct use in/out parameters bsork? im often asking myself if i need to
use a third in between variable array when dealing with in/out arrays....what's the point?


Also it's an exemple, but as bsork said it would be better to make a callback process (process on input changes) rather than
a process (always compute), but let's see first how to get it working.. At the time the pdf was made there wasn't yet maybe the callback. for me callback is equivalent to "has changed" at patch level:


Procedure Callback (n : integer);
begin

if (n= my_input) then begin // = if ie arrayIn, has changed and only, process foloowing code;
....
end;

end;

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 10 Jun 2010, 22:48

great ! it works this way , you were right
now I've to understand this callback procedure.....:)

Code: Select all

//////////////////////////
// 
/////////////////////////
// parameters declaration
var arrayIn   : Tparameter;
var arrayOut  : Tparameter;



// initialisation : create parameters
procedure init;
begin 
 ArrayIn := CreateParam('array In',ptArray);
 SetIsOutput(ArrayIn,false);// 

 ArrayOut := CreateParam('array Out',ptArray);
 SetIsInput(ArrayOut,false);
end;

// Callback procedure

Procedure Callback(N:integer); 
begin
            

end;

// Global variables
Var I, len: integer;
var ArrayTemp : array of single;

//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
If GetLength(arrayin)>0 then
begin
Len := GetLength(arrayin);
SetLength(ArrayOut, len); 
SetArrayLength(ArrayTemp,len);
For i:= 0 to len-1 do // loop for all indexes
  Begin
ArrayTemp[i] := GetDataArrayValue(ArrayIn, i); // store the ith element of ArrayIn to the ith element of ArrayTemp
SetDataArrayValue(ArrayOut, i, Arraytemp[i]); // send the same element to ArrayOut
  End; 
end;    
end;

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 10 Jun 2010, 22:57

cool! if often the error, set the size or min/max of arrays.

basically callback trigg the proc only on change, and the same code would look like this:

//////////////////////////////////////////////////////////////////////////
// Callback procedure
Procedure Callback(N:integer);
Var I, len: integer;
var ArrayTemp : array of single;
BEGIN
if (n=arrayIn) then begin
Len := GetLength(arrayin);
SetLength(ArrayOut, len);
SetArrayLength(ArrayTemp,len);
For i:= 0 to len-1 do Begin
ArrayTemp := GetDataArrayValue(ArrayIn, i);
SetDataArrayValue(ArrayOut, i, Arraytemp);
End;
end;
END;
////////////////////////////////////////////////////////
don't need to check the array length is >0 as in old "process" mode, because if the script detects a change that mean the array
has a more than zero size i think if im not wrong.
//////////////////////////////////////////////////////////////////////////////////////// but me usually i don't use temp array:
BEGIN
if (n=arrayIn) then begin
Len := GetLength(arrayin);
SetLength(ArrayOut, len);
For i:= 0 to len-1 do Begin
SetDataArrayValue(ArrayOut, i, GetDataArrayValue(ArrayIn, i));
End;
end;
END;
////////////////////////////////////////////////////////

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 10 Jun 2010, 23:32

[Edit: Ooops! you've both been busy while I had to take care of my son who had a nightmare while I was still not finished writing. I keep the text as it is anyway.]

You're right: Adding both SetLength(ArrayOut, len) and SetArrayLength(ArrayTemp, len) solves the ErrorProcess problem.

You question the need for arrays for "internal" use only. It might not be the case with the new script engine, but in the old one it did take more CPU to read inputs than to do the same in an internal array. I haven't really done any stress testing to check whether that's still true, but if that's so and the input data is seldom updated compared to how often computations are made on the same data you save cycles and often get (IMO) a much more readable program as a bonus.

With the exercise script in question here, you could say that the use of ArrayTemp is doubly superfluous: Not only is it just used to store data that is just sent out unaltered through ArrayOut, but if you want to "mirror" ArrayIn just remove SetIsOutput(ArrayIn, FALSE) and you get a "through" of the data. Another thing with the use of ArrayTemp is that even if you want to do something with the data between in and out, you could just reuse a single variable for that - no need to fill an array, Or for simple computations just write something like this: SetDataArrayValue(ArrayOut, i, round(GetDataArrayValue(ArrayIn, i)) * 10) .

Likening Callback to "has changed" is also spot on! :)
Bjørn S

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 10 Jun 2010, 23:44

thanks for the remider master bsork . will test internal/ext comparaisons but it must be better for freq udtates Out with less updates in i guess it's still the case.

hehe yup i said that to myself when i atlast understood what was "callback " " Ah ok it's like a 'has changed ' hehe

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 11 Jun 2010, 00:26

yep 23fx , it's a good comparison
bsork , hope your son will have a good dream now

I've to study all that , and i suppose I'll make a " why you don't have to be afraid about script lesson "

begin
thanks := ( from me) ;
end;

Clearscreen
Member
Posts: 482
Location: Australia
Contact:

Unread post by Clearscreen » 11 Jun 2010, 03:50

Hey guys - thanks so much for your help - I had set the in and out array lengths in the inits, but i hadn't done
SetArrayLength(ArrayTemp, len);
anywhere. Works a treat now - also a good tip to remember that if you have ErrorProcess in the console it's likely the array lengths aren't set...

Also, I'd used the main process as I got confused after reading the fastcallback procedure desription in the guide saying it used more CPU.... I was pretty tired last night! DOH! I'll be using the callback I think from now on... interested to hear more about the comparisons of using internal/external though.

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 11 Jun 2010, 15:27

Maybe time to think of an update of the tutorial..?
Bjørn S

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 11 Jun 2010, 15:58

yep, I've contacted B.Moussay but seems busy right now and don't answer yet
so if anyone.....;)

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 14 Jun 2010, 08:22

I should've mentioned it in my previous post, but.. Now that we have the Wiki up and running, maybe the updated tutorial should be created in there instead of as a doc/PDF?
Bjørn S

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 14 Jun 2010, 10:16

you're right Bsork , could be a good way..
I'm ready to help ( following slowly my own comprehension..) and i was thinking about adding concretes examples too inside or as external lessons..
but i need at least help to know exactly what's to be updated in the actual one....?

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 14 Jun 2010, 10:36

The obvious thing to start with when updating would be the new "structure" with the Callback, Process and Destroy procedures. Olivier has written the basics describing these things as compared to the old scripts in the scripting Wiki already, but that sort of information is so fundamental as it should be as up to date as possible in a tutorial to avoid confusion.
Bjørn S

Clearscreen
Member
Posts: 482
Location: Australia
Contact:

Unread post by Clearscreen » 15 Jun 2010, 07:02

An update would be great! It'd be good to see some more clearly explained examples I reckon...

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests