Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fschaar · Jul 19, 2012 at 01:37 PM · javascriptstringmath

calculate terms in runtime

Is there an easy way to get the result of a mathematical term in runtime?

For example I randomly genrate a term like 1+3*4=, the player has to type in the correct result an the game says right or wrong. Is there a function like

 result = Calculate(term:string);

Any ideas???

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J Lam · Jul 19, 2012 at 02:17 PM 0
Share

I think there's no predefined function for you, it may need you to code for it. If you generate the terms in your own way, then you should know the result already. If you're reading the term from somewhere else, then you need to parse the string and split the string from * and / first, then + and - and so on.

avatar image fschaar · Jul 19, 2012 at 03:12 PM 0
Share

Well the problem is, it ist a learning game for children, that shall figure out the right order of the numbers in the term. there can be be more then one solution. so i have to check, if what they put together is right or wrong

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Jul 19, 2012 at 03:12 PM

You can use eval to convert a string into code.

 function Start () {
     var a = Random.Range(1, 10);
     var b = Random.Range(1, 10);
     var c = Random.Range(1, 10);
     var operations = "-+*";
     var equation = "" + a + operations[Random.Range(0, operations.Length)]
         + b + operations[Random.Range(0, operations.Length)] + c;
     var x = eval(equation);
     Debug.Log (equation + "=" + x);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fschaar · Jul 20, 2012 at 08:28 AM 0
Share

I want to convert my project later to iphone and android. does eval work ther or do i have to create an own function anyway?

avatar image Eric5h5 · Jul 20, 2012 at 05:39 PM 0
Share

No, eval doesn't work with #pragma strict, so therefore not iOS.

avatar image fschaar · Jul 23, 2012 at 01:21 PM 0
Share

Ah thank you - now I have an idea, what pragma strict means ;) That means I have to write my own function to do the calculation ..

avatar image
0

Answer by Nightwatch · Jul 19, 2012 at 02:53 PM

Yeah you will have to parse it, build up a tree to apply the calculation order etc.

I would say, if you randomly generate it, do not generate it as string in the first pace, but in your own format you can calculate and just present it as string.

Maybe have custom a function RandomEq(...) and get back the result and the string representation in a struct.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Piflik · Jul 19, 2012 at 03:02 PM

Completely random might be difficult/impossible, but you could easily build a function that churns out many different terms...

First I would use a Switch statement (and a random number) to choose the general structure of the term, without fixed numbers, only variables. Then you could assign the variables random numbers, calculate the solution and test it against the player's input.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access a String array in one script from another script 0 Answers

Finding if a specific key contains a specific value in a Hashtable? 1 Answer

I want to have the map of the Hidden Object game, when the player is near the object and it press the letter "Q". The object is revealed to the player. 0 Answers

Enemy AI Error 1 Answer

String split script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges