About Store Forum Documentation Contact



Post Reply 
Monster spawning
Author Message
Kiekos Offline
Member

Post: #1
Monster spawning
Hey there!

Got a little problem here... I've followed one of the forum user's tutorial (I'm sorry but I can't find the thread now). I modified and used this part of the code:

Code:
void MOB::spawn(Str obj, Vec pos)
{
    Game::ObjParams obje; obje.load(obj);
    super::create(obje);

    Flt y = Game::World.hmHeight(pos.xz(),true)+0.6f;
    T.pos(pos+Vec(0,y,0)); // add Y so that it doesn't spawn just under the map
    ctrl.actor.group(1);   // disable collision (set actor group 1)
}

What am I missing as nothing happens in the actual game?

Thanks,
Kiekos
(This post was last modified: 10-15-2013 08:08 PM by Kiekos.)
10-13-2013 07:26 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Monster spawning
Hi,

You need to use Game. World. objCreate which will then call the create function of the object
10-13-2013 10:39 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #3
RE: Monster spawning
Oh... I see, but it's still not clear enough for me...

EDIT: I'm confused. I've been messing around with this and can't get it working. I'd like to use the spawn function whose code is mentioned above. Do I still have to call the .objCreate function and will it then call the spawn function?
(This post was last modified: 10-16-2013 03:30 PM by Kiekos.)
10-14-2013 01:45 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #4
RE: Monster spawning
Alright, I'm out of ideas right now. I came up with something which should work, cause it works in the ProjectX by TBJokers and I can't figure out why it doesn't work for me...

This is defined in the monster structure:
Code:
void MOB::spawn(Str obj, Vec pos)
{
    Game::ObjParams obje; obje.load(obj);
    super::create(obje);

    Flt y = Game::World.hmHeight(pos.xz(),true);
    T.pos(pos+Vec(0,y,0));

    ctrl.actor.group(false);
}

Bool MOB::update()
{
    if(super::update())
    {
        return true;
    }

    return false;
}

and this is in the game file:
Code:
Memc<MOB> Monsters;

[i]/* the following code part is in the update loop */[/i]

if(Kb.bp(KB_SPACE)) Monsters.New().spawn("obj/chr/skeleton/0.obj", Vec(0,0,0));
FREPAO(Monsters) .update(); // update all mobs

I have no idea why the monster is not spawning in the actual game... Any ideas?

EDIT: Btw, I've also tried this method:
Code:
Game::ObjParamsPtr obj = Game::Objs.ptrRequire("obj/chr/skeleton/0.obj");
Game::World.objCreate(*obj, Matrix(obj->scale(), Vec(0,0,0)));
which is shown in Game Basics Tutorials #10 - Dynamically Created Objects and it doesn't work either.
(This post was last modified: 10-16-2013 03:44 PM by Kiekos.)
10-16-2013 03:39 PM
Find all posts by this user Quote this message in a reply
Pherael Offline
Member

Post: #5
RE: Monster spawning
There is tutorial Dynamicaly Creatting Object, where you should found your answers how use Game.World.objCreate. I recomended to do it this way.

But if you don't want use World Manager to handle your MOBS (sine you update them manuly), you have also to draw them manualy (if you don't do it already)
10-16-2013 03:55 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #6
RE: Monster spawning
But I wrote just a second ago that I followed the tutorial for Dynamically Created Objects and that method doesn't work either. I'm out of ideas on what the problem is...
10-16-2013 03:58 PM
Find all posts by this user Quote this message in a reply
Pherael Offline
Member

Post: #7
RE: Monster spawning
When you using Game.World.objCreate do you set Game.World.setObjType(Monters, OBJ_MONSTER) where Monters is ObjMemx<MOB> Monster and OBJ_MONSTER is your OBJ_TYPE enum created in Editor?
10-16-2013 04:23 PM
Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #8
RE: Monster spawning
You need to set the object type prior to passing the object to the objCreate function
10-16-2013 04:44 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #9
RE: Monster spawning
Yeah I have everything set. I don't know what was wrong but it works now. Like seriously I wrote the same thing again, ran it and it worked.

EDIT: @Pixel Perfect, what do you mean?
(This post was last modified: 10-16-2013 10:15 PM by Kiekos.)
10-16-2013 04:46 PM
Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #10
RE: Monster spawning
Cool, glad you got it working.

No, I was wondering if the ObjParams type was unset for some reason but it obviously is or it wouldn't be working.
10-16-2013 05:16 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #11
RE: Monster spawning
Yep, working fine. Just thinking now: object will be created if I call the Game::World.objCreate in the main update loop and set it to be called if I press some button.

But what if I want (for example) to spawn 20 at the very beginning, when launching the game and loading the map. Placing the function in bool init() doesn't work, npcs don't show up.
10-17-2013 09:04 PM
Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #12
RE: Monster spawning
The world needs to be loaded before you spawn them from code. If you want them to spawn at the start of the game when loading the map then the simplest way would be to have them as objects in your level. That is ... add them in the editor to your level and they will be loaded with the game!
10-17-2013 09:16 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #13
RE: Monster spawning
Quote:(...) the simplest way would be to have them as objects in your level. That is ... add them in the editor to your level and they will be loaded with the game!

I know that! But the thing is that I want to have them created with code as (later on) I'm going to load all the information about npcs (type, position, count etc) from a database.

I think I'm just gonna do it like this:
Code:
//init
bool spawn = true;

//update
if (spawn) {
   //creating and spawning mobs
   spawn = false;
}

That bit should do the trick but... to be honest... I didn't want to do it that way as it seems a lil bit not right to me.
(This post was last modified: 10-17-2013 10:47 PM by Kiekos.)
10-17-2013 10:43 PM
Find all posts by this user Quote this message in a reply
Pherael Offline
Member

Post: #14
RE: Monster spawning
You don't need to call you spawn function in update loop, you can call it after first World.update in init function.

Also, you can use dummy object. I create something similar in my game - spawn point since my enemy are random monster. When hero is near, but not too near, spawn point creating a certain amount of enemy.

But if your enemies are located in specific location, it would be better just add them in editor, and then load data from database in create function.
(This post was last modified: 10-18-2013 08:46 AM by Pherael.)
10-18-2013 08:17 AM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #15
RE: Monster spawning
Quote:But if your enemies are located in specific location, it would be better just add them in editor, and then load data from database in create function.
Oh I get it, that sound like a reasonable solution.

Quote:You don't need to call you spawn function in update loop, you can call it after first World.update in init function.
Well... There was no World.update() in my init function so after I added it and moved mob spawning function (from udpate loop to init function) they started spawning, but there is a little problem with background loader then - it either shows 0% or 100% of loading process, it doesn't increase.

So I guess I'll either leave it as it is right now or follow the first method.
10-18-2013 02:50 PM
Find all posts by this user Quote this message in a reply
Post Reply