Library, Globally Accessible Functions

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Library, Globally Accessible Functions

Post by NutJob »

I have this function...

Code: Select all

function grantPuzzleExp(totalExp)
	local c = nil; local p = party.party; local a = {}; local exp = 0;

	for i=1, 4 do
		c = p:getChampion(i)
		if c:getEnabled() and c:isAlive() then table.insert(a,i) end
	end

	exp = math.ceil(totalExp / #a)

	for _,n in ipairs(a) do
		p:getChampion(n):gainExp(exp)
	end
end
...I would like to access without having to put it in every single script_entity when I want to use it.

How do I make a library that all scripts can access so I can call my generic functions?
User avatar
jxjxjf
Posts: 33
Joined: Fri Oct 24, 2014 6:26 am

Re: Library, Globally Accessible Functions

Post by jxjxjf »

I'm by no means a lua expert, but the way I've been doing this is by calling the function through the name of the script entity.

So, if you had that function in a script entity named "expScript", you'd call it like:

expScript.script:grantPuzzleExp(100);

It's ugly, but it works for me. haha
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Library, Globally Accessible Functions

Post by NutJob »

I'll give it a try. Thank you very much. Having that script in 12 script_entities seems like a waste when I can just do an include or import of a file or something.
User avatar
jxjxjf
Posts: 33
Joined: Fri Oct 24, 2014 6:26 am

Re: Library, Globally Accessible Functions

Post by jxjxjf »

Yeah, it sort of defeats the purpose of writing a beautiful function in the first place. haha

Hope it works out. :)
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Library, Globally Accessible Functions

Post by NutJob »

It sort of works. The function runs without error but the parameter (the experience integer) does not get passed along.

Edit: the function now thinks the 'totalExp' is a table. Stumped. ~shrugs~
User avatar
jxjxjf
Posts: 33
Joined: Fri Oct 24, 2014 6:26 am

Re: Library, Globally Accessible Functions

Post by jxjxjf »

Yeah, you're right... I've been doing this without parameters. Just tried it and integers don't pass, and a string will crash the whole thing.

I knew it was ugly, but I had hoped it would work for you. Sorry. :(

Hopefully someone else can give a real response.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Library, Globally Accessible Functions

Post by NutJob »

No worries, appreciate the insight on doing like this, in the first place. ~cheers~
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Library, Globally Accessible Functions

Post by Batty »

You should search the LoG1 modding forum, many of these scripting issues are dealt with there.
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Library, Globally Accessible Functions

Post by SnowyOwl47 »

I got it guys! Because i've already done this many times in the Easy Scripts in my Basic Scripting reference its easy for me.

Make a script named whatever but ill call mine game,
make a function like you have:

Code: Select all

function grantPuzzleExp(totalExp)
   local c = nil; local p = party.party; local a = {}; local exp = 0;

   for i=1, 4 do
      c = p:getChampion(i)
      if c:getEnabled() and c:isAlive() then table.insert(a,i) end
   end

   exp = math.ceil(totalExp / #a)

   for _,n in ipairs(a) do
      p:getChampion(n):gainExp(exp)
   end
end
And if you set it up correctly sense the scrip is named game you simply say

Code: Select all

game.script.grantPuzzleItem(100)
When making your own methods and code you can use globally you never put : never: ever: ever: its all dots .......

IF YOU EVER PLACE : instead of a dot . you it doesn't happen no errors but the grantPuzzleItem function won't get called because you don't use :
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Library, Globally Accessible Functions

Post by SnowyOwl47 »

Batty wrote:You should search the LoG1 modding forum, many of these scripting issues are dealt with there.
Log1 is way different then log2 so it wouldn't be a help he'd have to figure out how to convert it.
Post Reply