Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an OnSave() #28

Closed
UltraJohn opened this issue Oct 17, 2017 · 8 comments
Closed

Add an OnSave() #28

UltraJohn opened this issue Oct 17, 2017 · 8 comments
Milestone

Comments

@UltraJohn
Copy link
Contributor

Would be nice if we could have an OnSave() that runs before the game saves. I'm having problems with items not saving the correct location using the GameHook many mods use to save custom items, normal items seem to not always save properly. You know what i mean? :D

@piotrulos
Copy link
Owner

This should be possible to add, with gamehook there is a problem because onload is called too early and sometimes it didn't hook or game overwrite hook. That's why playmaker is hell for modders.
My cassette mod use trick with "late onload"
Something like this

public override void OnLoad()
{
    (...)
    new Thread(LateLoad).Start();
}
void LateLoad()
{
    Thread.Sleep(5 * 1000); //Wait 5 seconds
    GameHook.InjectStateHook(GameObject.Find("ITEMS"), "Save game", Save);
}

@UltraJohn
Copy link
Contributor Author

I tried that and it does not solve my issue :/

The gamehook injects properly to saving but some objects like the ratchet set and car parts save too early i think. when i run my save function i can see all objects are teleported to the correct location at the player, but as soon as the game goes to main menu and i load my save, the objects are back at the wrong location. So i think what i need is an OnSave that runs before the game saves so that i can set the location before the game tries to save.

@piotrulos
Copy link
Owner

I don't know if it is possible to hook before main save, by hooking you just add functions which save function call, but this call will always be after main function for saving.

Best way is to use your own save file to store correct position and load this file back from onload, and less chance to corrupt main save accidentally

@UltraJohn
Copy link
Contributor Author

The way msc saves items is very weird, so im not sure how i would make it work. I would have to make a copy of the gameobject variables and then delete from the world to prevent it saving in items.txt and then storing the whole gameobject in my own save data? Not sure if it's possible to do that. Right now I'm just moving objects out of map and removing physics so it stays in position, and when you save it sets the position back to player so that it saves to that location, but yeah some objects stay out of map and become lost :/

@piotrulos
Copy link
Owner

piotrulos commented Oct 18, 2017

Only world position you need to save, (x y z) and then load position from file and while loading just transform objects to loaded values. I give some example tomorrow

@UltraJohn
Copy link
Contributor Author

UltraJohn commented Oct 18, 2017

Not so easy i think because of the way msc saves items. If i want to load an item i need to restore all values of the gameobject, not just instantiate a new at the location, otherwise it will forget the values.

items

Update: Ok I think objects that have (Clone) in the name are being handled differently in the game code, so they dont save to items.txt, so most of them must be saving location before my code runs first which is why only some objects dont save properly.

Update2: I found a working solution (so far lol)
work

@piotrulos
Copy link
Owner

Maybe do something like this, you transform them to specifed (x y z) coordinations? And that is saved instead of going back to previous position. So in onload search for any objects in this wrong position and then transform them back to normal position.

http://answers.unity3d.com/questions/383671/find-gameobject-at-position.html

@UltraJohn
Copy link
Contributor Author

Yeah exactly what I did, i normally have items at location (0,100000,0) and on save i move objects to player, but some objects save too early so they appear at (0,100000,0) after loading, so I just check for those objects and relocate to player :D

@piotrulos piotrulos added this to the v0.4 milestone Dec 9, 2017
piotrulos added a commit that referenced this issue Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants