Jump to content

Recommended Posts

Hello, I need some help :)!

So, my character has a bunch of different things that use "inst.blahblah = true" to change his mode, I was wondering would it be possible to save these "inst.? = true" stuff when my character disconnects from the server or goes in a cave? Then he doesn't lose her modes?

Can someone maybe show me how I would do this :)? It'd really be a great help because I have no idea how I would do something like this...

Thanks so much for reading my problem, have a wonderful day/night :D!

Edited by SuperDavid

Easiest question. :) 

AddPrefabPostInit("your_character",function(inst)

	inst.blahblah = 123
	inst.blahblah2 = 456

	local old_OnSave = inst.OnSave
	local old_OnLoad = inst.OnLoad
	inst.OnSave = function(inst, data)
		data.blahblah = inst.blahblah
		data.blahblah2 = inst.blahblah2
		return old_OnSave(inst, data)
	end
	inst.OnLoad = function(inst, data)
		if data ~= nil then
			inst.blahblah = data.blahblah
			inst.blahblah2 = data.blahblah2
		end
		return old_OnLoad(inst, data)
	end
end)

http://pastebin.com/96AaqMSt

(I didn't test the code so be careful with mistypes)

Edited by Maris

@Maris I put this code in modmain.lua

Spoiler

AddPrefabPostInit("adam",function(inst)

	inst.gelid_mode = 123
	inst.flawless_mode = 456

	local old_OnSave = inst.OnSave
	local old_OnLoad = inst.OnLoad
	inst.OnSave = function(inst, data)
		if inst.hallow_anim ~= nil then
			data.gelid_mode = inst.gelid_mode
			data.flawless_mode = inst.flawless_mode
		end
		return old_OnSave(inst, data)
	end
	inst.OnLoad = function(inst, data)
		if data ~= nil then
			inst.gelid_mode = data.gelid_mode
			inst.flawless_mode = data.flawless_mode
		end
		return old_OnLoad(inst, data)
	end
end)

 

and it gave me this crash

Crash 1.jpg

Also, would it be possible to save this code with this function too? Like

" inst.components.transformermode:GetModeName() == "valkyrie_wendy_mode" "?

Thanks so much for your help Maris :D!

Edited by SuperDavid

Note that if you want use your loaded variables immediately, you should do it in NEXT TICK because order of code is following:

  1. Main function of prefab
  2. All AddPrefabPostInit s
  3. OnLoad ( + all OnLoad hooks if exist)

You can do it by this code:



AddPrefabPostInit("your_character",function(inst)
	print(inst.blahblah) -- Always nil or initial value!
	
	-----> do something here after value is initialized but not loaded yet
	
	inst:DoTaskInTime(0,function(inst) --next tick. 0 means the time.
		print(inst.blahblah) -- Just loaded value OR initial value if prefab is created.
		
		-----> do something here after value is loaded
		
	end)
end

http://pastebin.com/7gYbkTpg

Edited by Maris
8 minutes ago, Maris said:

See scorched_skeleton.lua as an example with OnSave and OnLoad functions.

I checked scorched_skeleton.lua & this is what I saw

Spoiler

local function onsave(inst, data)
    data.anim = inst.animnum
end

local function onload(inst, data)
    if data ~= nil then
        if data.anim ~= nil then
            inst.animnum = data.anim
            inst.AnimState:PlayAnimation("idle"..inst.animnum)
        end
    end
end

so, I wanted to try saving "inst.gelid_mode = true" & load it into the world I tried this

Spoiler

local function onsave_mode(inst, data)
    data_gelid = inst.gelid_mode
end

local function onload_mode(inst, data)
    if data ~= nil then
        if data_gelid ~= nil then
            inst.gelid = data_gelid
            inst.gelid_mode = true
        end
    end
end

masterpostintit
inst.OnLoad = onload_mode
    inst.OnSave = onsave_mode

and nothing happened? I have no idea why it didn't work?

If you use your custom prefab (not player prefab) you can use

inst.OnSave = on_save --your custom function
inst.OnLoad = on_load --your custom function

If you use your custom player prefab, there should be existing OnSave and OnLoad. It's not good to overwrite them, so you should save old functions:

local old_OnSave = inst.OnSave
inst.OnSave = on_save --Error!

But your on_save function should call old function OnSave i.e. old_OnSave. It does not see it until it's local function. So you should define it AFTER local variable:

local old_OnSave = inst.OnSave
inst.OnSave = function(inst,data)
   --do something
   return old_OnSave(inst,data)
end

Finally, if you are trying to patch game prefab without OnSave and OnLoad functions, you should check whether other mods already added these functions. If so, you shouldn't break them:

local old_OnSave = inst.OnSave
inst.OnSave = function(inst,data)
   --do something
   if old_OnSave ~= nil then -- only if old function exists!
      return old_OnSave(inst,data)
   end
end

P.S. Adding save info to the whole world is little bit different. The game doesn't check OnSave/OnLoad for TheWorld, but you could hook its components, e.g. TheWorld.state. Remember that components have different OnSave and OnLoad functions compared with prefabs.

P.P.S. There was ninja edit on first messages. You are too fast on testing the code :)  But I'm not sure why you crashed. OnSave/OnLoad definitely must exist for any player. "adam" is the player prefab? 

Adam is my character prefab & inside the adam.lua I have onload but I don't have a onsave function maybe that's the problem I don't know?

local function onbecamehuman(inst)
if not inst.components.sanity:IsSane() then
inst.AnimState:SetBuild("insane_adam")
end
inst:DoTaskInTime(1, function() inst.components.talker:Say(GetString(inst, "ANNOUNCE_BEGIN_WORLD")) end)
end

local function onload(inst)
inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
inst:ListenForEvent("ms_becameghost", onbecameghost)
if inst:HasTag("playerghost") then
onbecameghost(inst)
else
onbecamehuman(inst)
end
end

masterpostinit
inst.OnLoad = onload

And this code you gave me to save stuff it crashes the game saying "attempt to call upvalue 'old_OnSave' a nil value" I don't understand what that means... I'm sorry i'm really dumb..

Spoiler

AddPrefabPostInit("adam",function(inst)

	inst.blahblah = 123
	inst.blahblah2 = 456

	local old_OnSave = inst.OnSave
	local old_OnLoad = inst.OnLoad
	inst.OnSave = function(inst, data)
		if inst.hallow_anim ~= nil then
			data.blahblah = inst.blahblah
			data.blahblah2 = inst.blahblah2
		end
		return old_OnSave(inst, data)
	end
	inst.OnLoad = function(inst, data)
		if data ~= nil then
			inst.blahblah = data.blahblah
			inst.blahblah2 = data.blahblah2
		end
		return old_OnLoad(inst, data)
	end
end)

 

20 minutes ago, Maris said:

If you use your custom player prefab, there should be existing OnSave and OnLoad. It's not good to overwrite them, so you should save old functions:

Does this mean I can't have "inst.OnLoad = OnLoad_InsaneContainer" & "inst.OnLoad = onload" in my masterpostinit & I have to merge them or something? Because they both work fine in game & aren't conflicting with each other.

 

The only thing I wanna do is for example if my character has "inst.gelid_mode = true" & he leaves the server or goes in a cave I just want the game to remember that "inst.gelid_mode" was true then I can activate some code to set his stats & stuff to make him be in that mode, but I have no idea how to do that & lots of the stuff you told me I don't understand because i'm stupid :|...

I'm sorry if you don't want to help me that's fine I will just give up then because it seems really complicated to save something :(...

Edited by SuperDavid
48 minutes ago, SuperDavid said:

I have to merge them or something?

You have to merge them.

 

48 minutes ago, SuperDavid said:

it seems really complicated to save something

It's not so complicated. If you know lua ofc :) 

But it's complicated to debug the mod via forum. You get the crash so it's better if you will interpret it. It will be much more faster. And for doing this you need to know lua well enough. So you will know answers on most specific questions for your mod.

Well, okay, not my style but you can send the mod and I'll check & debug it.

 

Edited by Maris
17 hours ago, Serpens said:

why aren't you using a component?
I made you such a nice "mode" component, where you can store and save/load all your mode stuff, but you aren't using it? =( =P

Serpens I feel like it would take me to long to port my current modes that don't have a unique crafting recipe to the component modes (& i'm scared that I do something wrong & ruin everything), but the modes that have unique crafting recipe I will definitely have to use your component to set them!

