Gw Temp

Menu

Tutorial - 'File/String Spliting' by Guest

An item about Gamemaker posted on

Blurb

A tutorial on splitting files in GM

Body

The following scripts can be used to split files id GameMaker
For example you might want to send multiple peices of data to a computer.
Like sending character info to your opponent over the net.
To do this you'll combine the info together.

charactername = "Bob";
level = "7";
data = charactername+"-"+level;

so u end up with a string that has both peices of info combined into it.
so the variable data now stands for "Bob-7";
To extract it you use the following script i came up with.
I call it script_decipher.

//argument0 = which half to return (1=1st Half) (2=2nd Half)
//argument1 = specific character to split string (i.e. '+' or '-')
//argument2 = string to split
{
pos = string_pos(argument1,argument2);
prt1= string_copy(argument2,0,pos-1);
prt2= string_copy(argument2,pos+1,string_length(argument2));
if(argument0==1) return prt1;
if(argument0==2) return prt2;
}

so u can easily extract the info by typing:

charactername = script_decipher(1,"-",data);
level = script_decipher(2,"-",data);

This script can come in handy when your tryin to minimise message sending over a networked game.
But the downfall of this script is that it can basically only combine 2 peices of info together.
To overcome this ive developed the following script (called script_decipher_ext):

//argument0 = part to return
//argument1 = character to use to separate string
//argument2 = string to separate
{
prt = argument0;
cde = argument1;
str = argument2;
lft = str;

for(i=1; string_pos(cde,lft)!=0; i+=1)
{
part = script_decipher(1,cde,lft);
lft = script_decipher(2,cde,lft);
maxn = i;
}

part[maxn+1] = lft;

return part[prt]
}

This script basically can extract a string that has many peices of info combined into it.
For example:

name = "Willy";
age = "15";
email = "willy@willyshouse.com";

data = name + "-" + age + "-" + email;

So now the variable data stands for "Willy-15-willy@willyshouse.com".
To extract this info we use the following:

name = script_decipher_ext(1,"-",data);
age = script_decipher_ext(2,"-",data);
email = script_decipher_ext(3,"-",data);

And that concludes this tutorial. If you do happen to use this script a mention of me in the credits would be appreciated.

This tutorial brought to you by James Moore from Auckland New Zealand.