Jump to content

Creating a Random Outcome


mm_dancer

Recommended Posts

I would like to create a dice in my game, and wonder how I would go about creating an event that randomly chooses the value of the dice roll, I know its been done in other games, but I'm just starting with the whole Wherigo building!

 

Thanks for you time!

Link to comment

Some of the other developers, more experienced than I, will probably chime in here shortly.

 

I am sure there must be a random number generating function in the Lua language. There is no prewritten capability in the Builder. So you will need to do it in Author Script and call it from the program.

Link to comment

Some of the other developers, more experienced than I, will probably chime in here shortly.

 

I am sure there must be a random number generating function in the Lua language. There is no prewritten capability in the Builder. So you will need to do it in Author Script and call it from the program.

 

roll=math.random(6)

 

is the thing your after, but Tequila is correct, you will need to use Author Script and may be a little advanced if your only just starting to create Wherigos, and you don't have any computer programming knowledge.

 

If you do want to give it a go, its a function call rather than an event and can be called anywhere. The trouble is if you use it, then the function its in will no longer be editable in the builder.

 

Perhaps if you can give me an example of when you want to generate a random number, I can suggest minimising the effect on your Wherigo. I will probably suggest some sort of messagebox is displayed and when you press the OK button, the value is stored in a variable. Then you can use it when ever you want.

 

Oops, nearly forgot to say, computers arn't random, you will always get the same random numbers using the above, you need to initialise the randomness by using something like:

 

math.randomseed( os.time() )

 

although for testing, having a repeatable sequence of random numbers can be very useful.

Link to comment

Hi thanks for your responses. What I want to create is a Wherigo based around a board game style game. I have zones mapped out for the squares of the board.

I do have a little programming knowledge, at school I've done a little java, php, html, flash. not sure if lua is like any of those. Making this Wherigo is actually the final project I chose for my computers class at school, so I dont mind doing some raw coding, is there a tutorial for it?

So what I would like to happen is when the player rolls the dice a number is generated. Then I can call that number to add to the numbered space they are on to activate the proper zones for them to travel to next. There will be tasks or questions at the squares that determine if the player moves backwards or forwards, so if they get the question right, they move forwards, if they get it wrong they move backwards.

Link to comment

Hi thanks for your responses. What I want to create is a Wherigo based around a board game style game. I have zones mapped out for the squares of the board.

I do have a little programming knowledge, at school I've done a little java, php, html, flash. not sure if lua is like any of those. Making this Wherigo is actually the final project I chose for my computers class at school, so I dont mind doing some raw coding, is there a tutorial for it?

So what I would like to happen is when the player rolls the dice a number is generated. Then I can call that number to add to the numbered space they are on to activate the proper zones for them to travel to next. There will be tasks or questions at the squares that determine if the player moves backwards or forwards, so if they get the question right, they move forwards, if they get it wrong they move backwards.

 

Not having done any of those languages I'm not sure how similar they are, but generally a programing language is a programming language, each one has slighly different syntax and benifits for using it, so you should be fine. So Author script it is :mad:

 

Like I said earlier, you need to initialise the randomness, so in the example below, I've put that in the OK button for the Welcome message.

 

For generating the random number, I've put that in a custom button for the Dice object and then display the results in a message box so that you can see the result.

 

The example shows a number of techniques including putting numeric values into a builder variable, accessing a builder variable and putting it a message box and of course the random stuff.

 

Have fun, and make sure you know what and why I've done what I've done incase you get asked for your assignment :huh:

 

 

:blink::huh::D

require "Wherigo"

ZonePoint = Wherigo.ZonePoint

Distance = Wherigo.Distance

Player = Wherigo.Player

 

-- #Author Directives Go Here# --

-- #End Author Directives# --

 

carttest = Wherigo.ZCartridge()

 

-- MessageBox Callback Functions Table used by the Builder --

carttest.MsgBoxCBFuncs = {}

 

-- Cartridge Info --

