20-09-2026, 01:24 PM
Has anyone got an example script to save and load the position of ropes?
|
Help Rope Save Load
|
|
20-09-2026, 01:24 PM
Has anyone got an example script to save and load the position of ropes?
20-09-2026, 04:47 PM
(20-09-2026, 01:24 PM)mechabit Wrote: Has anyone got an example script to save and load the position of ropes?I'm going to join in on this thread as I will be exploring ways to save and load rope state as well. I don't immediately see anything in the docs that would help. I need to explore the API and code more to get a feel for what might be possible, and what could help. Just talking strategy, I envision several possibilities for this. 1) Full state save. Attempting to save the full rope state so that on load it can be precisely reconstructed. This could be difficult and costly and take a lot of time to develop, as I imagine the guts of Obi gets pretty complex. 2) Partial state save. Saving just critical data that allows for partial rope reconstruction. This would be a subset of option 1 that restores basic rope configuration but would allow for some variance. This could be a lot easier than 1 but might still be a big challenge. 3) Pre-configured prefabs. If 1 and 2 aren't possible or are too difficult, my next idea would be to see if you can save several prefab ropes in logical configurations, then on save/load, you restore the prefab that most closely matches what was going on in the scene. So for example, the scene I am currently playing with, I have a power cord(rope) on a roof, you then push it through a hole in the roof and it falls down through the hole, then from inside the building you can plug it in. In my case, I imagine saving 3 rope prefabs -- the original initial rope, a version where it has fallen through the roof, and a version where it's plugged in. It won't exactly match, but it covers the 3 common states. But I don't even know yet if I can do rope prefabs, so even this is just an idea that I don't know if it will work yet or not. The Obi folks might provide some guidance. Whether they do or not, when I figure something out, I'll share what I've learned.
21-09-2026, 07:24 AM
(This post was last modified: 21-09-2026, 07:37 AM by josemendez.)
(20-09-2026, 01:24 PM)mechabit Wrote: Has anyone got an example script to save and load the position of ropes? You can use the particles API. It allows you to read/write any per-particle properties, including positions. It's just a for loop: Code: for (int i = 0; i < actor.solverIndices.count; ++i)If you have ropes that move around and simply want to restore their position/shape at a given point in time, this is enough. Note however that merely saving/loading particle positions won't be enough if your ropes can be torn/cut/resized dynamically at runtime, since the amount of particles, particle indices and their constraint connectivity changes. If the rope has pooled particles, you may or may not want to save the pool of inactive particles as well. Also note that things like cursors, attachments, or element state won't be saved along with particle positions, you may or may not need to save these. All these details can considerably complicate saving/loading (the "full state" save described by Skotty above).
Thanks for the reply, it works great.
When is it safe to load saved data? I've tried setting rope positions using OnBlueprintLoaded event but this only works half the time. I'm guessing some kind of race condition. What is the correct time to load saved data? I've added a flag for blueprint loaded and OnSimulationEnd to wait for 1 simulation step after blueprint load and that seems to work, just wondering if there was a less clunky method. EDIT: actually it's better but still not 100%
21-09-2026, 09:32 AM
(21-09-2026, 08:17 AM)mechabit Wrote: Thanks for the reply, it works great. Depends on what you want to do exactly. If you want to load data mid-simulation, OnSimulationStart should work. This happens every step right before copying CPU data to the GPU. If you're looking for loading data *only* once at the start, upon a blueprint being loaded, you have two options: - instantiate and modify the blueprint itself instead of modifying solver data. - modify solver data right after the blueprint has been loaded, OnBlueprintLoaded should work reliably for this. Quote:I've added a flag for blueprint loaded and OnSimulationEnd to wait for 1 simulation step after blueprint load and that seems to work, just wondering if there was a less clunky method. This should also work reliably. Could you be more specific about what's not working? do particle positions not accurately reflect what you're loading? Note that all data in Obi is expressed in the solver's local space. Also note that velocities may cause the particles to move right after loading positions, if you expect the rope to start out with zero inertia after loading you might want to also set the appropriate entries in solver.velocities to zero.
21-09-2026, 01:09 PM
Using the code I described in the editor, some of the time when I press play the rope moves to the saved position and some of the time when I press play it returns to the initial state.
I've got fast enter play mode on. I just need a way to save the scene state and load it next time a player continues a save.
21-09-2026, 04:11 PM
(21-09-2026, 01:09 PM)mechabit Wrote: Using the code I described in the editor, some of the time when I press play the rope moves to the saved position and some of the time when I press play it returns to the initial state. I’ve been unable to reproduce this, doing what I described above works 100% of the time for me. Would it be possible for you to share your code?
21-09-2026, 05:35 PM
Ah, after digging around it looks like PlayerPrefs was the issue
I'll try again with a different saving method (21-09-2026, 07:24 AM)josemendez Wrote: You can use the particles API. It allows you to read/write any per-particle properties, including positions. It's just a for loop:I just tested out saving ropes with this approach and seems to be working fine so far using the OnBlueprintLoaded event for restore timing. Thanks. I'm not doing any tear/resize. I did also have to save/restore the rigidbody GameObject state that I had attached at the end in my case. But it plugged into my existing persistence architecture pretty seamlessly. I thought this was going to be the hard part, but I struggled less with this than some of the other things I'm trying to do.
22-09-2026, 07:11 AM
I've switch to json saving and it works fine now.
Another question: I see the ropes have a sleep threshold but no sleep event. Saving OnDisable doesn't work. Currently I'm just saving every couple seconds. What's the best way to know if a rope is sleeping? |
|
« Next Oldest | Next Newest »
|