Jump to content

Importing user made script


Cole Chapman
 Share

Recommended Posts

I am trying to figure out what the best way is to import a user made script into a blitzmax application. I want the application to be able to work with new scripts even after a build so I was thinking of something like

 

(Main.bmx) - pseudocode

Readfile "Scriptfile.scr"

If Readfile contains "Start" Then
     Start()
Endif

Function Start()
     Local I:int = 800
     Local y:int = 600
     'Ect'
EndFunction

 

Reading the file and seeing if it contains keywords for functions and then executing functions. If anyone else has any better ideas though, feel free to help me out :)

Link to comment
Share on other sites

Yeah, that will work, and it's faster than using some bloated Lua or Python parser, because they contain 90% features you don't need for your game.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

An interesting concept, Cole. I've thought about something similar but every time I do I end up trying to create a new language, which gets complex, and never finished, lol. My only input is to keep it simple and that you should do a better search than just "Start," maybe "Start()." There is also a module (Bah.Regex) you can use for regular expressions.

Link to comment
Share on other sites

Definately, but with things requiring some variables I may keep the option open for not using parentheses.

 

For example:

MovePlayerToPlayer(Player1, Player2)

or

MovePlayerToPlayer Player1, Player2

 

I could then splice the string to get things before and after the comma. Ill be sure to check out the Regex module

Link to comment
Share on other sites

I've attached the Regex module, just in case you want to try it out. I also played a bit with your idea:

 

myscript.scr

StartAI()
MovePlayerToPlayer()
MovePlayerToPlayer(Player1, Player2)

Start()
End()
Terminate(1)

 

script.bmx

SuperStrict

Import bah.regex
Import brl.standardio

Local file:TStream = ReadFile(AppDir+"/myscript.scr")
Local regex:tregex = tregex.Create("(.*)\((.*)\)")


While Not Eof(file)
Local match:tregexmatch = regex.find(Trim(ReadLine(file)))
	If match
		Select Lower(match.subexp(1))
			Case "startai"
				StartAI()
			Case "moveplayertoplayer"
				Local vars:String[]
					If match.subexp(2) <> ""
						vars = match.subexp(2).split(", ")
						MovePlayerToPlayer(vars[0], vars[1])
					Else
						MovePlayerToPlayer()
					EndIf
			Default
					If match.subexp(2) = ""
						Print "No Match: "+match.subexp(1)
					Else
						Print "No Match: "+match.subexp(1)+"("+match.subexp(2)+")"
					EndIf
		EndSelect
	EndIf
Wend

CloseStream(file)

Function StartAI()
Print "Do something with AI"
EndFunction

Function MovePlayerToPlayer(p1:String = "", p2:String = "")
	If p1 <> "" Or p2 <> ""
		Print "move "+p1+" to "+p2
	Else
		Print "Do something with player move with no arguments."
	EndIf
EndFunction

 

This will output:

Do something with AI

Do something with player move with no arguments.

move Player1 to Player2

No Match: Start

No Match: End

No Match: Terminate(1)

 

Actually having fun with this on this rainy day. Anyways, figured I'd just throw all of that out there.

bah.regex.rar

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...