Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Complex button + procedure question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nflavour




PostPosted: Thu Jan 08, 2009 10:27 pm   Post subject: Complex button + procedure question

I have a few body procedures. and all of them uses the same button procedure (GUI.CreateButton). And since CreateButton requires a procedure when it's click, so i created one. That procedure should do the following:
    add the price total based on the BODY procedure its in
    and add the number of items based on the BODY procedure its in

Since the Createbutton procedure doesn't accept with procedures parameters. So I was wondering how do I make a button work in this case?
i am just stuck here..
i thought of this
Turing:
var total,totalnum:int:=0
%procedure
procedure simple (price,num)
     total+=num*price
     totalnum+=num
end simpe
%button procedure
procedure buttons(price,num)   
     var buttton1 := GUI.CreateButton (0,0,20, "button1", simple(price,num)) %<--invalid
    loop
        exit when GUI.ProcessEvent
    end loop
end buttons
%body procedure <--Many of these
procedure body1
     const price:=110   %<--- These values are different in each procedure
     const num:= 5      %<--- These values are different in each procedure
     buttons(price,num)
end body1


I know i can use buttonwait, or mousewhere, but I want to use GUI.CreateButton.
Thanks in Advance.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Jan 08, 2009 10:57 pm   Post subject: RE:Complex button + procedure question

I think you meant to do
code:

GUI.CreateButton (0,0,20, "button1", body1)

and
code:

const price:=110   %<--- These values are different in each procedure
     const num:= 5      %<--- These values are different in each procedure
     simple(price,num)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Laplace's Demon




PostPosted: Thu Jan 08, 2009 11:05 pm   Post subject: Re: Complex button + procedure question

uhhh...

Well I'm not exactly sure what you're trying to do or asking... the code you provided is scrambled all over...

-Spelling errors causing errors in the procedure
-Lack of type for parameters should be price : int, num : int
-Trying to call the procedure without inputting values for the parameters (you are using the same variable name as the parameter declaration)
-Failure to import GUI

I guess your trying to make the same button do the math based on which "body procedure" your in. Your on the right track with parameters, but you just don't have it set up right. First of all rename your variables. You have overlap when declaring separate parameters. Instead of declaring new variables in each "body procedure" you want to set global variables to the desired values then call them that way. The simplest way to do it is to remove the parameters from your simple procedure and just use global variables for it. Have each body procedure alter those global variables.

I'm not sure why you have it set up the way you do, but try fixing your errors and reworking it to use global variables, just simpler that way.



edit: Or just listen to tony, he seems to be kind of on the ball when it comes to computer science.
nflavour




PostPosted: Thu Jan 08, 2009 11:14 pm   Post subject: Re: Complex button + procedure question

Tony @ Thu Jan 08, 2009 10:57 pm wrote:
I think you meant to do
code:

GUI.CreateButton (0,0,20, "button1", body1)

and
code:

const price:=110   %<--- These values are different in each procedure
     const num:= 5      %<--- These values are different in each procedure
     simple(price,num)

actually, i meant when i click the button it will do the math calculation. Not calling the body procedure when i click the button. Thanks
Laplace's Demon @ Thu Jan 08, 2009 11:05 pm wrote:
uhhh...

Well I'm not exactly sure what you're trying to do or asking... the code you provided is scrambled all over...

-Spelling errors causing errors in the procedure
-Lack of type for parameters should be price : int, num : int
-Trying to call the procedure without inputting values for the parameters (you are using the same variable name as the parameter declaration)
-Failure to import GUI

I guess your trying to make the same button do the math based on which "body procedure" your in. Your on the right track with parameters, but you just don't have it set up right. First of all rename your variables. You have overlap when declaring separate parameters. Instead of declaring new variables in each "body procedure" you want to set global variables to the desired values then call them that way. The simplest way to do it is to remove the parameters from your simple procedure and just use global variables for it. Have each body procedure alter those global variables.

