Anyone done script system that is written to a file?

Discuss any general programming issues here
Bugala
Posts: 824
Joined: Sun Feb 14, 2010 7:11 pm

Anyone done script system that is written to a file?

Post by Bugala » Mon Sep 23, 2019 11:22 am

So far I have done own script systems with Hollywood in way of:

Script {
[1] = {command = "display picture", filename = "filename.png", x=300, y=300},
[2] = {command = "displaytext", text = "just a test", x=100, y=100},
[3] = {command = "waitfor", forwhat="left mouse"},
[4] = {command = "clearall"}
...
}

And then this way executed line by line or something.

However, this is quite much work when I want to change some line in middle or something as example, and it isnt ideal otherwise either.

Therefore I am now going to give a try first time to do it in way that I have the script in a file.

as example: script.txt
Display Picture(300, 300, "filename.png")
displaytext(100, 100, "just a test")
Waitfor("leftmouse")
ClearAll
...

Have anyone done this way before and would be willing to share hes code?

I am mainly interested in how do you read the commands and variables from it.

My current idea is to simply read the file line by line and then use SplitStr command to separate commands and values.

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: Anyone done script system that is written to a file?

Post by Clyde » Mon Sep 23, 2019 3:38 pm

Not sure whether I get your point correctly, but if you do the second way, couldn't you just @INCLUDE this file as a hws file and you write these wrapper functions like "DisplayPicture()", "Waitfor()" etc. which are then just executed?
Currently using: Hollywood 8 with Windows IDE

p-OS
Posts: 112
Joined: Mon Nov 01, 2010 11:56 pm

Re: Anyone done script system that is written to a file?

Post by p-OS » Mon Sep 23, 2019 4:27 pm

Include is evaluated at compile time, he wants the commands to be executed at runtime.

Well, at least on amiganoid systems I wpuld simply set up an ARexx Port in my Hollwood App and define commands for it.

Bugala
Posts: 824
Joined: Sun Feb 14, 2010 7:11 pm

Re: Anyone done script system that is written to a file?

Post by Bugala » Mon Sep 23, 2019 5:32 pm

Should have explained bit better, but point is that this is own made script, not hollywood script. So #INCLUDE wont work because they are not Hollywood commands. My examples were bad as they were too Hollywood like commands.

But lets have another script:
Turn Left, 90
Move Forward, 10
Move Forward, 10
Turn Left, 90

so script could be something like this, where there would be command, and then one or more variables after it, like in this case turn left 90 degrees, and move forward 10 steps as example.

In practice my current script language I am about to make will remind Hollywood a lot, since it is meant for Game intros and similar stuff, idea being to simplify things a bit like I want to be able to click left mouse to move to next text or alternatively you wait specified time and it goes to next text automatically.

So by putting:
Displaytext(100, 100, "text1")
wait(5000)
Displaytext(100, 100, "text2")

I would now have text 1 displayed first and then either by waiting 5000 milliseconds, or by clicking left mouse button, it will move to displaying "text2". Or I could even click right mouse button for it to skip the whole thing altogether.

So that scripting languages purpose is to make things easy for me, that I dont need to start making those left/right mousebutton and waiting things separately, but I can just do with for example these three lines to achieve all that.

Edit:
And this also opens up a possibility for someone else to make scripts for my game when I have some system like this, without that someone needing to know how Hollywood works.

SamuraiCrow
Posts: 241
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Anyone done script system that is written to a file?

Post by SamuraiCrow » Tue Sep 24, 2019 2:40 pm

ReadTable and WriteTable will help with serialization of your language assuming it's formatted as a dynamic Array.

SamuraiCrow
Posts: 241
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Anyone done script system that is written to a file?

Post by SamuraiCrow » Tue Sep 24, 2019 3:12 pm

Running the script will require the array to be formatted as line number as a key and command table as a value. The command table will have the keyword string as the key and the parameter array as a value. The parameter array can contain expressions or terminal values but still use the parameter order as the keys. Expressions can contain functions calls, variables or constants. Functions are just commands with a return value but they still need definition before use. Variables and constants also need defining in the identifier block. The global identifier block is given a nonnumeric keyword string as key in the outer array/table as does all the global metadata.

As far as parsing to the bytecode table format, a JSON parser will do the job.

Bugala
Posts: 824
Joined: Sun Feb 14, 2010 7:11 pm

Re: Anyone done script system that is written to a file?

Post by Bugala » Wed Sep 25, 2019 10:40 am

@SamuraiCrow thanks for answering. Didnt realise there could be something like json parser already existing to do the job for me, although I am not sure JSON will work for me in the end, but at least it is a start and maybe I can at least use parts of it, I am actually asking about it a bit in here:
viewtopic.php?f=7&t=2240