Maris I'm sorry but I keep having error " There was a problem processing the uploaded file. -200 " when trying to upload my mod & my mod's anims are over 8mb, do you want me to keep trying to upload?

 

Edited by SuperDavid
17 minutes ago, Maris said:

Is there a topic on the forum with the full description of idea of your mod? 

I don't understand what "full description of idea of your mod" means, i'm so sorry but can you say it more simpler if even possible :)? I'm super sorry for being so daft :wilson_facepalm:...

If what you mean is if I have ever uploaded my mod on the Klei Forums? Because I never did. (And I don't think it's possible since the Max file size for uploads is 2MB..)

If what you mean is if there's any topic on my mod character? There isn't.. If you want me to do one you can tell me how, i'm sorry for being annoying..

If you need me to do something just tell me, i'm sorry i'm not very smart...

Edited by SuperDavid
10 hours ago, SuperDavid said:

I don't understand what "full description of idea of your mod" means, i'm so sorry but can you say it more simpler if even possible :)? I'm super sorry for being so daft :wilson_facepalm:...

If what you mean is if I have ever uploaded my mod on the Klei Forums? Because I never did. (And I don't think it's possible since the Max file size for uploads is 2MB..)

If what you mean is if there's any topic on my mod character? There isn't.. If you want me to do one you can tell me how, i'm sorry for being annoying..

