- Title: Server sided Monsters
- Description: How to make server sided monsters
- GM Version: :GM81:

- Registered: yes
- File Type:
- File Size:
- File Link: Server : Download server Client : Download Client Version Compatible 39dll: Download 39dll
- Required Extensions: none
- Required DLLs: 39dll
- Tags: Server,sided,monsters,objects,39dll,
Shows how to make server sided enemies/monsters
Hi, GMC community! I have made a tutorial how to make server sided monsters, but can also be used to create server sided items/objects(This tutorial shows just how it's works so you will need to customize this tutorial for your own needs),
Remember this tutorial works only when you have an exact copy of the room both in server and client.
Step 1: (In client) Go to RESCOURCES>DEFINE CONSTANTS, and create a new variable with the name MID_MONSTER_CREATE and MID_MONSTER_MOVE(Choose also a value fitting your own needs.)

Step 2: (In Server) In the object that is controlling the server values we put this in the create event:

for(i=0; i < 200; i+=1) // As u know the ”200” means how many monsters u can have in the server
{
global.monsters[i] = -1;
} //Step 3:(In server) To create monsters in the server we need spawns right? Create a obj_spawner.
This we add in alarm 0 event in obj_spawner:::
if spawn<maxspawn //controlling how many spawned monsters there are if spawn value is below maxspawn, then create a new monster
{
monsterAI=instance_create(x+random(xxx),y+random(yyy),obj_monster) // creates a monster at a random position near the spawn.
monsterAI.spawn=id //Spawner ID connected to monster
monsterAI.kind=kind // What kind of monster it is.
with monsterAI{
scr_monster_init(kind) //executes the script to search for what kind of monster it is.(more of this on step 4)
clearbuffer();
writebyte(MID_MONSTER_CREATE) //send message monster create
writefloat(x)
writefloat(y)
writebyte(myid)
writeshort(kind)
with obj_player
{
send_client("all") //send to all clients that are ingame that monster has been created
}}
spawn+=1 // adds a spawn variable(if the spawn=maxspawn then don’t spawn more obj_monster)
}
alarm[0]=mint+random(maxt) //restart the alarm 0 event with a random time
And in the server room in obj_spawner creation code:

We put this creation code:
kind=0 //frog spawnsort="Frog spawner" //spawner draw name (dont needed) spawn=0 maxspawn=1 maxt=200 // mint=150 // xxx=10 // yyy=20 // alarm[0]=mint+random(maxt)
Step 4:(In Server and Client) Now we need to do a script what kind of monsters there are, name it scr_monster_init(This is just mine code that you need customize for your needs):

Step 5:(In server) In obj_monster step event code you can have like this(this is just an example how my AI is working):
If started=false
scr_monster_init(kind) //execute script to determinate which kind of monster it is.
if round(random(25))=1 //every time the monster change direction
dir=choose(1,2,3,4,5) //choose a direction between the 5
{
if dir=1{
y+=2 //down
}
if dir=2{
y-=2 //up
}
if dir=3{
x+=2 //right
}
if dir=4{
x-=2 //left
}
if dir=5{
x=0 and y=0 //stand
}
if changedir!=dir //if the direction changes then send to client new direction
{
clearbuffer();
writebyte(MID_MONSTER_MOVE);
writebyte(myid);
writefloat(dir);
writefloat(x)
writefloat(y)
with obj_player
{
send_message("all")
}
changedir=dir
}
}
Make sure u have the following variables in the obj_monster(( create event )):
dir=0
changedir=0
started=false
for (i=0; i<200; i+=1)
{
if global.monsters[i]=-1
{
global.monsters[i]=id
myid=i
exit;
}
}Step 6:(In client) obj_client who controlling the values in client need to have this code in ((create event)):
for(i=0; i < 200; i+=1)
{
monsters[i] = -1;
}Step 7: Here is the code in my obj_monster(feel free to use it):
Information about object: obj_monster
Sprite: <no sprite>
Solid: false
Visible: true
Depth: 200
Persistent: true
Parent: <no parent>
Mask: <same as sprite>
Create Event:
execute code:
image_speed=0
started=false
dir=0
prevspr=0
Step Event:
execute code:
if started=false
scr_monster_init(kind)
if dir=1
{
y+=2
image_speed=0.2
sprite_index= sdown
prevspr=1
}
if dir=2
{
y-=2
image_speed=0.2
sprite_index= sup
prevspr=2
}
if dir=3
{
x+=2
image_speed=0.2
sprite_index= sright
prevspr=3
}
if dir=4
{
x-=2
image_speed=0.2
sprite_index= sleft
prevspr=4
}
if dir=5
{
x=0 y=0
image_speed=0
if prevspr=1
sprite_index=sdown
if prevspr=2
sprite_index=sup
if prevspr=3
sprite_index=sright
if prevspr=4
sprite_index=sleft
}
execute code:
Collision Event with object obj_player:
execute code:
Collision Event with object obj_solid:
execute code:
image_speed=0
Collision Event with object spr_safe:
execute code:
image_speed=0
Draw Event:
execute code:
if sprite_exists(sprite_index)
{
draw_sprite_ext(sprite_index,image_index,x,y,1,1,0,c_white,image_alpha)
}Step 8:(In client) In client’s packet receiver we need add this code:

When a byte is received a script executes.
Now in client we need to make the 2 scripts scr_monster_create and scr_monster_move
In scr_monster_create we add:
monsterAI=instance_create(readfloat(),readfloat(),obj_monster) monsterAI.myid=readbyte() monsterAI.kind=readshort() monsters[monsterAI.myid]=monsterAI
And in scr_monster_move we add:
mid=readbyte();
monsterAI=monsters[mid]
monsterAI.dir=0
monsterAI.dir=readfloat()
if monsterAI.dir=1 or 2 or 3 or 4 or 5 //Updates the position whenever monster change the dir
{
monsterAI.x=readfloat()
monsterAI.y=readfloat()
}Last step: To prevent to not showing existing monsters when u join game. I recommend you making this script (And execute the script when player joins the game or login) in Server::
for(i=0; i < 200; i+=1)
{
if global.monsters[i]>-1
{
with (global.monsters[i])
{
clearbuffer();
if object_index=obj_monster
{
writebyte(MID_MONSTER_CREATE)
writefloat(x)
writefloat(y)
writebyte(myid)
writeshort(kind)
}
}
send_client("self")
}
}That’s all!!!
Remember have the same collision mask and the position for solids for the server and client or the movement will be buggy..
Feel free to ask me if u don’t understand and tell me if u get errors.(May have forgot something)
The monster x,y movement runs smoothly and does not lag.
Don’t forget to thank me if I helped(Credit would be nice too).
P.s maybe then I will release a tutorial for trading between players too.
Edited by tntboss95, 27 August 2012 - 06:57 PM.











