Trouble with monster locations and if statements

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
Ulfsark
Posts: 18
Joined: Sun May 05, 2013 5:52 pm

Trouble with monster locations and if statements

Post by Ulfsark »

Hey guys, I am working on a puzzle where the player must use teleprompters to switch the locations of monsters in 3 cells and arrange them in the correct order.
Correctly organizing the creatures should allow for a door to open. However I am having trouble accessing the x,y coordinates of the creates and using them effectively in an if statement. I have scoured the forums and modding pages and have yet to find a soulution.

Basically what I need is something that if all 3 monsters are in the correct location, the door opens.

Thank you very much for your help, this modding community is awesome, there are so many good resources everywhere, sadly I am still not able to figure this out even using them :p

Thanks!
-Ulfsark
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: Trouble with monster locations and if statements

Post by Ryeath_Greystalk »

I think there are a couple ways you could go about this. If you are just using generic monsters id's then you would probably want to use a entitiesAt() to check and see if the monsters are in the right order. Something like

Code: Select all

local monster1 = false
local monster2 = false

for i in entitesAt(x,y)
    if i.name == "snail" then 
         monster1 = true
    end
end
for i in entitesAt(x,y)
    if i.name == "spider" then
          monster2 = true
    end
end

if monster1 and monster2 then
    secret_door_one:open()
end
That is incomplete code but should give you an idea.

If you want to us the monster x,y then you need to use the id, which is why I use custom names when I need to use x,y. Something like,

Code: Select all

local monster1 = false
local monster2 = false

if test_snail_one.x == 15 and test_snail_one.y == 22 then
    monster1 = true
end
if test_spider_one.x == 16 and test_spider_one.y == 22 then
   monster2 = true
end

if monster1 and monster2 then
     super_secret_spidey_door:open()
end
(again incomplete but an idea)

Expect Komag to appear shortly and show you how to do it in about 3 lines. His uber ninja coding skills are awesome.

Good luck with the project.
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Trouble with monster locations and if statements

Post by Komag »

actually Ryeath_Greystalk's solutions would be fine I think, since you only have three things. It could possibly be crunched down, but not easily and wouldn't reduce it much.

Also, you could use the first version with ids if you wanted to, not just names.

Code: Select all

for i in entitesAt(x,y)
    if i.id == "test_snail_one" then 
         monster1 = true
    end
end
Finished Dungeons - complete mods to play
Ulfsark
Posts: 18
Joined: Sun May 05, 2013 5:52 pm

Re: Trouble with monster locations and if statements

Post by Ulfsark »

You guys are awesome. Thank you very much for your help.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Trouble with monster locations and if statements

Post by JohnWordsworth »

Just a random suggestion for a minor improvement to make your dungeon a bit more robust going forward, instead of hard-coding the coordinates in your script like this...

Code: Select all

if ( test_snail_one.x == 24 and test_snail_one.y == 15 ) then
   ...
end
You might want to store these as variables at the top of the script, like so...

Code: Select all

snail_one_x = 24;
snail_one_y = 15;

if ( test_snail_one.x == snail_one_target_x and test_snail_one.y == snail_one_target_y ) then
  ...
end
Even better perhaps, you could actually put down some hidden pressure plates as the targets, and completely turn off all of the "triggered by" options for the pressure plates. So, in the square you would like the snail to be, put a pressure plate with ID plate_snail_target and use the following:

Code: Select all

if ( test_snail_one.x == plate_snail_target.x and test_snail_one.y == plate_snail_target.y ) then
  ...
end
Obviously, these two suggestions are just a minor extension to Ryeath_Greystalk's solution, but the benefit comes that if you ever need to move this part of the dungeon to another part of the floor to make way for something else, you won't need to remember to update your script (as you will see the pressure plates on the map). Perhaps over kill, but if you have a lot of scripts like this - it's not much extra work at all and it just seems tidier than having loads of scripts with precise positions in them, which is just asking for a random bug to appear at some point!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Ulfsark
Posts: 18
Joined: Sun May 05, 2013 5:52 pm

Re: Trouble with monster locations and if statements

Post by Ulfsark »

Hey guys, I ran into a small issue trying to implement this.

Here is my code with the error message "'do' expected near 'if'" written where it occurs in the script. I am sure it is something simple but if statements in Lua is something I am still getting used to.

Thanks in advance!

Code: Select all

	
local monster1 = false
		local monster2 = false
			local monster3 = false

for i in entitesAt(14,10)
    if i.name == "crabP" then -- 'do' expected near 'if'
         monster1 = true
			else monster1 = false
   		 end
		end
		
for i in entitesAt(16,10)
    if i.name == "herderP" then-- 'do' expected near 'if'
          monster2 = true
			else monster2 = false
  		  end
		end

for i in entitesAt(18,10)
    if i.name == "crowernP" then-- 'do' expected near 'if'
          monster3 = true
			else monster3 = false
  		  end
		end
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: Trouble with monster locations and if statements

Post by Ryeath_Greystalk »

just add do at the end of your for statement

for i in entitiesAt(14,10) do

the error shows up on the next line but that's just the way the debugger works
Ulfsark
Posts: 18
Joined: Sun May 05, 2013 5:52 pm

Re: Trouble with monster locations and if statements

Post by Ulfsark »

Ryeath_Greystalk wrote:just add do at the end of your for statement

for i in entitiesAt(14,10) do

the error shows up on the next line but that's just the way the debugger works
Thank you very much! I knew it was something simple :/
Post Reply