Jump to content

Basic Math in a Cartridge


USCcouple

Recommended Posts

Hi,

 

I am trying to perform some basic math operations on some variables. Say I have three variables (Number1, Number2, and Answer). I would like to add Number1 to Number2 and store that in Answer.

 

I would think that I could write:

Answer = Number1 + Number2

 

But the compiler turns that into:

Answer = Answer + Number2

 

So it seems that I have to write:

Answer = Answer + Number1

Answer = Answer + Number2

 

is that correct? is there not a more condensed way of doing the addition?

 

I also need to do division and multiplication. I looked through the lua documentation but could not get any of it to work with the compiler. Is there any documentation on what math can be done?

 

Thanks,

Kevan

Link to comment

I believe the builder does not support adding variables. Instead you could try writng a function to handle your math such as:

 

-- This goes in the main section
Answer = addNumbers(Number1, Number2)

-- This goes in the Author functions section
function addNumbers(num1, num2)
return num1 + num2
end

 

Of course the issue with this is that the builder will more than likely do this:

Answer = "addNumbers(Number1, Number2)"

 

Notice the quotes. You'd have to manage that seperately.

Edited by Tydirium
Link to comment

Also, don't forget to:

 

require "math"

 

in the Author Directives section. This gives you access to the math library.

 

Actually you shouldn't need the require "math" statement for simple math operators. You do need it to call any math.* functions though

Edited by Tydirium
Link to comment

Also, don't forget to:

 

require "math"

 

in the Author Directives section. This gives you access to the math library.

 

Actually you shouldn't need the require "math" statement for simple math operators. You do need it to call any math.* functions though

 

Cool, it works, Thanks!

 

Kevan

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...