A monster needs to tell me where it is!
A monster needs to tell me where it is!
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!
- 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!
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.
I've done this in a goofy/long-winded way to make sense of it, you would likely just normally do this...
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
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.
Re: A monster needs to tell me where it is!
This worked! Thank you for reminding me about pointers.
Re: A monster needs to tell me where it is!
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)
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)
- 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!
@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.
Re: A monster needs to tell me where it is!
This one can. That's why it is so scary.Ixnatifual wrote:Guys, snails can't speak.
Re: A monster needs to tell me where it is!
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
The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563
My preciousss: http://www.moonsharp.org
Re: A monster needs to tell me where it is!
Another thing you can do is have the monster tell your script where he is each time he moves, like this:
Monster code:
Level Script Code:
Monster code:
Code: Select all
onMove = function(monster, direction)
local myScript = findEntity("my_level_script")
If myScript then
myScript:notifyMonsterMoved(monster)
end
end
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!
won't that tell where the monster just moved from?
Finished Dungeons - complete mods to play