Loading Files at Runtime
Monkey Forums/Monkey Programming/Loading Files at Runtime
| ||
| Is there a way to load files during run-time? I just want to have the ability to do so when designing levels/UI so I can see the changes without having to recompile. |
| ||
| Take a look at DataBuffer.Load. Depending on the target, you can also use FileStream. The APIs section of the docs is a pretty good quick reference for finding 'how do I?' stuff like this. |
| ||
I was playing with this but couldn't seem to get it to load properly. Does it only accept .bin files? Here's what I was doing:
// I'm passing "monkey://data/chunkDB.xml" as the path parameter.
Method New (path:String)
Super.New()
_instances = New StringMap<Graphic>()
Local e:XMLError = New XMLError()
#if CONFIG="debug"
Local stream:DataStream = New DataStream(DataBuffer.Load(path)) // null error here
Print("LEN: " + stream.Length)
Print("DATA: " + stream.ReadString(stream.Length))
'_xmlData = ParseXML(_dataBuffer.Load(0), e)
#else
_xmlData = ParseXML(LoadString(path), e)
#end
If (e.error) Error(e.ToString())
End
As I commented in the code, I'm getting a crash because of a null error. |
| ||
What target are you using? This works here on glfw/html5:Import brl.datastream Function Main() Local stream:=New DataStream( DataBuffer.Load( "monkey://data/test.xml" ) ) Print stream.ReadByte() End Strictly speaking, binary files should have a .bin or .dat extension (or you can use #DATA_FILES+="*.blah"), but since .xml is also a text file type, the file should be recognized anyway. |
| ||
| I'm using Flash - I see on the docs that availability for both DataStream and DataBuffer are "All" so I assume it should work for Flash also? |
| ||
| Have you tried my code above? It works here with flash, so I can only assume there's something else wrong with your code. It'd help if you could post a runnable sample. |
| ||
| Whoops - operator error. I was changing the wrong path var, heh. Thanks mate! |
| ||
Hrm while it is running now, I can't seem to get it to reload the new XML file after I've made changes. The class I have this in is huge and contains a lot of irrelevant code, which is why I didn't post it before. But here's what I'm doing now:
Method Added:Void ()
Super.Added()
Local e:XMLError = New XMLError()
#if CONFIG="debug"
Local stream:DataStream = New DataStream(DataBuffer.Load("monkey://data/" + _path))
Local raw:String = stream.ReadString(stream.Length)
_xmlData = ParseXML(raw, e)
Print(raw)
#else
'_xmlData = ParseXML(LoadString(_path), e)
#end
If (e.error) Error(e.ToString())
' SNIP load stuff with xml ...
I've simply setup a hotkey to re-add my UI object, which calls the "Added()" function above. After I've made some changes to the XML and reloaded, I can see that the result of Print(raw) does not reflect the changes I made. |
| ||
| Oops, should have mentioned - stuff in data/ is 'baked into' the app. With Flash, you don't have as many options as other targets - have you tried HttpRequest? Or perhaps you could develop on glfw target, which gives you full filesystem access etc. |
| ||
| Ah, that would make sense. I haven't tried HttpRequest yet as the docs say its not supported on Flash. |
| ||
| LoadString() works on all targets. |