Some scripting

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Some scripting

Post by cromcrom »

Hi all,

here is some script I created. You can use it as a script_entity, or on a onDie, or whatever.
What it does is that when the action is called, the party members that succeed in a willpower check (custom stuff), willl gain a set random number of Exp.

I am a self taught modder/coder, and this piece of code is certainly improvable, I release it anyways, in the hope that other beginners can learn some stuff from it, or advanced coders can improve it (especially the iteration process of checking all party members).

Feel free to ask if questions.

Code: Select all

local willpower1 = party:getChampion(1):getStat("willpower")
local willpower2 = party:getChampion(2):getStat("willpower")
local willpower3 = party:getChampion(3):getStat("willpower")
local willpower4 = party:getChampion(4):getStat("willpower")
local name1 = party:getChampion(1):getName()
local name2 = party:getChampion(2):getName()
local name3 = party:getChampion(3):getName()
local name4 = party:getChampion(4):getName()

local randomnumber = math.random(25)
local experiencegained = math.random(10,25)

		if willpower1 < randomnumber
		then 
		hudPrint(name1.." doesn't learn anything.")
		else
		hudPrint(name1.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
		party:getChampion(1):gainExp(experiencegained)
		end
local randomnumber = math.random(25)
local experiencegained = math.random(10,25)
		if willpower2 < randomnumber
		then 
		hudPrint(name2.." doesn't learn anything.")
		else
		hudPrint(name2.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
		party:getChampion(2):gainExp(experiencegained)
		end
local randomnumber = math.random(25)
local experiencegained = math.random(10,25)
		if willpower3 < randomnumber
		then
		hudPrint(name3.." doesn't learn anything.")
		else
		hudPrint(name3.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
		party:getChampion(3):gainExp(experiencegained)end
local randomnumber = math.random(25)
local experiencegained = math.random(10,25)
		if willpower4 < randomnumber
		then
		hudPrint(name4.." doesn't learn anything.")
		else
		hudPrint(name4.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
		party:getChampion(4):gainExp(experiencegained)
		end
A trip of a thousand leagues starts with a step.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Some scripting

Post by Komag »

I like this, it really could add to a more full role-play element, nice!
Finished Dungeons - complete mods to play
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Some scripting

Post by Shroom »

I haven't tried this as I am at work, but I think this should work

Code: Select all

local awill = {}
awill[1] = party:getChampion(1):getStat("willpower")
awill[2] = party:getChampion(2):getStat("willpower")
awill[3] = party:getChampion(3):getStat("willpower")
awill[4] = party:getChampion(4):getStat("willpower")

local aname = {}
aname[1] = party:getChampion(1):getName()
aname[2] = party:getChampion(2):getName()
aname[3] = party:getChampion(3):getName()
aname[4] = party:getChampion(4):getName()

for i=1,4 do
	if awill[i] < math.random(25) then
		hudPrint(aname[i].." doesn't learn anything.")
	else
		local experiencegained = math.random(10,25)
	        hudPrint(aname[i].." succeeds to understand what is going on and gain "..experiencegained.." XP." )
	        party:getChampion(i):gainExp(experiencegained)
	end	
end
It uses arrays to hold the 4 scores and names and the other values are generated as they are needed.

Hope it helps (and works!)
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Some scripting

Post by antti »

Shroom wrote:I haven't tried this as I am at work, but I think this should work

Code: Select all

local awill = {}
awill[1] = party:getChampion(1):getStat("willpower")
awill[2] = party:getChampion(2):getStat("willpower")
awill[3] = party:getChampion(3):getStat("willpower")
awill[4] = party:getChampion(4):getStat("willpower")

local aname = {}
aname[1] = party:getChampion(1):getName()
aname[2] = party:getChampion(2):getName()
aname[3] = party:getChampion(3):getName()
aname[4] = party:getChampion(4):getName()

for i=1,4 do
	if awill[i] < math.random(25) then
		hudPrint(aname[i].." doesn't learn anything.")
	else
		local experiencegained = math.random(10,25)
	        hudPrint(aname[i].." succeeds to understand what is going on and gain "..experiencegained.." XP." )
	        party:getChampion(i):gainExp(experiencegained)
	end	
end
It uses arrays to hold the 4 scores and names and the other values are generated as they are needed.

Hope it helps (and works!)
You know, this isn't very far away from a more generic function where you could test out any desired skill or stat against any value. You know, so that you could just insert testStat("dexterity", 20) into your script somewhere to try out a feat of dexterity and then the function could return true if any of the characters succeeded. Or something along those lines. 8-)
Steven Seagal of gaming industry
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Some scripting

Post by Shroom »

Like this maybe?

Code: Select all

function teststat(stat, value, xpmin, xpmax)

	local astat = {}
	astat[1] = party:getChampion(1):getStat(stat)
	astat[2] = party:getChampion(2):getStat(stat)
	astat[3] = party:getChampion(3):getStat(stat)
	astat[4] = party:getChampion(4):getStat(stat)
	
	local aname = {}
	aname[1] = party:getChampion(1):getName()
	aname[2] = party:getChampion(2):getName()
	aname[3] = party:getChampion(3):getName()
	aname[4] = party:getChampion(4):getName()
	
	for i=1,4 do
	   if astat[i] < math.random(value) then
	      hudPrint(aname[i].." doesn't learn anything.")
	   else
	      local experiencegained = math.random(xpmin,xpmax)
	           hudPrint(aname[i].." succeeds to understand what is going on and gain "..experiencegained.." XP." )
	           party:getChampion(i):gainExp(experiencegained)
	   end   
	end

end
I think you could pass variables like this? So the call would be :-

teststat("willpower",25,10,25)
User avatar
Filipsan
Posts: 84
Joined: Fri Mar 16, 2012 10:18 am

Re: Some scripting

Post by Filipsan »

or

Code: Select all

function teststat(stat, testmaxvalue, xpmin, xpmax)

   local astat = 0
   local aname = ""
   
   for i=1,4 do
      astat = party:getChampion(i):getStat(stat)
      aname = party:getChampion(i):getName()
      if astat < math.random(testmaxvalue) then
         hudPrint(aname.." doesn't learn anything.")
      else
         local experiencegained = math.random(xpmin,xpmax)
         hudPrint(aname.." succeeds to understand what is going on and gain "..experiencegained.." XP." )
         party:getChampion(i):gainExp(experiencegained)
      end   
   end
end
not tested though - i don't know LUA+ i don't have steam version :)
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Some scripting

Post by Shroom »

even neater :D
Post Reply