Page 1 of 2

A monster needs to tell me where it is!

Posted: Thu Dec 13, 2012 12:51 am
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!

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

Posted: Thu Dec 13, 2012 1:20 am
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

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

Posted: Thu Dec 13, 2012 4:12 am
by vorebane
This worked! Thank you for reminding me about pointers. :)

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

Posted: Thu Dec 13, 2012 9:52 am
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)

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

Posted: Thu Dec 13, 2012 9:57 am
by Ixnatifual
Guys, snails can't speak.

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

Posted: Thu Dec 13, 2012 10:59 am
by JohnWordsworth
@petri: Oh, that's cool. I didn't realise that every entity lives in Lua's global namespace. :)

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

Posted: Thu Dec 13, 2012 11:49 am
by thomson
Ixnatifual wrote:Guys, snails can't speak.
This one can. That's why it is so scary.

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

Posted: Thu Dec 13, 2012 11:58 am
by Xanathar
I bet it speaks slowly though.

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

Posted: Thu Dec 13, 2012 5:22 pm
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

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

Posted: Thu Dec 13, 2012 7:02 pm
by Komag
won't that tell where the monster just moved from?