Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Memblocks - What am I doing wrong?

Author
Message
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 9th Jan 2010 18:38 Edited at: 9th Jan 2010 23:31
Hi everyone!

This is some code from a game I am trying to make over the net. When I write the info to the memblock as a host and send it over the net, the joiners should receive it and retrieve the data. But for some reason I'm not aware of, the back letter of the host name keeps changing and the map name does weird things. Here I slapped it together so it instantly reads the info after writing, but it still does that weird thing...

code:



Thanks, TheComet


Make the paths of your enemies easier with WaypointPro!
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 10th Jan 2010 00:30
Hi All

This is my first ever post to the forum but your query happened to coincide with some stuff I'm playing around with.

Your code is doing two things:

The value for the last letter in the player name string is being over written by (the length of the level name string - 1), then being printed to screen as a character.

This is happening here:



All you've done is got the position in the mem block wrong. To fix the position you need to increment t by 2. However, this causes another problem which I couldn't quite figure out.

The second problem is that an extra "l" is being added to the end of the level name string in each loop but I couldn't find where the program was doing this.

Both these problems can be seen if you run this code.




The only thing I have done here is to add on two extra line that print the length of the level name string and the last character of the player name string that is being constantly over written.

I think part of the main problem is that the place in the memblock where you store the length of the level name moves depending on the length of the player name string, which in itself shouldn't be causing a problem but is a little confusing when trying to work out where all the data is in the memblock.

Taking a cue from how memblocks for meshes etc are created, I've restructured your memblock to include a header, mainly so that there is a value that shows where the position of the level name data is stored in the memblock, which makes it easier to locate. If lashed up some code to show how this works:




I hope this is of some help.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 10th Jan 2010 11:09
Quote: "This is my first ever post to the forum"

You joined in 2005 and just now posted?


"Any sufficiently advanced technology is indistinguishable from magic" ~ Arthur C. Clarke
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 10th Jan 2010 13:19
Quote: "You joined in 2005 and just now posted?"


It would seem so
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Jan 2010 14:21
If you guys want to get a little fancy, you can copy strings to and from string variables and memblocks using the win32 api. The following example set's up a string, copies it to a memblock, prints the results of both, copies part of the memblock string to another string, and then displays both of those.



The things to keep in mind if you decide to use this method:
1. Make sure the memblock is LARGER than the string data you want to copy to it. If you copy a longer string than the size of the memblock, you will be writing data to uncharted areas of memory which can cause quite a nasty crash.

2. Any strings written to a memblock should be terminated with a NULL character - a value of zero in this case. "my string" should be written to the memblock as

109 121 32 115 116 114 105 110 103 0

The null character is very important as it separates the boundaries between strings in the memblock for the winapi to be able to read.

3. Any string variable RECEIVING copied data from a memblock or another string should be at least as long as the data it is receiving +1 (up to 255 characters) . Since string variables are stored as memory pointers in DBC, it is possible to write into undefined memory areas if not using the built in string commands already in DBC. In the case of using the winapi, you are copying memory directly from one place to another using commands outside of DBC's command set - so DBC really can't check what's going on. So, a safe bet is to set the variable to a safe length before copying data directly to it. A simple way is to use

player$=space$(255)

That will create a variable named player$ that has 255 spaces in it so it's ready to receive data from a memblock.

Follow those guidelines and you can manipulate strings in memblocks fairly easily using the Win32 api. You can also copy between memblocks if you want.

There's another useful copy function that allows you to copy a specific number of bytes for copying only parts of a string:

call dll kernel32,"lstrcpynA",target,source,number of bytes

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 10th Jan 2010 21:43
Wow Latch... That IS fancy... Very clever! How do you know all of this stuff?

@29 games

Thanks a lot! I structured my memblocks with headers and data and it worked smoothly after that. Me + memblocks = @_@

TheComet


Make the paths of your enemies easier with WaypointPro!

Login to post a reply

Server time is: 2024-04-20 13:54:47
Your offset time is: 2024-04-20 13:54:47