If you need me to do something just tell me, i'm sorry i'm not very smart...

@SuperDavid I think he wants to know all the features of your character, so he can imagine the result you want to achieve. And then maybe he can give you code that would better fit to what you want to achieve in total.
@Marisfrom what I got from his threads so far, he is making a character that can transform in alot of different modes. Eg. a mode where he gots a free backpack, or a mode for every builder Tag (so he can craft eg. the stuff from maxwell) and so on.
And instead of using one single "mode" component, he saves the mode in inst instead. So inst.beabermode = true and so on.

I made a component for the modes and added a check for them into the builder class, so when the component has a specific mode, then he is able to build eg maxwell stuff. 
I think the easiest way would be to simply expand this component also for all other modes:

 

14 hours ago, Serpens said:

@SuperDavid I think he wants to know all the features of your character, so he can imagine the result you want to achieve. And then maybe he can give you code that would better fit to what you want to achieve in total.

All of the things I want to achieve for my character? Because I have over several things I want to achieve & I don't want to be a bother to anyone :(...

@Maris You want to know more about my character? Okay, here's a very brief explanation!

So, my character's called "Adam, The Ne Plus Ultra" & he's a very extreme character that I want to make feel very unique compared to all other mod characters, one of his greatest attributes is being able to transform into different modes.

These are all the modes. And most of the modes can stack with each other, each mode makes it that it requires a different style of play!

Normal Adam
Insane Adam
Cat Adam              = inst.cat_mode
Yogi Adam             = inst.yogi_mode
Gelid Adam            = inst.gelid_mode
Flawless Adam         = inst.flawless_mode
Overcharged Adam      = inst.static_mode
Ninja Adam            = inst.components.transformermode:GetModeName() == "ninja_mode"
Valkyrie Adam         = inst.components.transformermode:GetModeName() == "wathgrithr_mode"
Ghostly Adam          = inst.components.transformermode:GetModeName() == "wendy_mode"
Masked Adam           = inst.components.transformermode:GetModeName() == "masked_mode"
Lord Adam             = inst.components.transformermode:GetModeName() == "lord_mode"

I have a couple more but these are the important ones.

My character has much more than 1 perk & 2 cons like most characters, he has a bunch of perks & cons & listing them all would take up too much space & I don't want to scare anyone away ;)...

So, my character's supposed to be half human & half alien with some artifact of extreme power embedded in him, and with those qualities my character can have any type of perks & cons! I started working on my character 7 months ago & i'm still not 50% finished, hahaha!

If there's anything else you want to know just tell me, i'm sorry if this isn't what you wanted to know :|...

Edited by SuperDavid
4 hours ago, Maris said:

Mod is too complicated, sorry. It's difficult to read the code without indents.

@Maris You mean you can't help without the entire mod? Well here's the entire mod in working condition

https://dropfile.to/sbMnmrN

All I need help with is just find a way to save & load inst.?_mode = true then activate some code but if you don't want to help at all that's okay :)... Sorry, for bothering you...

Edited by SuperDavid

if you dont have an existing OnSave which I think you don't, try removing the 2 lines i marked

local old_OnSave = inst.OnSave --remove this line
	inst.OnSave = function(inst, data)
		if inst.hallow_anim ~= nil then
			data.gelid_mode = inst.gelid_mode
			data.flawless_mode = inst.flawless_mode
		end
		return old_OnSave(inst, data) --and this line
	end

--or 

local old_OnSave = inst.OnSave
	inst.OnSave = function(inst, data)
		if inst.hallow_anim ~= nil then
			data.gelid_mode = inst.gelid_mode
			data.flawless_mode = inst.flawless_mode
		end
		if old_OnSave ~= nil then
			return old_OnSave(inst, data) --and this line
		end
	end

 

Edited by Aquaterion
9 hours ago, SuperDavid said:

@Maris You mean you can't help without the entire mod? Well here's the entire mod in working condition

https://dropfile.to/sbMnmrN

All I need help with is just find a way to save & load inst.?_mode = true then activate some code but if you don't want to help at all that's okay :)... Sorry, for bothering you...

just use the component... there you already have Save and Load functions... and for a simple ==true, you even don't need a to add anything to the component, cause you can simply check the mode name, which is already saved and loaded.
And if you want to save more, just add it to the save/load function from the component...

@Serpens I had no idea the component you made me is that awesome, I tested it & it really does work :D!!! Serpens I'm going to replace all my inst.mode with your component instead, thanks so much for the great component ;)!!

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...