carttest.Id="5e372135-660b-476a-9264-9cc703e89b4e"

carttest.Name="test"

carttest.Description=[[]]

carttest.Visible=true

carttest.Activity="TourGuide"

carttest.StartingLocationDescription=[[]]

carttest.StartingLocation = ZonePoint(52,-2,0)

carttest.Version=""

carttest.Company=""

carttest.Author=""

carttest.BuilderVersion="2.0.5129.5086"

carttest.CreateDate="5/13/2009 6:03:55 PM"

carttest.PublishDate="1/1/0001 12:00:00 AM"

carttest.UpdateDate="5/13/2009 6:23:17 PM"

carttest.LastPlayedDate="1/1/0001 12:00:00 AM"

carttest.TargetDevice="PocketPC"

carttest.TargetDeviceVersion="0"

carttest.StateId="1"

carttest.CountryId="2"

carttest.Complete=false

carttest.UseLogging=false

 

-- Zones --

zoneAZone = Wherigo.Zone(carttest)

zoneAZone.Id="8fe2e2d9-e550-482d-951f-e33e988da2a3"

zoneAZone.Name="AZone"

zoneAZone.Description=[[]]

zoneAZone.Visible=true

zoneAZone.DistanceRange = Distance(-1, "feet")

zoneAZone.ShowObjects="Always"

zoneAZone.ProximityRange = Distance(-1, "feet")

zoneAZone.AllowSetPositionTo=false

zoneAZone.Active=true

zoneAZone.Points = {

ZonePoint(52,-2.00001,0),

ZonePoint(52,-1.99999,0),

ZonePoint(52,-1.99999,0),

ZonePoint(52,-2.00001,0)

}

zoneAZone.OriginalPoint = ZonePoint(52,-2,0)

zoneAZone.DistanceRangeUOM = "Feet"

zoneAZone.ProximityRangeUOM = "Feet"

zoneAZone.OutOfRangeName = ""

zoneAZone.InRangeName = ""

 

-- Characters --

 

-- Items --

zitemDice = Wherigo.ZItem{Cartridge=carttest, Container=zoneAZone}

zitemDice.Id="9ef7982b-1af2-462a-be17-8aaa25f19f58"

zitemDice.Name="Dice"

zitemDice.Description=[[]]

zitemDice.Visible=true

zitemDice.ObjectLocation = ZonePoint(51,-2.00000000181663,360)

zitemDice.Locked = false

zitemDice.Opened = false

zitemDice.Commands = {

RollDice = Wherigo.ZCommand{Text="Roll Dice", CmdWith=false, Enabled=true, EmptyTargetListText="Nothing available"},

}

zitemDice.Commands.RollDice.Custom = true

zitemDice.Commands.RollDice.Id="8b1214bd-2711-4ffc-bf7d-0784e8c1b19b"

zitemDice.Commands.RollDice.WorksWithAll = true

 

-- Tasks --

 

-- Cartridge Variables --

DiceValue = 0

carttest.ZVariables = {DiceValue = 0}

 

-- Builder Variables (to be read by the builder only) --

buildervar = {}

buildervar.DiceValue = {}

buildervar.DiceValue.Id ="c644c588-6676-48b5-97aa-6da4914713d2"

buildervar.DiceValue.Name = "DiceValue"

buildervar.DiceValue.Type = "Number"

buildervar.DiceValue.Data=[[0]]

buildervar.DiceValue.Description=[[]]

 

-- ZTimers --

 

-- Inputs --

 

--

-- Events/Conditions/Actions --

--

 

-------------------------------------------------------------------------------

------Builder Generated functions, Do not Edit, this will be overwritten------

-------------------------------------------------------------------------------

 

function carttest:OnStart()

-- #GroupDescription=Welcome and initialisation --

-- #Comment=Welcome and initialisation Comment --

Wherigo.MessageBox{Text=[[Welcome to the .... game]],Buttons={"OK",},Callback=carttest.MsgBoxCBFuncs.MsgBoxCB1}

end

 

