Is it possible to call a procedure inside a procedure, and if so, how?
Author |
Message |
Tasty Pastry
|
Posted: Sun May 21, 2017 2:18 pm Post subject: Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
I've made a typewriter procedure for one of my programs, but I need it to be in a different font. I've tried Font.Draw, but since it's a procedure it wont let me use it inside the Typewriter Proc. Is there a way around this?
Turing: |
var chars : array char of boolean
var font : int
font := Font.New("Pokemon GB:18")
proc TypewriterPrint (text : string)
loop
for i : 1.. length(text )
Input.KeyDown (chars )
if chars (KEY_ENTER ) then
delay(50)
put text (i ) ..
else
delay(100)
put text (i ) ..
end if
end for
put ""
end loop
end TypewriterPrint
Font.Draw (TypewriterPrint ("This will be printed like a typewriter"), 150, 150, font, black)
|
Functions don't seem to be working either. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Insectoid

|
Posted: Sun May 21, 2017 3:16 pm Post subject: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
Font.Draw accepts a string as a parameter. The only way to pass a function as the parameter is if that function returns a string.
However, you can use procedures inside procedures that you wrote yourself. For example,
code: | proc writeHello
Font.Draw ("Hello", 10, 10, font, black)
end writeHello |
If you want to change the font drawn by your procedure, add it as a parameter:
code: | proc writeHello (var myFont : int)
Font.Draw ("Hello", 10, 10, myFont, black)
end writeHello
var font := Font.New ("some font")
var otherFont := Font.New ("some other font"
writeHello (font)
writeHello (otherFont)
|
This will write 'hello' twice, once in each font.
Bear in mind that Font.Draw requires coordinates to know where to draw, unlike 'put' which keeps track of where the pointer is. However, using some simple math and the convenient Font.Width() function, you can calculate where to place each individual letter. |
|
|
|
|
 |
Tasty Pastry
|
Posted: Sun May 21, 2017 3:44 pm Post subject: Re: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
Insectoid @ Sun May 21, 2017 3:16 pm wrote: Font.Draw accepts a string as a parameter. The only way to pass a function as the parameter is if that function returns a string.
However, you can use procedures inside procedures that you wrote yourself. For example,
code: | proc writeHello
Font.Draw ("Hello", 10, 10, font, black)
end writeHello |
If you want to change the font drawn by your procedure, add it as a parameter:
code: | proc writeHello (var myFont : int)
Font.Draw ("Hello", 10, 10, myFont, black)
end writeHello
var font := Font.New ("some font")
var otherFont := Font.New ("some other font"
writeHello (font)
writeHello (otherFont)
|
This will write 'hello' twice, once in each font.
Bear in mind that Font.Draw requires coordinates to know where to draw, unlike 'put' which keeps track of where the pointer is. However, using some simple math and the convenient Font.Width() function, you can calculate where to place each individual letter.
I don't seem to understand. I want to use the typewriter proc but in a different font, is that possible? |
|
|
|
|
 |
Insectoid

|
Posted: Sun May 21, 2017 3:47 pm Post subject: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
You cannot change the font of the 'put' command. You have to re-write your typewriter procedure to use Font.Draw instead. |
|
|
|
|
 |
Tasty Pastry
|
Posted: Sun May 21, 2017 4:03 pm Post subject: Re: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
Insectoid @ Sun May 21, 2017 3:47 pm wrote: You cannot change the font of the 'put' command. You have to re-write your typewriter procedure to use Font.Draw instead.
I did that, but now it's just drawing the font in the same place. Typing in '..' gives a syntax error.
Turing: |
var chars : array char of boolean
var font : int
font := Font.New("Pokemon GB:18")
proc TypewriterPrint (text : string)
loop
for i : 1.. length(text )
Input.KeyDown (chars )
if chars (KEY_ENTER ) then
delay(50)
Font.Draw (text (i ), 150, 150, font, black)
else
delay(100)
Font.Draw (text (i ), 150, 150, font, black)
end if
end for
put ""
end loop
end TypewriterPrint
TypewriterPrint ("Hello!")
|
|
|
|
|
|
 |
Insectoid

|
Posted: Sun May 21, 2017 5:24 pm Post subject: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
What does the 150, 150 mean in your Font.Draw line? |
|
|
|
|
 |
Tasty Pastry
|
Posted: Mon May 22, 2017 11:15 am Post subject: Re: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
Insectoid @ Sun May 21, 2017 5:24 pm wrote: What does the 150, 150 mean in your Font.Draw line?
OH I get it now!
Turing: |
var chars : array char of boolean
var font : int
var x : int
x := 0
font := Font.New("Pokemon GB:18")
proc TypewriterPrint (text : string)
loop
for i : 1.. length(text )
Input.KeyDown (chars )
if chars (KEY_ENTER ) then
delay(50)
Font.Draw (text (i ), 150 + x, 150, font, black)
x := x + 20
else
delay(100)
Font.Draw (text (i ), 150 + x, 150, font, black)
x := x + 20
end if
end for
put ""
end loop
end TypewriterPrint
TypewriterPrint ("Hello!")
|
Now it just loops over and over again? |
|
|
|
|
 |
Insectoid

|
Posted: Mon May 22, 2017 1:11 pm Post subject: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
That could be due to the loop inside your function. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tasty Pastry
|
Posted: Mon May 22, 2017 2:41 pm Post subject: Re: RE:Is it possible to call a procedure inside a procedure, and if so, how? |
|
|
Insectoid @ Mon May 22, 2017 1:11 pm wrote: That could be due to the loop inside your function.
How did I not notice that??!? I'm sorry, I've been working on this program for a while now and its like my brain has turned off. Thanks so much! |
|
|
|
|
 |
|
|