Jump to content

Class creation


Recommended Posts

Hey :)

I want to create classes to minify the modmain.lua.

modmain.lua

local require	= GLOBAL.require
local Stack		= require("inventory/stack")

AddPlayerPostInit(Stack)

inventory/stack.lua

local stackable_replica		= require "components/stackable_replica"
local size					= GetModConfigData("INVENTORY_MAX")
TUNING.STACK_SIZE_LARGEITEM	= size
TUNING.STACK_SIZE_MEDITEM	= size
TUNING.STACK_SIZE_SMALLITEM	= size 

return Class(function(self)
	function self:Init()
		print("Bag Called")
	
		-- Define functions
		local stackable_replica_ctorBase = stackable_replica._ctor or function() return true end    
		function stackable_replica._ctor(self, instance)
			self.instance		= instance
			self._stacksize		= net_byte(instance.GUID, "stackable._stacksize", "stacksizedirty")
			self._maxsize		= size
		end

		local stackable_replicaSetMaxSize_Base = stackable_replica.SetMaxSize or function() return true end
		function stackable_replica:SetMaxSize(maxsize)
			self._maxsize = size
		end

		local stackable_replicaMaxSize_Base = stackable_replica.MaxSize or function() return true end
		function stackable_replica:MaxSize()
			return self._maxsize
		end
	end	
end)

How i can correctly initialize the class?

The game will be crashed with following exception:

Spoiler

[00:00:17]: Mod: Inventory (Grimm's: Inventory)	Loading modmain.lua	
[00:00:17]: MOD ERROR: Inventory (Grimm's: Inventory): Mod: Inventory (Grimm's: Inventory)	
[00:00:17]: [string "scripts/modutil.lua"]:35: modname must be supplied manually if calling GetModConfigData from outside of modmain or modworldgenmain. Use ModIndex:GetModActualName(fancyname) function [fancyname is name string from modinfo].
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/modutil.lua(35,1) in function 'GetModConfigData'
        ../mods/Inventory/scripts/inventory/stack.lua(2,1) in main chunk
        =[C] in function 'require'
        ../mods/Inventory/modmain.lua(2,1) in main chunk
        =[C] in function 'xpcall'
        scripts/util.lua(630,1) in function 'RunInEnvironment'
        scripts/mods.lua(483,1) in function 'InitializeModMain'
        scripts/mods.lua(457,1) in function 'LoadMods'
        scripts/main.lua(251,1) in function 'ModSafeStartup'
        scripts/main.lua(306,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
        scripts/modindex.lua(76,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(63,1) in function 'BeginStartupSequence'
        scripts/main.lua(305,1) in function 'callback'
        scripts/modindex.lua(516,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(490,1) in function 'Load'
        scripts/main.lua(304,1) in main chunk
[00:00:17]: [string "scripts/mainfunctions.lua"]:963: variable 'global_error_widget' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        scripts/mainfunctions.lua(963,1)
        =[C] in function 'SetPersistentString'
        scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
        scripts/modindex.lua(76,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(63,1) in function 'BeginStartupSequence'
        scripts/main.lua(305,1) in function 'callback'
        scripts/modindex.lua(516,1)
        =[C] in function 'GetPersistentString'
        scripts/modindex.lua(490,1) in function 'Load'
        scripts/main.lua(304,1) in main chunk
[00:00:17]: DoLuaFile Error: (null)

 

 

Link to comment
Share on other sites

as the log says, you can't call GetModConfig outside of modmain.

So instead you could make in your modmain:
GLOBAL.TUNING.GRIMM_NUMBER = GetModConfigData("INVENTORY_MAX")
and then use this new tuning value in your new component.

But I don't get exactly what this has to do with classes ^^

edit:
Ah maybe you mean that "init" function in your class?
Just take a look at other components, all of them use classes.
So I think there is no init function, just put the init stuff under you class definition (like in components).

The main problem in your case is (as you may know, but I would like to summarize it):
How to replace the stackable replica component only for your character...
You can't simply replace the file itself, cause this would also change oter characters or overwrite other mods that change size.
But you also can't use the default way, cause it contains local functions you can't change with addcomponentpostint/addclasspostinit...
Hmmm...
 

Edited by Serpens
Link to comment
Share on other sites

aaaah, you wanted to outsource code, that usually belongs to modmain, in other files :D
I really wasnt able to read this out of your post, cause you asked about classes and gave code that looked like a new component.

Edited by Serpens
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
 Share

×
  • Create New...