So I had recently got my game to sort-of work with multiplayer. My problem now is that I have Lua objects that need to be communicated to/from the server/client. So, I need to come up with a mechanism that takes the objects needed and turns them into strings, and another for taking strings and putting them into programmatic objects.
I have been piggybacking off of the LOVE physics, which is great. I use a lightly modified version of Hero ( https://github.com/airstruck/hero ) along with JSON ( https://github.com/rxi/json.lua ) to send/receive the physics world.
Since I have been programming for single player, I haven't designed the game objects to be friendly with tostring. Every physical item in the game has one body, one shape, and one fixture, just as an FYI. I used the setUserData/getUserData methods from Body, and either had a nil or table value for all body's userdata values. The problem was that not all the values would tostring, so I had to comment out the userData section of Hero.
My plan is to create a class for the userData values, and create an object specifically for handling userData for each body. My problem is that I am trying to think how to best-possible enforce code that makes sure that all values can be converted into strings. Any thoughts, or do I need to add some clarity to what I'm trying to get across?
Putting Objects Into Strings
Re: Putting Objects Into Strings
The solution seems so simple that I've probably misunderstood something. Since you're creating a class specifically for that, you can add a mandatory parameter to your class creation function: the tostring function to use with the class.
What am I missing?
What am I missing?
Re: Putting Objects Into Strings
https://www.lua.org/manual/5.2/manual.html#pdf-tostring
you need to add __tostring metamethod to your userdata in C
or manually turn it to string in lua without using tostring
you need to add __tostring metamethod to your userdata in C
or manually turn it to string in lua without using tostring
Re: Putting Objects Into Strings
What you need is called serialization. E.g. this library provides it:
https://github.com/bakpakin/binser
Your backend server should take care of deserializing string into objects then (e.g. PHP serialize/unserialize).
https://github.com/bakpakin/binser
Your backend server should take care of deserializing string into objects then (e.g. PHP serialize/unserialize).
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Putting Objects Into Strings
After rereading the post, I think that the question isn't how to make sure that every object contains a tostring method so that all objects can be (manually) serialized, as I understood initially, but how to make sure that you never add a field to any object that can't be serialized (i.e. is userdata or lightuserdata).
If that's the question, all I can think of is a fairly expensive run-time verification in the __newindex metamethod that checks the type of the value written for every field. If it's a userdata, complain. If it's a table, check or set its metatable to ensure it has a __newindex metamethod that also performs the checks. It gets really ugly when the checks need to be recursive.
If that's the question, all I can think of is a fairly expensive run-time verification in the __newindex metamethod that checks the type of the value written for every field. If it's a userdata, complain. If it's a table, check or set its metatable to ensure it has a __newindex metamethod that also performs the checks. It gets really ugly when the checks need to be recursive.
Re: Putting Objects Into Strings
Easy, Use Sock.LUA ( https://github.com/camchenry/sock.lua ).
You can send (and receive) everything that is inside a variable or a table.
You can send (and receive) everything that is inside a variable or a table.
Hi! I wish you have an amazing day!
Who is online
Users browsing this forum: No registered users and 2 guests