Scripting to Damage Prisoners

Discussion about Mods for Prison Architect

Moderator: NBJeff

User avatar
Seth2697
level1
level1
Posts: 35
Joined: Thu Oct 16, 2014 10:01 am

Re: Scripting to Damage Prisoners

Postby Seth2697 » Sun Oct 19, 2014 10:51 am

Hmm is this about right?


function Create()

Object.SetProperty("Prisoner", "Damage", 1);

end

function Death

local mineRadius = 5; -- 5 tile radius for instance
local amountOfDamage = 1; -- Kills 'em outright
local nearbyPrisoners = Object.GetNearbyObjects("Prisoner", 1000000);

for prisonerName,prisonerDistance in pairs(nearbyPrisoners) do
Object.SetProperty(prisonerName, "Damage", amountOfDamage);

end

or am I going about it the wrong way? Sorry i'm such a noob my LUA experience is limited hate to make you have to keep breaking it down for me
User avatar
Pruvan
level2
level2
Posts: 94
Joined: Mon May 27, 2013 4:11 am

Re: Scripting to Damage Prisoners

Postby Pruvan » Sun Oct 19, 2014 1:10 pm

No, it should look something like this:

landmine.lua

Code: Select all

function Create()
end

function Update(timePassed)
   local mineRadius = 5; -- 5 tile radius for instance
   local amountOfDamage = 1; -- Kills 'em outright
   local nearbyPrisoners = Object.GetNearbyObjects("Prisoner", mineRadius);
   for prisonerName,prisonerDistance in pairs(nearbyPrisoners) do
      Object.SetProperty(prisonerName, "Damage", amountOfDamage);
end
cathedral30
level0
Posts: 9
Joined: Sat Nov 30, 2013 1:59 pm

Re: Scripting to Damage Prisoners

Postby cathedral30 » Sun Oct 19, 2014 7:40 pm

Thanks for all the help but I'm still getting the same problem, my current code is

Code: Select all

function Create()
   Object.SetProperty( "ReadytoUse", true)
   
end

function Update( timePassed )

   if Object.GetProperty("ReadytoUse") then       
        Object.CreateJob("Death")
      return
   end
   local prisoners = Object.GetNearbyObjects("Prisoner", 100)
   for prisonerName,prisonerDistance in pairs(objects) do
      Object.SetProperty(prisoners, "Damage", 1)

   
   Object.SetProperty( "ReadytoUse", false)
   
   sleep(3)
   
end

function JobComplete_Death()

   Object.SetProperty("ReadytoUse", true)
   
end
User avatar
Seth2697
level1
level1
Posts: 35
Joined: Thu Oct 16, 2014 10:01 am

Re: Scripting to Damage Prisoners

Postby Seth2697 » Sun Oct 19, 2014 8:30 pm

I put in the script you typed out there but there just walking over the mines with no damage being caused what am I missing?
User avatar
Pruvan
level2
level2
Posts: 94
Joined: Mon May 27, 2013 4:11 am

Re: Scripting to Damage Prisoners

Postby Pruvan » Mon Oct 20, 2014 12:19 pm

cathedral30 wrote:Thanks for all the help but I'm still getting the same problem

One more thing I forgot, which I missed cause of the code tags here. You have to start your script files with an empty line. So your first line should be blank, before adding any functions, variables, etc... The game probably can't find your Create() function cause it's on the first line. Just add an empty line before it and things should start working.

Some other problems I found though:

Code: Select all

function Create()
   Object.SetProperty( "ReadytoUse", true)
   
end

function Update( timePassed )

   if Object.GetProperty("ReadytoUse") then       
        Object.CreateJob("Death")
      return
   end

ReadytoUse is always going to be true when you create your object. So anything below this point will never run. This may be intentional, but I'm pointing it out cause I think it might not be. I'm also pretty sure "Death" is not a valid job.

Code: Select all

   local prisoners = Object.GetNearbyObjects("Prisoner", 100)
   for prisonerName,prisonerDistance in pairs(objects) do
      Object.SetProperty(prisoners, "Damage", 1)

Looks like I fed you broken code, my apologies. I was probably mixing my example scripts. pairs(objects) should be pairs(prisoners), since objects doesn't exist in this example. Alternatively, "local prisoners" should be "local objects". Whichever you prefer. Your for loop needs to end with an "end" statement as well, which I also forgot. I'm too used to C++/C# I guess. You also changed prisonerName to prisoners inside the SetProperty function for some reason. It should be prisonerName, since we're assigning the property to each prisoner via their name.

Code: Select all

   sleep(3)

sleep() is not a valid function. I'm also not sure why you'd want to use it. If you want to stagger the amount of times your script runs, you should probably make use of Update's timePassed variable. For example by making a local timer (outside your functions), adding timePassed to said timer and then checking if enough time has passed before continuing:

Code: Select all

local timer = 0;

...

function Update( timePassed )

   timer = timer + timePassed
   if timer < 3 then
      return
   else
      timer = 0
   end

In the end your code should look something like this (final draft this time I hope):

Code: Select all

local timer = 0