I'm not sure why you have it set up the way you do, but try fixing your errors and reworking it to use global variables, just simpler that way.

edit: Or just listen to tony, he seems to be kind of on the ball when it comes to computer science.

Sorry for the Confusion and horrible coding, but you did get what I meant. And I understand what you mean, to set global variables, but when i click the button, and it calls the simple procedure. How will the simple procedure know which body procedure it's under, so how will it know what are the prices and amount? Is there a way to check who it was called by?
Thanks
Tony




PostPosted: Thu Jan 08, 2009 11:40 pm   Post subject: Re: Complex button + procedure question

nflavour @ Thu Jan 08, 2009 11:14 pm wrote:
when i click the button, and it calls the simple procedure. How will the simple procedure know which body procedure it's under, so how will it know what are the prices and amount?

It doesn't, because there's not link between "simple" and "body1". In fact, "body1" is not called from anywhere, in the code you posted. Maybe there's more to your project, but as it stands, it's hard to be certain of your intentions.

There is no stack trace available in Turing -- a procedure doesn't know where it was called from, unless you tell it somehow. A typical implementation is that each GUI button would have a unique procedure, so then that procedure can assume that it was triggered by a specific button (that button being the only way to call it). You can then setup your values in that procedure, and call something more generalized, using arguments.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nflavour




PostPosted: Thu Jan 08, 2009 11:45 pm   Post subject: Re: Complex button + procedure question

Tony @ Thu Jan 08, 2009 11:40 pm wrote:
nflavour @ Thu Jan 08, 2009 11:14 pm wrote:
when i click the button, and it calls the simple procedure. How will the simple procedure know which body procedure it's under, so how will it know what are the prices and amount?

It doesn't, because there's not link between "simple" and "body1". In fact, "body1" is not called from anywhere, in the code you posted. Maybe there's more to your project, but as it stands, it's hard to be certain of your intentions.

There is no stack trace available in Turing -- a procedure doesn't know where it was called from, unless you tell it somehow. A typical implementation is that each GUI button would have a unique procedure, so then that procedure can assume that it was triggered by a specific button (that button being the only way to call it). You can then setup your values in that procedure, and call something more generalized, using arguments.


oh, Thanks, basically i need to create different button for each body procedure and the buttons would get its own simple procedure.
Nick




PostPosted: Fri Jan 09, 2009 6:36 am   Post subject: RE:Complex button + procedure question

the best thing to do: ditch Turing's built in GUI

what you want to do is to create your own button (not as hard as it sounds)

basically you wanna draw a box, then check to see if the mouse is within the box and is clicked

what you will need is a draw procedure and a checking procedure (need to make use of the Mouse module)

the basic way of explaining is:

a the button will have an x, a y, a second x (x2) and a second y (y2)

the mouse will also have a y (my), an x (mx), and also a button status (mb) (Mouse.Where(mx,my,mb))

then you check to see if the mx is x<=mx<=x2 and check if y<=my<=y2 then you check if mb > 0

if the following is true, then the button is clicked and therefore the procedure is called

that help at all?
nflavour




PostPosted: Sat Jan 10, 2009 12:08 pm   Post subject: Re: RE:Complex button + procedure question

Nick @ Fri Jan 09, 2009 6:36 am wrote:
the best thing to do: ditch Turing's built in GUI

what you want to do is to create your own button (not as hard as it sounds)

basically you wanna draw a box, then check to see if the mouse is within the box and is clicked

what you will need is a draw procedure and a checking procedure (need to make use of the Mouse module)

the basic way of explaining is:

a the button will have an x, a y, a second x (x2) and a second y (y2)

the mouse will also have a y (my), an x (mx), and also a button status (mb) (Mouse.Where(mx,my,mb))

then you check to see if the mx is x<=mx<=x2 and check if y<=my<=y2 then you check if mb > 0

if the following is true, then the button is clicked and therefore the procedure is called

that help at all?


Thanks! but I got it working by creating a button for each page. Thanks for all you guys' help
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: