A monster needs to tell me where it is!

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
vorebane
Posts: 41
Joined: Wed Oct 03, 2012 4:31 am

A monster needs to tell me where it is!

Post by vorebane »

I am trying to write a script that will check where a specific monster is, and do something different based on what level its on and what its x and y are. However, all my attempts to reference the monster cause the script to stop my playtest. I'm going to go at explaining this using wild cards, I want to keep my secrets for a bit just yet. :) Whether I ask for monster_1.level, themonster.level (having made a spawn command that specifically ids the monster as 'themonster'), findEntity(themonster).level, I get an error message. Am I misusing the dot operator here? Based on the scripting reference page I am honestly not sure what I am doing wrong. Please help me out, I am pretty sure this is my last big hurdle!
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: A monster needs to tell me where it is!

Post by JohnWordsworth »

Just a tad mixed up I think, there is a difference between the ID of an object you have placed (which is a text string) and a variable that points to it. If you have placed a monster and, in the editor you have named it 'my_snail_01' then you could use the following to find out where that snail is (if it still exists).

This is fully expanded so you can see every step. Everything after -- on a line is ignored, and just lets you document your code.

Code: Select all

local snail_id = "my_snail_01";  -- This is just storing the ID string of your snail.
local snail = findEntity(snail_id);  -- This is storing a pointer to the snail, which might be 'nil' if it's not found.

-- You have to check if the snail has been found or not, otherwise, calling snail.level might cause a crash.
if ( snail ~= nil ) then
  hudPrint("Found the Snail");
  local snail_level = snail.level;
end
I've done this in a goofy/long-winded way to make sense of it, you would likely just normally do this...

Code: Select all

local snail = findEntity("my_snail_01");

if ( snail ~= nil ) then
  -- Do something with snail.level
end
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
vorebane
Posts: 41
Joined: Wed Oct 03, 2012 4:31 am

Re: A monster needs to tell me where it is!

Post by vorebane »

This worked! Thank you for reminding me about pointers. :)
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: A monster needs to tell me where it is!

Post by petri »

Even better (more efficient, less code) would be:

if snail_1 then
print("the snail is on level", snail_1.level)
end

The variable snail_1 is nil if the entity with the same name does not exist. You only need to use findEntity if you need to programmatically set up the id. E.g.

local i = 5
local id = "snail_"..i
local snail = findEntity(id)
Ixnatifual

Re: A monster needs to tell me where it is!

Post by Ixnatifual »

Guys, snails can't speak.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: A monster needs to tell me where it is!

Post by JohnWordsworth »

@petri: Oh, that's cool. I didn't realise that every entity lives in Lua's global namespace. :)
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
thomson
Posts: 337
Joined: Thu Sep 13, 2012 9:55 pm
Location: R'lyeh
Contact:

Re: A monster needs to tell me where it is!

Post by thomson »

Ixnatifual wrote:Guys, snails can't speak.
This one can. That's why it is so scary.
[MOD] Eye of the Beholder: Waterdeep sewers forum sources; Grimtools (LoG1 -> LoG2 converter) sources
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: A monster needs to tell me where it is!

Post by Xanathar »

I bet it speaks slowly though.
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
User avatar
pferguso
Posts: 40
Joined: Tue Nov 06, 2012 6:09 pm

Re: A monster needs to tell me where it is!

Post by pferguso »

Another thing you can do is have the monster tell your script where he is each time he moves, like this:

Monster code:

Code: Select all

onMove = function(monster, direction)
      local myScript = findEntity("my_level_script")
      If myScript then
             myScript:notifyMonsterMoved(monster)
      end
end
Level Script Code:

Code: Select all

function notifyMonsterMoved(sender, monster)
       print("monster now at "..monster.x..", "..monster.y.."")
end
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: A monster needs to tell me where it is!

Post by Komag »

won't that tell where the monster just moved from?
Finished Dungeons - complete mods to play
Post Reply