Putting Objects Into Strings

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Putting Objects Into Strings

Post by EliterScripts »

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?
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Putting Objects Into Strings

Post by pgimeno »

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?
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Putting Objects Into Strings

Post by monolifed »

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
User avatar
dusoft
Party member
Posts: 492
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Putting Objects Into Strings

Post by dusoft »

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).
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Putting Objects Into Strings

Post by pgimeno »

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.
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

Re: Putting Objects Into Strings

Post by Darlex »

Easy, Use Sock.LUA ( https://github.com/camchenry/sock.lua ).
You can send (and receive) everything that is inside a variable or a table.
Hi! I wish you have an amazing day!
Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests