Using math.random with another variable instead of a number

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
FlaconiaUnited
Posts: 14
Joined: Fri Oct 02, 2020 2:07 pm

Using math.random with another variable instead of a number

Post by FlaconiaUnited »

Hey guys. We are trying to make a randomized character creator, but running into some problems. It is set up as follows.

We begin with a starting variable: StatBudget, which begins at 30. The aim is to have 3 stats that all together add up to 30.

STR is generated using math.random(1,20).
StatBudget is then set to StatBudget - STR.
WIS should be generated by picking a random number between 1 and StatBudget.
StatBudget is then set to StatBudget - WIS.
CHA will then be set to the remaining StatBudget number.

However, our code for generating WIS is not working. This is what we have currently in the script field:

Variable["Wisdom"] = math.random(1, (Variable["StatBudget"]));

Is it possible to use math.random in this way, by replacing a number with another variable? Hopefully we are just doing it wrong. :D
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using math.random with another variable instead of a number

Post by Tony Li »

Hi,

Yes, that's fine to do.

You may find it helpful to temporarily add a Lua Console to your scene so you can type in those Lua statements manually to make sure your syntax is correct. For example, I just typed this in to test:

Code: Select all

Variable["StatBudget"] = 20
Variable["Wisdom"] = math.random(1, Variable["StatBudget"])
return Variable["Wisdom"]
return Variable["StatBudget"]
I typed each line individually. Some don't return any output, but the last two [that start with 'return'] return reasonable values. I also entered just "return Variable" to print out the values of all variables.
Post Reply