Any simple way to store what an object must do in its update

Hi !
I’m currently trying to figure how to store the content of an update method for a class. I explain my problem:
I have asteroids turning around a star. I load the position of objects from a json scene for ex.
But how can i save in the json the fact the asteroids must turn around the sun, apart from specifying the center of the orbit, the radius, the speed, around which axis…

How to save the mathematical part of the function ? (the transform matrices, sin cos, etc )
How would I, for example, tell it has to not turn around, but instead use 1/X equation ?

Does this would really need to use a scripting language like LUA ? Am I touching the limits ? I don’t want to have to parse an equation like I would do when you first program a calculator.

I don’t think there’s a way to store mathematical equations in JSON, unless you write your own JSON parser for them. Which is probably more work than what you’d like.

However, what comes to my mind is that you can write the Update method for your object to accept a method as a parameter via delegate, and use that delegate in combination with something like enum to determine how the object will be updated. Basically something like this (note - it might not be syntatically correct):

public void Update(GameTime gameTime, Action updateEquation)
{
    // snip
    updateEquation(parameters);
    // snip
}

and in your main Update loop call it via, for example, asteroid.Update(gameTime, orbitSun);

By specifying the center of the orbit, the radius, the speed, around which axis…

That is how most commercial games do it. Don’t overthink it.

Thanks :slight_smile:
I already use delegates like this for the update. But I’m then necessarily have to use predefined combinations of what can be possible. It would not be “moddable” as it would be in-engine defined.

I’ll stick with this for now if there is no other solution simpler than using external lib
It is not my top priority but I start thinking about it in advance and overthink often :confused:

Im not familiar with json however…

My first thought would be serialization of those objects or of the actual instances that contain them.
This is not always trivial in your case im not sure its even possible when it is sometimes its more of a pain then just writing a parser. If serialization can’t do it though i dunno what can and i suppose then its write your own save load parser.

I tend to think BASIC with code, not always the most efficient but it works and is flexible…

So that being said, perhaps store a symbol in the JSON, say * or / for multiply and divide, then use a switch statement to perform reflective operations within the code, I am sure there are just 1-3 variants right?

Well, hope that helps…