Yesterday before seeing this answer I started working a bit on that, I guess it is called Parser then, and showing what I got might clarify a bit what I am after.

Test Script File:

Code: Select all

DisplayPicture(1, 2, "yep.png")
ClearAll()
EnableSkip, LeftMouse, next step
EnableSkip, RightMouse, End
Display Picture, 300, 300, "intropic1"
Display Text, 100, 100, "Intro Text 1"
Wait, 5000
ClearAll
Display Picture, 300, 300, "intropic2"
Display text, 100, 100, "Intro Text 2"
Wait, left mouse
End
The Parser I started working on looks like this:

Code: Select all

Function s_Intro:ReadScript(scriptfile)
	
	Local scriptfiledata={}
	
	OpenFile(1, scriptfile)
	While Not Eof(1) Do InsertItem(scriptfiledata, ReadLine(1))
	CloseFile(1)

	For Local n = 0 To TableItems(scriptfiledata)-1
		/*csvdata[n] = ReplaceStr(scriptfiledata[n], " ", " ")*/
		array, c = SplitStr(scriptfiledata[n], ",")
		
		self.script[n].command = LowerStr(array[0])
		
		Switch LowerStr(array[0])
		Case "display picture"
			self.script[n].x = array[1]
			self.script[n].y = array[2]
			self.script[n].file = array[3]
		Case "display text"
			self.script[n].x = array[1]
			self.script[n].y = array[2]
			self.script[n].text = array[3]
		Case "enableskip"
			self.script[n].with = LowerStr(array[1])
			self.script[n].towhere = LowerStr(array[2])
		Case "playmusic"
			self.script[n].file = array[1]
		Case "playsound"
			self.script[n].file = array[1]
		Case "wait"
			If GetType(array[1]) = #NUMBER
				self.script[n].type = "number"
				self.script[n].waitfor = ToNumber(array[1])
			Else
				self.script[n].type = "something"
				self.script[n].waitfor = LowerStr(array[1])
			EndIf
		EndSwitch
	Next

EndFunction
However, this is far from perfect. First of all, this doesnt give me freedom to write what ever way i write, for example, if i do nested writing:

Code: Select all

DisplayPicture(1, 2, "yep.png")
   ClearAll()
       EnableSkip, LeftMouse, next step
then I suppose it would already fail to recognise those commands.

Another problem is that what if the text itself has "," in it, it will then split the text.

Therefore if there would be something like JSON to handle taking all this info from a file, it would be very good, since then I dont need to figure out these problems myself, as I have no interest in figuring them out anyway, I just want to get my text file into a table in such way I can use them.

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Anyone done script system that is written to a file?

Post by jalih » Wed Sep 25, 2019 2:25 pm

Bugala wrote:
Wed Sep 25, 2019 10:40 am
I dont need to figure out these problems myself, as I have no interest in figuring them out anyway, I just want to get my text file into a table in such way I can use them.
Trust me, that stuff is worth figuring out! Very rewarding and fun stuff!

I have ported a simple toy interpreter by Ed Davis to Hollywood a few years ago. There are working scanner and parser modules and example on how to parse a Pascal like programming language.

I can send you the scanner and parser support files along with a small example on how to parse some of your script syntax to get you started, if needed?

SamuraiCrow
Posts: 241
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Anyone done script system that is written to a file?

Post by SamuraiCrow » Wed Sep 25, 2019 7:07 pm

My suggestion to you is this: write the interpreter using nested tables first. You can export your table format to a JSON file later to see what it looks like and use the JSON importer to read your changes back in.

The only problem I can see with using JSON is that Hollywood uses tables to represent arrays while JSON doesn't. JSON is a JavaScript derivative. Importing from JSON to Hollywood tables is no problem but exporting back to JSON might export an ordered array as a table with numeric keys.

As for command structure, the entire command structure of MongoDB is in JSON format as is its query construct and return format. It's useful to know.

I guess the only thing is: how pretty do you want your language to be in text?

Bugala
Posts: 824
Joined: Sun Feb 14, 2010 7:11 pm

Re: Anyone done script system that is written to a file?

Post by Bugala » Thu Sep 26, 2019 9:40 am

@jalih, yes I would like to take a look at that. Maybe I can use parts of it for my own parser.

@Samuraicrow
Well, Idea is to make things easier for me as well as readable, and possibly even usable by other people, so tables are not the way to go. I have done before with Tables and it is quite much pain to write and alter the code, especially if I decide I want something in middle, and it is also quite ineffective when finding out what is happening. That is why I would like to use something similar to Hollywoods way, where there is:
Command(values, as, needed)

Having given this some thought last night, I think I have mostly figured out how to make that parser. I am going to take a look on that Jalihs code still to see how he did it, and then start making the parser.

Post Reply