function Create()

   Object.SetProperty( "ReadytoUse", true)

end

function Update( timePassed )

   timer = timer + timePassed
   if timer < 3 then
      return
   else
      timer = 0
   end

   if Object.GetProperty("ReadytoUse") then
      Object.CreateJob("Death")
      return
   end

   local prisoners = Object.GetNearbyObjects("Prisoner", 100)
   for prisonerName,prisonerDistance in pairs(prisoners) do
      Object.SetProperty(prisonerName, "Damage", 1)
   end

   Object.SetProperty( "ReadytoUse", false)

end

function JobComplete_Death()

   Object.SetProperty("ReadytoUse", true)

end

Remember to add an empty line at the start (and end, for good measure) of your script if you decide to copy this. Like I said, the code tags like to remove empty lines. I also left the "ReadytoUse" stuff as-is, since I'm not sure what you were trying to do there.
User avatar
Pruvan
level2
level2
Posts: 94
Joined: Mon May 27, 2013 4:11 am

Re: Scripting to Damage Prisoners

Postby Pruvan » Mon Oct 20, 2014 12:26 pm

Seth2697 wrote:I put in the script you typed out there but there just walking over the mines with no damage being caused what am I missing?


Same as above, forgot to end the for loop with an "end". Try this:

Code: Select all

function Create()
end

function Update(timePassed)
   local mineRadius = 5; -- 5 tile radius for instance
   local amountOfDamage = 1; -- Kills 'em outright
   local nearbyPrisoners = Object.GetNearbyObjects("Prisoner", mineRadius);
   for prisonerName,prisonerDistance in pairs(nearbyPrisoners) do
      Object.SetProperty(prisonerName, "Damage", amountOfDamage);
   end
end

And remember to add empty lines at the start (and end), same as above. Tested this and it should work.

EDIT: Also don't forget to give your object the "scripted" property in materials.txt
cathedral30
level0
Posts: 9
Joined: Sat Nov 30, 2013 1:59 pm

Re: Scripting to Damage Prisoners

Postby cathedral30 » Mon Oct 20, 2014 7:36 pm

Thanks so much, everything is working perfectly now.

Just wondering, is it possible to have the area defined as the room the object is in rather than just a distance around it and if there is, what would the code be?
User avatar
Seth2697
level1
level1
Posts: 35
Joined: Thu Oct 16, 2014 10:01 am

Re: Scripting to Damage Prisoners

Postby Seth2697 » Mon Oct 20, 2014 7:55 pm

Well my LUA is exactly like above now but still having the same result. Is it something maybe in my materials.txt im putting in wrong? For some reason I keep getting the mine being called object_mine in game maybe that's why? I've got the script property and everything not sure what it could be, here's the materials.txt:

Materials.txt:


BEGIN Object
Name Mine
Width 1
ConstructionTime 3.0000
MoveSpeedFactor 0.500000
Toughness 10.0000
Price -100
AttachToWall false
Group StaticObject
BlockedBy Wall
BlockedBy StaticObject
BlockedBy UtilityStation
MadeOf SolidMetal
BEGIN Sprite
x 0
y 0
w 2
h 2
RotateType 4
Properties StaticObject
Properties Scripted
END
User avatar
Pruvan
level2
level2
Posts: 94
Joined: Mon May 27, 2013 4:11 am

Re: Scripting to Damage Prisoners

Postby Pruvan » Mon Oct 20, 2014 8:41 pm

cathedral30 wrote:Just wondering, is it possible to have the area defined as the room the object is in rather than just a distance around it


AFAIK this is not possible yet.

Seth2697 wrote:Well my LUA is exactly like above now but still having the same result.


materials.txt looks fine. Does your lua file have the same name as your object? In your case it should be called mine.lua. Make sure it's in the data\scripts folder as well (so data\scripts\mine.lua).

You can define your object's displayed name in-game in base-language.txt, same as for every other object. Seeing object_mine in-game when you haven't done this yet is normal behavior.
User avatar
Seth2697
level1
level1
Posts: 35
Joined: Thu Oct 16, 2014 10:01 am

Re: Scripting to Damage Prisoners

Postby Seth2697 » Tue Oct 21, 2014 12:34 am

First off thanks for the patience again I know this is probably getting repetitive. Second you will def be added to the contributors section of my mod I couldn't have made this without you. But still having the problems... I added the language txt, my LUA was named mine.lua just like in my materials.txt, folders all in the right spots Materials seems written correctly directly copied your LUA code and they have spaces above and below. everything seems checked but there just still walking over the mines I lay them all in my main walk ways and nothing...

Mind if I email you the file so you can see for yourself what i'm doing wrong?
User avatar
Seth2697
level1
level1
Posts: 35
Joined: Thu Oct 16, 2014 10:01 am

Re: Scripting to Damage Prisoners

Postby Seth2697 » Tue Oct 21, 2014 8:45 am

Fixed! Thanks Pruvan! Overlooked no end line on the sprite cords in my materials.txt, sneaky sneaky. Everyone check out the Land Mine mod I just posted if you'd like to see the final result!

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 2 guests