function zitemDice:OnRollDice()

-- #GroupDescription=Roll that dice --

-- #Comment=Roll that dice Comment --

buildervar.DiceValue.Data = tostring(math.random(6))

Wherigo.MessageBox{Text=[[You rolled ]]..buildervar.DiceValue.Data,}

end

------End Builder Generated functions, Do not Edit, this will be overwritten------

-------------------------------------------------------------------------------

------Builder Generated callbacks, Do not Edit, this will be overwritten------

-------------------------------------------------------------------------------

--#LASTCALLBACKKEY=0#--

------End Builder Generated callbacks, Do not Edit, this will be overwritten------

-- #Author Functions Go Here# --

carttest.MsgBoxCBFuncs.MsgBoxCB1 = function(action)

if action ~= nil then

-- #GroupDescription=initialise stuff here --

-- #Comment=initialise stuff here Comment --

math.randomseed( os.time() )

end

 

end

-- #End Author Functions# --

-- Nothing after this line --

return carttest

;):):)

Link to comment

If I remember correctly, you'll have to require math at the top of your author functions.

 

Once you have a few programming languages under your belt, it gets easier to learn another because you already know how to write your thoughts in code.

 

I'd advise you to take a look at some of the open source Wherigo cartridges. To find out if a cartridge is open source, go to the cartridge's listing page on the Wherigo.com site. If you see the Creative Commons logo and a link to download the source on the left side, it's open source. You can learn a lot about Wherigo and how things work by looking at open source cartridges and playing them through in the emulator.

 

Good luck on your project. Feel free to ask the forum for any clarifications you might need.

Link to comment

If I remember correctly, you'll have to require math at the top of your author functions.

 

Once you have a few programming languages under your belt, it gets easier to learn another because you already know how to write your thoughts in code.

 

I'd advise you to take a look at some of the open source Wherigo cartridges. To find out if a cartridge is open source, go to the cartridge's listing page on the Wherigo.com site. If you see the Creative Commons logo and a link to download the source on the left side, it's open source. You can learn a lot about Wherigo and how things work by looking at open source cartridges and playing them through in the emulator.

 

Good luck on your project. Feel free to ask the forum for any clarifications you might need.

 

I thought I better test it before posting the code, and it does work, so I'm guessing somehow all the standard libraries like the maths one get included. Thats not to say the standard libraries will work correctly on all machines, for instance the IO (input output) library for file access does not work on Garmins, but does work in the emulator.

 

True about programming languages, the slowest bit is looking up the syntax for the language you happend to be using at the moment. I kind of like Lua - easy going sort of language, although thats not always a good thing when your trying to work out whats gone wrong.

 

Ranger Fox's Wack-a-lacky was the main one I looked through and adapted ideas - contains a bit of most things. Ranger Fox, is there a list anywhere of downloadable cartridges? Its always good to see what and how other people have done things and find out querks of the language.

Link to comment

Great! I created the lua file and it worked perfectly. So now how do I incorporate it into the cartridge I have already started? Also is there a spot in the code to assign media to each different roll, I noticed that was done in the slides and ladders one.

Link to comment

Great! I created the lua file and it worked perfectly. So now how do I incorporate it into the cartridge I have already started? Also is there a spot in the code to assign media to each different roll, I noticed that was done in the slides and ladders one.

 

The simplest way to have a different picture for each roll is to have an IF statement and do a different message box for each roll. That way you can skip the bit about displaying variables in the messagebox. All that can be done in the normal designer.

 

As for integrating, If I where you, I would design your existing cartridge to do everything you want, just with the two lines (initialisation one and the actual doing the rolling one) missed out. Initially use the builder to set the dice roll to a specific value e.g. 3 at the point you would want to calculate the random value.

 

Once that is working, you will require 2 Notepads and cutting and pasting. Look at the two files (example and your lua file) side by side, you should see the structure of them relatively easily and spot where to cut and paste. In your file its the bit wher the variable is set to the dice roll number e.g. 3 in the example above.

 

