Re: [NTLK] basic NewtonScript question

From: Paul Guyot (pguyot_at_kallisys.net)
Date: Tue Mar 09 2004 - 21:33:26 PST


Aux environs du 10/03/04 à 16:40 +1300, sous le titre "[NTLK] basic
NewtonScript question", Nidak prit sa plus belle plume pour écrire
les mots suivants:
>Hello to all.
>
>Lately I start programming using NTK. But having trouble with Array.
>
>I want to create a program which has two buttons.
>One button (say Btn1) is used to generate random number
>from 1 to 6 then store its value to an array. Another button
>(say Btn2) is used to read the numbers from that array one by
>one.
>
>My problem is: I don't know how to create an Array that can be
>accessed from Btn1 and Btn2.
>
>I know this is very basic stuff but I couldn't figure it out.
>Could somebody help me please?

Your buttons are widgets part of a base view (probably a protoFloater
or something). Open the browser of the protoFloater by
double-clicking it. Click the proto on the left and then choose New
slot from the browser window and create your slot (for example call
it someArray). There are two possibilities then.

You can leave the array to nil and create the array in the
buttonClickScript of the first button. It also means you have to
handle the case when the array is nil in the second button's script.

btn1.buttonClickScript :
func ()
begin
        // If the array is nil, create it.
        if (someArray = nil) then someArray := [];

        // Add a random number between 1 and 6.
        AddArraySlot( someArray, Random(1, 6) );
end;

btn2.buttonClickScript :
func ()
begin
        if (someArray) then
        begin
                // Handle the case where the array is not nil.
                // (it can be empty, tho).
        end else begin
                // Handle the case where the array is nil.
        end
end;

You can define it to []. But this array will be locked when the
package will be running on the Newton, so you will need to clone it.
This will mean unnecessary clones.

btn1.buttonClickScript :
func ()
begin
        // Clone the array if it's empty
        if (Length(someArray) = 0) then someArray := Clone(someArray);
        // You could also write:
        // if (Length(someArray) = 0) then someArray := [];

        // Add a random number between 1 and 6.
        AddArraySlot( someArray, Random(1, 6) );
end;

btn2.buttonClickScript :
func ()
begin
        // Handle the case where the array is not nil.
        // (it can be empty, tho).
end;

HTH,

Paul

-- 
Philosophie de baignoire - consultations sur rendez-vous.
NPDS/NewtonOS: http://newton.kallisys.net:8080/
Apache/FreeBSD: http://www.kallisys.com/
-- 
This is the NewtonTalk list - http://www.newtontalk.net/ for all inquiries
Official Newton FAQ: http://www.chuma.org/newton/faq/
WikiWikiNewt for all kinds of articles: http://tools.unna.org/wikiwikinewt/


This archive was generated by hypermail 2.1.5 : Tue Mar 09 2004 - 22:00:01 PST