Multiline code?

Monkey Forums/Monkey Programming/Multiline code?

ondesic(Posted 2012) [#1]
Can you do something like:

ar:String = "This is a line of
code I want to break into mutiple
lines"

is there a way to do this, like with an underscore?


therevills(Posted 2012) [#2]
This works, but you will get a carriage return:

[monkeycode]Function Main()
Local str:String = "This is a
long String"

Print str
End[/monkeycode]

Or you can do this:
[monkeycode]Function Main()
Local str:String = "This is a " +
"long String"

Print str
End[/monkeycode]


NoOdle(Posted 2012) [#3]
therevills version doesn't work with DrawText, the snippet below will. It would be better to create a class to handle multiline strings, you could then store the split string array and not have to do this every loop, only when the string changes.

[monkeycode]
Local str : String = "This is a line of@code I want To break into mutiple@lines"

Local strX : Int = 100
Local strY : Int = 100

For Local this : String = Eachin str.Split( "@" )
DrawText this, strX, strY
strY = strY + 25
Next
[/monkeycode]


ondesic(Posted 2012) [#4]
Though I used an example of a string, the main thing I want to use this for is code, like:

DrawText (this,
strX,
strY)

This is a simple example, but you get the idea. This ability would be very helpful for long-lined code.


NoOdle(Posted 2012) [#5]
have you tried the code I posted in an App? It should do what you want.


therevills(Posted 2012) [#6]
Noodle I think you are misunderstanding what ondesic is wanting to do. He wants to code on multiple lines, not display the text in game on multiple lines.

@ondesic, I just tried this and it works fine:
[monkeycode]Strict

Import mojo

Function Main:Int()
New Game()
Return 0
End

Class Game Extends App
Field str:String
Field x:Int = 2
Field y:Int = 4
Field z:Int = 10

Method OnCreate:Int()
SetUpdateRate(60)
' multi line string
str = "Hello " +
"this is on a new line " +
"and we can keep doing this"
Return 0
End

Method OnUpdate:Int()
' multi line if statement
If x = 2 And
y = 4 And
z = 10 Then
Print "HELLO"
End
Return 0
End

Method OnRender:Int()
Cls
DrawText str, 10, 10

' multi line drawtext
DrawText "Hello",
10,
50

Return 0
End
End[/monkeycode]


Samah(Posted 2012) [#7]
This is some of the code from my MonkeyTouch entry:

[monkeycode]enemyBullets.FireBulletSpray(
firstBullet, Null,
srcX, srcY, 0,
firstAngle, firstAngle+intervalAngle*(i-1),
1000+delay*(i-3), 0,
speed-(i-3)*intervalSpeed, speed-(i-3)*intervalSpeed,
i)[/monkeycode]


NoOdle(Posted 2012) [#8]
Oh yes, sorry I was completely missing the point! It's been a long day :P


ziggy(Posted 2012) [#9]
As long as the last char of a source code line is an operator, a coma, an open paranthesi, the compiler will handle it properly (and also Jungle Ide by the way).
Example of wrong code:
Function Main()
	Local a:Int = 34
	-5
	Print a
End

Working code:
Function Main()
	Local a:Int = 34 - 
	5
	Print a
End

That's the "rule" that sohuld be applied according to Trans design if I'm not wrong. Sometimes it happens to work as the internal Trans tokenizer ignores carrier returns, but AFAIK this source-code line splitting is only granted to work between operators and continuation symbols such as a coma or an open parenthesis.


Sensei(Posted 2014) [#10]
Just found this as I'm having a problem where I want to create a dynamic string array on multiple lines and I get the error: "Error : Syntax error - expecting ']'."

Local textArray:String["one", "two", "three",
  ""four", "five", "six", "seven"]


This is just an example, not the actual string content. I can change the text array into a single string and perhaps use a pipe delimiter to split the bits into a new array, but I'd still like to know how to go about doing a string or data array as the example above on multiple lines.

Thanks!


muddy_shoes(Posted 2014) [#11]
Local textArray:String[] = ["one", "two", "three",
  "four", "five", "six", "seven"]



Sensei(Posted 2014) [#12]
Doh thanks. Long day...