Page 1 of 1

Some scripting

Posted: Thu Sep 13, 2012 9:48 am
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

Re: Some scripting

Posted: Thu Sep 13, 2012 2:38 pm
by Komag
I like this, it really could add to a more full role-play element, nice!

Re: Some scripting

Posted: Thu Sep 13, 2012 3:17 pm
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!)

Re: Some scripting

Posted: Thu Sep 13, 2012 3:37 pm
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-)

Re: Some scripting

Posted: Thu Sep 13, 2012 3:52 pm
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)

Re: Some scripting

Posted: Thu Sep 13, 2012 4:12 pm
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 :)

Re: Some scripting

Posted: Thu Sep 13, 2012 4:17 pm
by Shroom
even neater :D