Jump to content

Requiring an existing script And Calling a function from it

Recommended Posts

Hey guys I am trying to use a existing script to do some of the logic that I want to incorporate into my mod.  The code I'm looking for is in pigman.lua  So what I am doing in m mod main is local pigScript = require(pigman) this is located in scripts/pigman.lua  The game finds the file no problem.  I also have added 

Assets = {
	Asset("SCRIPT", "scripts/pigman.lua")
local pigScript = GLOBAL.require("pigman")

I then put into pigman.lua a little function for testing.  Here it is

function test()
	print("hello world");

Then from my mod main I have a GLOBAL function tied to Test Function to see if I can call this. Here it is.

function G.CallOtherScript()
	-- also have tried pigScript:test()
	-- test()

So my question is what is the proper way for me to call a function from another script that isn't in mod main? I should also add that if there is anything special when passing arguments to "said" function that would be nice to know as well. Thanks for everything guys / gals.

Edited by Silisiban
Link to comment
Share on other sites

require is to load module, not to run a script.
Because it's not running the script, test() is not assigned in the global scope. Meaning test() is not available in the file that has G.CallOtherScript().
pigman.lua must have a return if it's used as the module.

Here's the proper way to write the module. 

Pigman = {}

function Pigman.test()	
  print("Hello World")

return Pigman

This will leave the reference of the method test() into Pigman table. Then pigScript.test() will do print the message.

Actually, it's not recommended if you want to do something with ingame instances or components. Create your own component in this case. 

Link to comment
Share on other sites

@YakumoYukari Okay I tried messing around with that and still couldn't get it to function.. I guess it may be better to explain what it is I'm trying to accomplish.  I am going to be making a book for wickerbottom that is called Mind Control and it will make all bunnymen and pigmen become followers when reading the book.  I have the functionality just tied to a global function for now for testing purposes.  Everything is working as I would expect.  However, could you shed some light on what would be the cleanest and best way of going about this?

function G.MindControl()	
	local me = G.AllPlayers[1];
	local x,y,z = me.Transform:GetWorldPosition();
	local toBeControlled = TheSim:FindEntities(x,y,z,30,nil,nil,{"manrabbit","pig"});
	for k,inst in pairs(toBeControlled) do		
		if me.components.minigame_participator == nil then
		inst.components.follower:AddLoyaltyTime(item.components.edible:GetHunger() * TUNING.PIG_LOYALTY_PER_HUNGER)
		inst.components.follower.maxfollowtime =
		if inst.components.sleeper:IsAsleep() then
		inst.components.talker:Say("Your will is my command...")


Link to comment
Share on other sites

You just need to call this function in your onread function for the book-component.

An entity with the reader component reads the book. The reader component has Read(book) called on it, and it calls book.onread(some parameters) on the book. Your book's onread function should be doing this.

Edited by Ultroman
Link to comment
Share on other sites


@Ultroman  I guess perhaps I should have worded that differently.  Let me explain a little further.  I know how to make it a book that isn't my issue.  As you know @Ultroman from seeing some of the other stuff that I do I normally put things into functions etc to test and manipulate them before setting it to its final state.  What I am wanting to know is if I should be cleaning up this code in anyway like making a component etc.. Or maybe perhaps making my own tuning times or ??? Hope that clarifies it some more.

Link to comment
Share on other sites

I wouldn't say so. It's just one function, and you don't need to keep any state, which is one of the big reasons as to why you'd start creating a component. Just make your function a local function in your book prefab LUA, and call it from a function that is hooked up to the onread function.

Concerning the TUNING variables, you might as well just add your own variables to TUNING (or just put the values you want to use directly into the calculation). If any mod changes the TUNING variables for pig loyalty, your mod will also be affected.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...