A word of warning BACKUP. Especially when your using Notepad to do the changes, but its a very good thing to do anyway. I always write to a new .lua file so that when (and I do mean WHEN) things go wrong, you can go back to your backup and try again.

Link to comment

ok so i am trying to add the media, and I was trying to copy the other if statement.

so i found this code:

function zitemDice:OnRollDice()

-- #GroupDescription=Roll that dice --

-- #Comment=Roll that dice Comment --

buildervar.DiceValue.Data = tostring(math.random(6))

Wherigo.MessageBox{Text=[[You rolled ]]..buildervar.DiceValue.Data,}

end

 

and then the code for my correct answer is like this:

function zinputQuestion1:OnGetInput(input)

Question1Answer = input

-- #GroupDescription=Script --

-- #Comment=Script Comment --

if Wherigo.NoCaseEquals(Question1Answer,"David Ulmer") then

Wherigo.MessageBox{Text=[[Congrats that is correct, you advance to space 4.]],Buttons={"Continue",},Callback=carttest.MsgBoxCBFuncs.MsgBoxCB1}

zone4.Visible = true

zone4.Active = true

zone1.Visible = false

zone1.Active = false

else

Wherigo.MessageBox{Text=[[sorry that is incorrect, you must remain on this space until your next turn]],Buttons={"Ok",},Callback=carttest.MsgBoxCBFuncs.MsgBoxCB2}

end

 

so i thought that if i added this line:

if Wherigo.NoCaseEquals(Question1Answer,"David Ulmer") then

like this:

if Wherigo.NoCaseEquals(buildervar.DiceValue.Data,"1") then

 

but the program doesnt seem to like that?

Link to comment

ok so with a little farting around with the code I sucessfully put the dice into my game, now i need to be able to get the value that the dice rolled and add it to the square that the player is on.

 

The builder can do quite a lot of stuff, its not all bad. Use it as a teaching tool. What your asking for, the builder can do about 99% of the job. Write what you want in the builder and at the points it can't do stuff like set the random value, use a fixed value. Then use notepad to modify the fixed value for the random value.

 

So, taking what you are asking for and reword it slighlty

set the variable to the square the player is on

add a number (increment is the technical term) e.g. 3

save the project and make sure its working

 

in notepad, change the 3 to: math.random(6)

Link to comment

ok so i am trying to add the media, and I was trying to copy the other if statement.

so i found this code:

function zitemDice:OnRollDice()

-- #GroupDescription=Roll that dice --

-- #Comment=Roll that dice Comment --

buildervar.DiceValue.Data = tostring(math.random(6))

Wherigo.MessageBox{Text=[[You rolled ]]..buildervar.DiceValue.Data,}

end

 

and then the code for my correct answer is like this:

function zinputQuestion1:OnGetInput(input)

Question1Answer = input

-- #GroupDescription=Script --

-- #Comment=Script Comment --

if Wherigo.NoCaseEquals(Question1Answer,"David Ulmer") then

Wherigo.MessageBox{Text=[[Congrats that is correct, you advance to space 4.]],Buttons={"Continue",},Callback=carttest.MsgBoxCBFuncs.MsgBoxCB1}

zone4.Visible = true

zone4.Active = true

zone1.Visible = false

zone1.Active = false

else

Wherigo.MessageBox{Text=[[sorry that is incorrect, you must remain on this space until your next turn]],Buttons={"Ok",},Callback=carttest.MsgBoxCBFuncs.MsgBoxCB2}

end

 

so i thought that if i added this line:

if Wherigo.NoCaseEquals(Question1Answer,"David Ulmer") then

like this:

if Wherigo.NoCaseEquals(buildervar.DiceValue.Data,"1") then

 

but the program doesnt seem to like that?

 

NoCaseEquals I think is for comparing text values, although not 100% sure - need to do some experimentation to check. In my original value I stored the number as text and not a proper number, which is why your having trouble with it. Having it text was more convenient for the message box, but perhaps not such a good thing for you since you want to perform maths on it.

 

