Jump to content


Photo
- - - - -

How to make Server Sided Monsters( Tutorial )


  • Please log in to reply
10 replies to this topic

#1 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 07 August 2012 - 10:26 AM

  • Title: Server sided Monsters
  • Description: How to make server sided monsters
  • GM Version: :GM81: :GM8: :GM7:
  • 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,
Summary
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.)

Posted Image




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

Posted Image

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:

Posted Image

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):

Posted Image


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:

Posted Image

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.

  • 5

#2 Orbitguy

Orbitguy

    UNS Lead Guy

  • GMC Member
  • 314 posts
  • Version:GM:Studio

Posted 18 August 2012 - 07:10 PM

A very solid example on how to do this. Lovin' the pictures.
  • 0

#3 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 20 August 2012 - 02:07 PM

A very solid example on how to do this. Lovin' the pictures.


Thx for positive feedback I really appreciate it and I wish you good luck on SLO, It looks very promising to me.
  • 1

#4 AlexTM

AlexTM

    GMC Member?

  • GMC Member
  • 1422 posts
  • Version:GM8

Posted 27 August 2012 - 04:46 PM

"Unexpected error occured" When trying to start the server. *sigh* :sleep:
  • 0

#5 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 27 August 2012 - 05:03 PM

I pm you so we can talk about it
btw, It's how it is done, I always make like this to mine new online projects and it works perfectly ^^ You just need to have the skills of customize it to fit in your code
  • 0

#6 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 27 August 2012 - 06:40 PM

Uploaded a example. (Did it really fast but it should work)
  • 0

#7 Rassym

Rassym

    GMC Member

  • New Member
  • 286 posts
  • Version:Unknown

Posted 12 September 2012 - 11:11 AM

Looks great, I love the way you explained this ;)
Looking forward to adding this in my platform game!

Just one question, won't this be very hard to add in a online platformer?
  • 0

#8 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 12 September 2012 - 11:16 AM

Looks great, I love the way you explained this ;)
Looking forward to adding this in my platform game!

Just one question, won't this be very hard to add in a online platformer?


not at all ;) i've made a online platformer with this :)
If you want to get some more help then ask me.
  • 0

#9 Rassym

Rassym

    GMC Member

  • New Member
  • 286 posts
  • Version:Unknown

Posted 12 September 2012 - 11:30 AM


Looks great, I love the way you explained this ;)
Looking forward to adding this in my platform game!

Just one question, won't this be very hard to add in a online platformer?


not at all ;) i've made a online platformer with this :)
If you want to get some more help then ask me.


Sure ;)
Haven't got far enough to add the monsters yet tought : D
  • 0

#10 Tekniko

Tekniko

    GMC Member

  • GMC Member
  • 68 posts
  • Version:GM:Studio

Posted 15 September 2012 - 12:42 AM

Thanks for sharing this. Very easy to understand. I will try this out for fun tonight.
  • 0

#11 tntboss95

tntboss95

    GMC Member

  • GMC Member
  • 90 posts
  • Version:GM8

Posted 15 September 2012 - 09:09 AM

Thanks for sharing this. Very easy to understand. I will try this out for fun tonight.

no problem, i wish u good luck
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users