Generating a random number

FINAL EDIT: You need to change the value XXX in “goldAmountEquation,((generatorLevel*XXX)” to 100. That does the job.

How do you generate a random number between, lets say, 20-200? I know of math.random(20,200) but would that work inside of a .dbr file? Since the language is LUA it should work, right?

To give you a gist of what I’m trying to accomplish: I want the goldgenerator to drop at least XX amount of iron bits. I got that going (goldValueMin, XX,) but seeing the exact XX value everytime I break something, is immersion breaking. Too calculated, no RNGesus. So I want the object to drop at least XX amount of bits + a random XX amount on top of that.
e.g. 350 + math.random(20,200) = 435 Iron Bits
Obviously you would need to assign the “math.random” part a new name but would it work like this?

math.random(20,200) = ranInt
goldValueMin, 350 + ranInt,

I have basic coding knowledge but I’m still stupid… Do I need to create a new math.random function inside the goldgenerator.tpl first and then re-call that function inside the moneygenerator.dbr? If yes (I’m assuming you’d have to do that since a new function inside the .dbr without it being declared in the .tpl would just be ignored) what Type would it be? It should be Equation all things considered. And how would you call-up/use the function since if we look at the Equations for generating gold for multiple players - they calculate everything on the spot (I just noticed: what is generatorlevel? “goldAmountEquation, ((generatorLevel*.5)+15)*numberOfPlayers,” the answer to all my questions?:rolleyes:). What would be the correct syntax? A lot of questions but the ones about syntax and declaring/calling up the function are the most important ones.

I’d really appreciate it if someone could help or at least point to the documentation on those functions maybe x) tyvm

tl;dr: 1.) How do you create a new function in a .tpl
2.) How do you use that function in a .dbr
3.) How do you add/merge the generated number and the pre-set number together?

EDIT 1: Don’t worry, I still didn’t figure it out. But: merging the numbers is useless since IF math.random would work I could just set the values starting at 350 to a max Value of 750. Same result, less code. IF it would work…

Lua script needs to be in lua files in the sources.

To connect a dbr to lua, you need a dbr that has triggers, typically a ScriptEntity type. You do not want/need to be messing with templates at all as a modder.

You need to use a dbr that has a trigger (aka onAddToWorld or onPickup, etc…) and in that trigger space hook that dbr to a global lua function from your source/scripts.

This was not meant to get you all the way there. But just to get you to stop trying to put lua in the dbrs, it will not work. Lua is only used in lua scripts, which can be made accessible from dbrs with specific trigger fields that can link to the lua script.

Looking at what you actually want to do, methinks you should study the existing goldgenerator fields a bit more. Lua should not even be needed for this.

I’m curious, are you just trying to generate more gold, or are you putting your feet in the modding waters? Let us know if you want to swim…or drown:D

I understand. Thanks for stopping me from wasting even more time :v

I’ve found several script triggers by using the DBR Editor on the moneyobject.dbr. I’ll try creating a lua script with the math.random method and hooking it up with the onDrop method. Theoretically speaking when something drops money it should trigger the math.random function and add whatever amount I defined in LUA to the dropped bits, right?

I’ll be trying to figure that out for now :smiley:


Both I guess? Can’t really tell what counts as “modding” :wink: I’m just trying to increase the amount of iron bits. While I succeeded in increasing the dropped amounts of money I guess the real modding comes into play once I start using LUA and script my path to RNGesus… But yes I try to get into modding this game since it’s a lot of fun since everything is so open source :3

I was thinking about that too… That’s all I’ve been thinking about. There’s just 7 lines that matter in the moneygenerator.dbr but I can’t figure it out. Can I change the goldAmountEquation to do what I want?

Coming to think of it that actually sounds right… Knowing me: it isn’t.

I’ll give it a shot and write again once I had my mental breakdown and suffered from burn out :slight_smile:

goldValueMin 350
goldValueMax 550


as mentioned, no it would have to be in a lua script, not the dbr/tpl file

To give you a gist of what I’m trying to accomplish: I want the goldgenerator to drop at least XX amount of iron bits. I got that going (goldValueMin, XX,) but seeing the exact XX value everytime I break something, is immersion breaking. Too calculated, no RNGesus.

RNG determined the exact amount, it just does not force you to keep track of your total to figure out how much you picked up :wink:

I already have that in my code. This:


I just don’t see a lot of alteration. Its usually just 450 Iron Bits (or split up into stacks that add up to 450) lying around. What I want is 463, 564, 704 etc. Or is it already doing that by finding a value between goldValuMin/Max and I’m simply not noticing it?

EDIT: Does the gap need to be smaller?
EDIT2: Nope, smaller gap doesn’t give me values between 450 and 500, just 450.
EDIT3: Okay, now I managed to only get 3500 and I think I know why: the values inside of goldAmountEquation, the numbers that get multiplied with generatorLevel, define the chances of one getting 450 or 3500. Needs further research though. I used values over 100 so that should explain why I was only getting 3.5k drops.
EDIT4: Yay! Success! I have different amounts dropping now. Ranging from 450 to 3500 and everything in between. Kudos jiaco, generatorLevel seemed to be the important part!

Hopefully final post but I’m still up for discussing this:


This did it. I get values ranging from 450 to 3500. 479, 3290, 540 etc. So the important part seems to be generatorLevel*XXX. It seems to define the chances of using amounts higher than the goldValueMin. If set to 100 it will always use the full range. The Addition outside of the bracket is still a mysterium to me…

Thanks everyone!