If you followed my previous comment, the variable is hopefully staying as a number and not text, so you should have more luck with it. Once you have got my last comment working were you set the value of the variable then increment its value by the random number, use the normal builder to construct the If statement. In the lua file, it should look something like

 

if newValue == 1 then

 

Hope I'm not confusing you too much :unsure:

Link to comment
so i thought that if i added this line:

if Wherigo.NoCaseEquals(Question1Answer,"David Ulmer") then

like this:

if Wherigo.NoCaseEquals(buildervar.DiceValue.Data,"1") then

 

but the program doesnt seem to like that?

Since the value you are comparing it with is not going to contain letters (or, if it does, the case of those letters is not your biggest problem), you can make it a simple comparison with the "==" operator.

 

However, there's a more fundamental issue. When using the Groundspeak builder, the Lua file "declares" (sort of) each variable twice, once as a "buildervar", so that the builder can make sense of it, and once as a cartridge variable, which is the value used at runtime. (In fact there's a third copy, in the ZVariables object; I'm not sure what this is used for.) So the value of buildervar.DiceValue.Data is not relevant. Your runtime code (executed in event handler functions, etc) should refer to plain "DiceValue". So your comparison is simply

if DiceValue == "1" then

You could simplify further by not using tostring() when calling math.random(). Then your variable would contain a number:

DiceValue = math.random(6)

and your comparison would be

if DiceValue == 1 then

 

The creation of the "builder" and "cartridge" flavour of the variables is handled for you by the builder.

 

By the way, if you are going to change the cartridge code in this way, then as soon as you include the call to math.random(), all bets are off as to whether the builder will be able to read in your Lua file. So while developing, as a_snail said, use a number like 3, and edit it "math.random(6)" later. Or, use a special number like 9999, and write a little editor macro which will take your Lua file "carttest.lua" and copy it to "compilethis.lua" while replacing 9999 with "math.random(6)", and so on for other expressions which you might need to tweak outside the builder. That way you don't have to edit your "master" Lua file. (Or use Earwigo, which allows you to integrate arbitrary Lua statements at any point.)

 

On a slightly brighter note, however, you don't have to worry too much about the "type" of a (cartridge) variable. The builder makes you choose between Number, String, and Flag, but Lua itself does not have typed variables, so these distinctions are only used to limit the options which the builder offers you when allowing you to assign a value to a variable within the builder itself.

Link to comment

Yes, sticking the math.random(6) does have some unwelcome effects on the builder, although generally it displays OK, just editing the function in the builder interface crashes it unless you put the function in the Author code section.

 

Once the function is working and your not going to edit it again, then your OK to put in the math.random(6) hence getting it all working with a fixed value first.

 

Sounds like another good reason to swap over to Earwigo :unsure:

 

With a bit of luck however, you should have ended up with something like:

DiceValue = 4
DiceValue = DiceValue + math.random(6)
if   DiceValue == 10 then
 --do something here
end

Link to comment

wow i cant believe how helpful you are! it really is making building this cartridge much more fun, usually i read your comment and go... what, then with a little figuring i can make it work.

so what i did was used a mock example to see if it would work. first i had that when player enters zone 1 set variable 'Space Number' to 1. then when roll occurs increment Space Number by 3. Then if Space Number equals 3 set zone 15 to visible and active (i know i know 1+3 doesnt = 15 :laughing: )

so will i have to do for each sqaure and if statement for each of the 6 values it could be from the roll? or is there a get function that can get the variable for me?

Link to comment

so i played around some more with the if statements and added one for each of the first 6 squares to see if its working, so i have a start square (which isnt square one) and when the player enters that the space number is set to 0, then when the dice is rolled the space number is incrimented by math.random(6), and then i set 6 if then statements, eg if space value = 1 set zone 1 to visible and active. this all works, but the space that is set to active and visible is one higher than what the dice is rolled, so i have no idea what is going on.

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...