View Full Version : getting setDataPath to work?
whisperstorm
10-21-2003, 09:10 AM
I'm working on a simple game, which is posted in the mylaszlo.com site. I want to allow folks to load new "levels" via the internet, etc. When I change my code such that the data is external all is well. Things load ok.
I have a dataset like this:
<dataset name="positionInfo" src="world001.xml"></dataset>
which is referenced in the code like this:
<view id="map" name="map" datapath="positionInfo:/items">
<block datapath="item" />
</view>
But when I try to call:
map.setDataPath("world002.xml");
Nothing happens. Though strangely enough when I call it a 2nd time - all the tiles disappear...
I know about the absolute paths, which I tried with
http://localhost:8080/lps-1.0.1/my-apps/tinyworld/world002.xml
but still nothing.
Any idea what I'm doing wrong? Do you call setDataPath on the view, or the dataset?
Also this "block" is a special class that constructs things via the "ondata" event. That event is not getting fired as well - would I need to manually call that method?
I'm including a version of the tinyworld code that has what I am talking about. Move onto the brown square to try to load the other data file...
antun
10-21-2003, 09:40 AM
You don't call setDatapath() to change the file you're referencing. Each file is going to map to a dataset, so you could declare both each dataset at the top of your file:
<dataset name="dsOne" src="world001.xml" />
<dataset name="dsTwo" src="world002.xml" />
... and then change the datapath in the view from one to the other:
foo.setDatapath( 'dsTwo:/myrootnode/mychildnode' );
That's probably not what you're looking for - the impression I got was that you want to be able to dynamically set the filename.
To achieve this, use a single dataset (make sure it's getting it's data over HTTP), and change the dataset's src attribute at runtime. You'll have to call doRequest() to re-request the data. The ondata event should then fire:
<canvas debug="true">
<dataset name="foo" src="data_1.xml" type="http" autorequest="true" />
<simplelayout axis="y" spacing="5" />
<button>Serialize
<method event="onclick">
debug.write( foo.getPointer().serialize() );
</method>
</button>
<button>Switch to data_2.xml
<method event="onclick">
foo.setAttribute( 'src', 'data_2.xml' );
foo.doRequest();
</method>
</button>
</canvas>
-Antun
Originally posted by whisperstorm
But when I try to call:
map.setDataPath("world002.xml");
Nothing happens.
whisperstorm
10-22-2003, 11:00 AM
Ok I got this to work - at least for the tiles.. I added a data element called "player" so that I can change the location of the player based on the map that is loaded, but I'm running into all kinds of problems. Only 1 out of 10 tries did it work the correct way (placing the character in the right location) and I'm going to go over my code first to see if there's something wierd about what I'm doing. For some reason after I load a 2nd dataset, my collision detection and player placement get's really out of wack - and after reloading the SWF a few times things really break down.
I'm not sure if I've run into some kind of bug, or just a flaw in my own code. I'll post what I have right now if you want to poke at it.
whisperstorm
10-22-2003, 11:03 AM
Here's what I have right now... It uses a 2nd class to define the player character, and loads in row and col information to try to place the character.. I think I'm hitting some kind of race condition on this?
I dont have time at the moment to try to tease out exactly what is going on, but if you are bored, feel free to look at it... I'll try to find out specifically what's going wrong in a few days.. RL is taking away my time.
antun
10-22-2003, 12:31 PM
I wasn't sure what to do with this - would you like me to replace the existing one or not - you indicated that there were a couple of problems with it.
-Antun
whisperstorm
10-22-2003, 04:08 PM
No, dont use this code - its still broken. I'll work to get you a better test case of what I'm seeing. I only posted this to let you see the strangeness.
antun
10-22-2003, 04:09 PM
Sure thing. I'll try to set aside some time to look over it tomorrow to figure out what's happening there.
-Antun
whisperstorm
10-23-2003, 11:20 AM
Thanks! The main thing that is strange is that the player gets placed in the wrong location consistently. I'd say only 1 out of 8 times did the 2nd level load properly and place the character view in the right location. I'm guessing that there's something about the asynchronous loading that is confusing the app.
Perhaps we can talk today at the Laszlo meeting. I'm planning on being there.
antun
10-23-2003, 04:24 PM
I haven't yet found out what's causing it, but what I did notice is that the debugger throws an error every now and again. It doesn't seem to be consistent.
Here are a couple of points:
You have some code in the <script> tag at the top fo the page - stuff there gets executed when the page loads. I wouldn't recommend putting code in the root of it (you have a for loop that creates a global multidimensional array), as it might potentially not be there when needed (for example with regards to data requests). It's OK to put functions in there though.
Also, you should always localize counter variables in loops in LZX:
for (var i = 0; i < myMap.length;i++){
instead of:
for (i = 0; i < myMap.length;i++){
I'll keep looking at this...
-Antun
whisperstorm
10-24-2003, 10:21 AM
Thanks! I did notice that intermittant error - strangely enough if you move away and come back it's "gone" I have a feeling multiple things are happening. I'll keep at it and post what I find as well thanks for persisting. Sorry I missed you at the Laszlo meeting last night.
antun
10-24-2003, 11:00 AM
I've been tinkering with the app for a while, and have it working pretty smoothly. If you look at the code, I've put some comments in there prefixed by "AK:", but in general:
1) I moved all the data into a single XML file, with two <area> nodes. It just seemed to make more sense to load it all up front so that there wasn't a delay.
2) I turned on pooling for the data, which speeds up the transition.
3) There was a bug in the XML you were using (maybe I wrote it in there by accident when I suggested the XML structure :(. There were two row 2 col 2 nodes. Deleted one.
4) I cleaned up the collision detection and moved it into a separate method, just for the purpose of being able to debug it a bit more cleanly. I reordered the guts of that method so that the first thing it checked for was whether the cell you were trying to move to was in bounds or not. The reason for that intermittent error in the debugger was that you'd move to an out-of-bounds square which was an undefined object in the myMap array. You were doing something like (simplified code for clarity):
if ( !myMap[x][y].collision && (playerRow > 1) )
Although this code will work perfectly well (the "(playerRow > 1)" would catch OOB squares), it still threw the error. The reason the error appeared intermittent was that the debugger only spits out the first of each kind of error it encounters.
The next thing you should think about doing is speeding up the loading. Calling setSource() isn't ideal in this situation since even though the graphics get cached, the app still needs to wait for a response from the server before drawing them (hence the delay). I would recommend creating a multi-frame resource and instead of calling setSource(), setting (or better, constraining) the frame number to the appropriate one. This does mean that you won't be able to have custom graphics that live on remote servers and are specified in the XML file, so it might not be the most practical thing for you.
Hope this helps!
-Antun
whisperstorm
10-24-2003, 11:19 AM
Wow! It seems much more responsive. You rock! I need to keep the separate files as a central function of the app is to allow anyone to create a world that this widget can load up and navigate around in.
I was really frustrated with this yesterday, you've helped me tremendously. I'll post a new version with your changes and a few of my own to mylaszlo later today.
Thanks!!
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.