Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to store a math formula?

Discussion in 'Scripting' started by raycosantana, Feb 24, 2016.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hi

    Im trying to create a framework for an turn based J-RPG, Ive been doing some research like looking how RPG maker works and I noticed they store entire formulas with variables an all, how do they do that?

    rpg maker formula.png

    I want my framework to be as generic as possible so I would like to do something similar :rolleyes:

    Thanks in advance! ;)
     
  2. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    Ever heard of compiler design? RPG Maker most likely reads that formula as a string, parses each of those variables, operators, and constants as tokens. They get thrown into some sort of custom compiler such that the formula can be calculated/interpreted and run during gameplay runtime.

    EDIT:
    Here are some examples from that very page:
    http://www.tutorialspoint.com/compiler_design/compiler_design_lexical_analysis.htm
    http://www.tutorialspoint.com/compiler_design/compiler_design_types_of_parsing.htm
     
    raycosantana and landon912 like this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I do something like this in my framework. I wrote a simple arithmetic evaluator for it:
    https://github.com/lordofduct/space...ob/master/SpacepuppyBase/Dynamic/Evaluator.cs

    An object can be passed in as well to pull values from, the syntax explains it. Unfortunately I currently only support one object passed in at a time.

    That just does the calculations. I have various things that support it. You could just pass in strings manually. Or things like my VariantReference explicitly uses it:
    https://github.com/lordofduct/space...lob/master/SpacepuppyBase/VariantReference.cs

    As you can see here:
    VariantReference_02.png

    Unfortunately pulling this out of my framework is not trivial, but you can look it over to get an idea how I accomplish it.

    Also, this runs slower than if it were actual code since it's parsed and interpreted at runtime.
     
    Last edited: Feb 24, 2016
    raycosantana likes this.
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Maybe a bit overkill for what you want to do, but you could integrate Lua within your project.
     
    raycosantana likes this.
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Thanks for the responses but I think writing a compiler is a bit too much for me, I will just writte individual scripts for each spell/ability.