spawn a monster with a script

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

spawn a monster with a script

Post by FeMaiden »

I know this is has probably been answered before..

how do I spawn a monster with a script, not using a spawner. spawners don't move.
I want the game to spawn a new monster in the same tile the first monster was on when it triggers the script with "onDie"

I see this command

spawn("uggardian", party.level, 11, 15, 1, 0)

and this

spawn("herder",1,1,1,1,0)

and this

spawn("viper_root", party.level, 17, 4, 3, 0)


but I don't understand what those numbers are at the end, and what does "party.level" mean?

are those numbers after it, the coordinates?
like if my map is at x2 y3 z1, i would need to type

spawn("viper_root", party.level, 2, 3, 1, 0)

?

but i'm assuming this command spawns the monster in front of the party, I'm hoping to make it spawn the new monster in the same square that the first monster was in when it died,
in case the party used a ranged attack, I don't want it to look weird that the second monster spawns in front of the party when the original was 3 squares away.
I want it to spawn one of those swarms that has 2 monsters on the same tile when they kill the first monster so it looks like it split into 2.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: spawn a monster with a script

Post by minmay »

http://www.grimrock.net/modding/scripting-reference/
Look for the "spawn" function on that page. It's in the "Global Functions" section near the top.

For your specific case, you'd probably prefer to use the shortcut function GameObject:spawn(), which simply spawns the object at the same position as the GameObject used to call the method. So party:spawn("torch") will spawn a torch at the party's position. Your onDie hook could just read:

Code: Select all

onDie = function(self)
  self.go:spawn("twigroot")
end
and it would spawn a twigroot at the same position as the dying monster. Note that this creates a console warning since there's already a monster at that position (the dying one), so you should probably delay it until later in the frame instead.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: spawn a monster with a script

Post by FeMaiden »

minmay wrote:http://www.grimrock.net/modding/scripting-reference/
Look for the "spawn" function on that page. It's in the "Global Functions" section near the top.

For your specific case, you'd probably prefer to use the shortcut function GameObject:spawn(), which simply spawns the object at the same position as the GameObject used to call the method. So party:spawn("torch") will spawn a torch at the party's position. Your onDie hook could just read:

Code: Select all

onDie = function(self)
  self.go:spawn("twigroot")
end
and it would spawn a twigroot at the same position as the dying monster. Note that this creates a console warning since there's already a monster at that position (the dying one), so you should probably delay it until later in the frame instead.
how do I "delay" it?
I like that "simpler" solution.

however, i would like a better explanation of the earlier example. i'm reading the scripting reference

in the earlier examples,

(viper_root, party.level, 17,4,3,0)

"party.level" is the level index?
17.4 is the xy coordinates of the map?
while the 3 means the mob will face west?
and the 0 is the elevation?
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: spawn a monster with a script

Post by FeMaiden »

well, i wrote this script

Code: Select all

function herderSplit()
	onDie = function(self)
  	self.go:spawn("herder_swarm")
end
end
and nothing happens...at least it doesn't crash...
theres no new monster spawn, no hud warning either.

I made sure to connect the herder to my script with onDie and it executes the herderSplit function
but nothing happens
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: spawn a monster with a script

Post by THOM »

FeMaiden wrote:
17.4 is the xy coordinates of the map?
while the 3 means the mob will face west?
and the 0 is the elevation?
yes

the "party.level" term means, the level, on which the object is created is the same, the party is on. You could also write the specific number like "12". Or "self.level" which would mean the same level, the existing object is on.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: spawn a monster with a script

Post by FeMaiden »

this time i tried this

Code: Select all

function herderSplit()
  	self.go:spawn("herder_swarm")
end
and it correctly spawns the herder swarm but not where the monster is. it spawns it where I spaced the scripting entity, I basically just scripted my own stationary "spawner" asset.

why is this so hard for me...
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: spawn a monster with a script

Post by THOM »

a yes - the "self" is always the object, the command comes from. In this case a script-entity. If you would put the code in the onDie hook of the monster, the "self" would be the monster.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: spawn a monster with a script

Post by FeMaiden »

THOM wrote:a yes - the "self" is always the object, the command comes from. In this case a script-entity. If you would put the code in the onDie hook of the monster, the "self" would be the monster.
how do I put the code in the onDie hook of the monster?
is that the space where it says "select target"?


well, I just tried this

Code: Select all

function split()
herder_5:spawn()
onDie = function(self)
  	self.go:spawn("herder_swarm")
end
end
and the whole dungeon editor crashed to the desktop, completely closing the app...

please just explain what the "onDie hook" is and how I "put code in the onDie hook"

am I supposed to create a scripting entity and link the onDie connector to it?
is the "hook" someplace else it in the monster object itself?
Last edited by FeMaiden on Sun Oct 25, 2015 11:56 pm, edited 1 time in total.
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: spawn a monster with a script

Post by THOM »

The onDie hook is part of the monster-component.

you can add there something like

Code: Select all

			onDie = function(self)
				self.go.sound:fadeOut(1)
			end,
this example is copied from the definition of the "fire-elemental" monster.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
FeMaiden
Posts: 290
Joined: Wed Jan 16, 2013 8:16 am

Re: spawn a monster with a script

Post by FeMaiden »

*sigh*


in the components of the monster I see

gravity
model
animation
monster
brain
move
turn
basicAttack

where is the "hook"?

I expand "monster" and see

AI state
monster level
health
items
connectors


where is the "hook"?

if I expand connectors
I see onDie <select target> and a space to execute a function

where is the "Hook"?

so I just paste that code in one long line into the <select target> space?
Post Reply