Jump to content

How to display a variable?


Firemeboy

Recommended Posts

I'm working on a cartridge where every time a player enters a zone, a variable increases by one. But I'd like the player to be able to see what that number is. Ideally the user would be able to push a button, or do something similar, and then a message would appear that says, "you have (variable) numbers of...X".

 

I can't just put them in the inventory, because they pick up an item, and drop it off somewhere else. But I'd like to keep track of how many times they perform the action.

 

Anybody know of an easy way to do this?

Link to comment

Let me put this into context. Let's say you have a place in your game where players can pick up, for instance, a coin. Players can also pick up the same coin in other areas and you want to tell the player how many coins s/he has.

 

To do this, first, you can move the same coin item around to different areas. When the user picks the coin up, it will display the same "You have x coins" message.

 

Here's an easy way to do this:

 

First, create a variable in the "Variables" section. I will call it "numcoins" for reference later. Make it a number type and start it off at zero.

 

You will have a coin item already created with a "pick up" event, so I won't go into creating that. However, in your "pick up" event, you will show a message to the player. You currently have "You have x coins" typed in there, but it doesn't display a number at the moment. Sound good so far?

 

Now, above the message, add an action. Choose "Increment a variable by a value". Fill in everything to increment "numcoins" by 1. Click "OK" when you're done.

 

The next part gets a little funky at the moment. In fact, Nate could probably provide you with a better alternative. But, for the moment, I can show you what I do.

 

First, save the cartridge. After that, open the .lua file in notepad. Do a search for "You have x coins". You'll see something like this:

Wherigo.MessageBox{Text=[[You have x coins.]],}

 

Change it to read:

Wherigo.MessageBox{Text=[[You have ]] .. numcoins .. [[ coins.]],}

 

Save the text file and re-open your cartridge in the builder.

 

Now, this isn't the best of approaches to use because I've been told the Builder may overwrite this later and not interpret it correctly. In fact, I've seen it to my code before. I'd suggest doing this last, just before you build your cartridge. I tried adding a script and firing that particular message box in the custom author script section, but the Builder wouldn't accept the code. However, it does well with the steps I outlined.

 

Good luck and feel free to ask more questions in the thread. I'm around multiple times a day and so are the Groundspeak folks. You'll get a good answer.

Link to comment

K, that worked like a charm. But I am trying to figure out one more thing.

 

Let's say, in your example, at the end of the game you want to get with another person and show them how many coins you have. You've already clicked through the message, but you want to show them how many coins you got during the game. Is there a way the user can click on a button to pull up how many coins they have? Or should I do an end game function that displays that information?

 

It might be nice to have an in game way to do it, in case they lose track of how many coins they have, but an end game function would work as well.

Link to comment

Why not change the inventory coin's name to indicate the quantity? For instance, when you have one lackey coin, it says "Lackey Coin". But for more coins, it will say "Lackey Coin x3". That might offer the cleanest and least confusing UI for the user. You could also change the coin's description to match the number of coins the player has. Either way.

 

At the end of the game, you can also display a tally. When the player uploads the completed game, I assume you will be able to see as well. I haven't gone that far yet, though.

Link to comment

I had a similar question before. And this is a solution I found so far.

It is very similar to Range Fox's way, and the followings are additional findings.

 

- In the message box in builder. It works the direct typing "[[You have ]] .. numcoins .. [[ coins.]]" (double quotation is not needed) instead of using notepad.

 

- It seems more useful to move the item to player.

For example: In the catridge event "When a cartridge is started", add script "Move item*** to Player".

 

These are method I use to "Yamanote Line Roulette" catridge.

 

I hope it is a additional proper answer for you.

Link to comment

I found and agree to Ranger Fox.

 

It is easy to find for player that the variable is shown in "name" or "description" of the item.

 

I found that the variable is also shown in these field using the same format.

([[You have ]] .. numcoins .. [[ coins.]])

Link to comment

I agree, TOSY. There are many ways to do this, depending on the effect you want and how your game works. The way I posted seemed to be the simplest. However, I'm still looking for someone to suggest a way that doesn't involve opening the script in notepad.

 

As time goes on, there will be more people who will only understand the builder and will be lost if we tell them to open the file in notepad. So finding an answer in the builder would be preferable to doing anything more "advanced" like I suggested.

Link to comment

I got this to work for the message, but when I tried it for an item, it didn't work.

 

For example, at the end of the game, I move an item to the player's inventory. The description for that item says 'you have X coins'. I go into notepad and change it to '[[you have]] .. numcoins .. [[coins]]' but when I try to compile it, it says:

 

attempt to concatenate global 'numcoins' (a nil value)

 

Again, this works when I send a message to a player, but I want the player to be able to check at any time how many coins they have, based on that variable.

 

So I'm a bit lost.

Edited by Firemeboy
Link to comment

The attached code should help you with what you need. Hmm... I can't upload a .lua or .txt file. Instead, I copied and pasted the code to the end of this post.

 

If you run the cartridge in the emulator (after compiling it, of course), you will see one zone south of the player icon. The first time you move the player into the zone, the coin will be placed in the player's inventory. Including the first visit, every time the player enters the zone, the coin count will increment by one and show up nice and neat in the player's inventory. I changed both the coin's name and description in this example. You can do either or both.

 

==============================================================

The code will break if you save it in the builder. If you do, fix it by going to lines 109 and 110 in the source. You will see the following:

 

zitemcoin.Description = You have .. numcoins .. coins

zitemcoin.Name = numcoins .. coins

 

Add the quotes back to make it look like this:

 

zitemcoin.Description = "You have " .. numcoins .. " coins"

zitemcoin.Name = numcoins .. " coins"

 

==============================================================

 

First of all, do you have a variable names "numcoins" declared? If you don't, that's where your problem is--and it's an easy fix. And you did assign a default value to the variable, right?

 

As you can see above, apparently, that part in Lua is using quotes to denote a string instead of the square brackets. That might be the cause of another problem once you get past the variable null value.

 

==============================================================

 

See if this helps. If not, you know where to ask.

 

As for everyone else, including our developers at Groundspeak, how would you rewrite the example so it would not break when saved in the builder application while still producing the same effect? I think the answer to that would be very helpful--especially if this could be accomplished without opening the Lua file in a text editor.

 

==============================================================

==============================================================

==============================================================

==============================================================

 

require "Wherigo"

ZonePoint = Wherigo.ZonePoint

Distance = Wherigo.Distance

Player = Wherigo.Player

 

-- #Author Directives Go Here# --

-- #End Author Directives# --

 

cartnumcoins = Wherigo.ZCartridge()

 

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

cartnumcoins.MsgBoxCBFuncs = {}

 

-- Cartridge Info --

cartnumcoins.Id="0740bc64-26dd-4983-a9c4-da9e45006193"

cartnumcoins.Name="numcoins"

cartnumcoins.Description=[[Demonstrate the numcoins code.

 

Every time you enter the zone, you will add a coin to your inventory. The first time you enter the zone, you will begin with four coins.]]

cartnumcoins.Visible=true

cartnumcoins.Activity="TourGuide"

cartnumcoins.StartingLocationDescription=[[]]

cartnumcoins.StartingLocation = ZonePoint(37,-80,0)

cartnumcoins.Version="0.01"

cartnumcoins.Company="Ranger Fox Adventures, Ltd."

cartnumcoins.Author="Ranger Fox"

cartnumcoins.BuilderVersion="2.0.4715.3441"

cartnumcoins.CreateDate="1/18/2008 12:54:38 PM"

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

cartnumcoins.UpdateDate="1/18/2008 1:06:18 PM"

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

cartnumcoins.TargetDevice="PocketPC"

cartnumcoins.TargetDeviceVersion="0"

cartnumcoins.StateId="1"

cartnumcoins.CountryId="2"

cartnumcoins.Complete=false

cartnumcoins.UseLogging=false

 

-- Zones --

zoneAddcoincount = Wherigo.Zone(cartnumcoins)

zoneAddcoincount.Id="6e7d9dd5-329b-42b8-a466-496c554c2886"

zoneAddcoincount.Name="Add coin count"

zoneAddcoincount.Description=[[When you enter this zone, you add a coin to your inventory.]]

zoneAddcoincount.Visible=true

zoneAddcoincount.DistanceRange = Distance(1500, "feet")

zoneAddcoincount.ShowObjects="OnEnter"

zoneAddcoincount.ProximityRange = Distance(200, "feet")

zoneAddcoincount.AllowSetPositionTo=false

zoneAddcoincount.Active=true

zoneAddcoincount.Points = {

ZonePoint(36.99981,-80.00034,0),

ZonePoint(36.99975,-79.998,0),

ZonePoint(36.99911,-79.99792,0),

ZonePoint(36.99908,-80.00034,0)

}

zoneAddcoincount.OriginalPoint = ZonePoint(36.99981,-80.00034,0)

zoneAddcoincount.DistanceRangeUOM = "Feet"

zoneAddcoincount.ProximityRangeUOM = "Feet"

zoneAddcoincount.OutOfRangeName = ""

zoneAddcoincount.InRangeName = ""

 

-- Characters --

 

-- Items --

zitemcoin = Wherigo.ZItem(cartnumcoins)

zitemcoin.Id="4501462b-9431-428c-8d29-bde68faeae31"

zitemcoin.Name="coin"

zitemcoin.Description=[[]]

zitemcoin.Visible=true

zitemcoin.ObjectLocation = Wherigo.INVALID_ZONEPOINT

zitemcoin.Locked = false

zitemcoin.Opened = false

zitemcoin.Commands = {

}

 

-- Tasks --

 

-- Cartridge Variables --

numcoins = 3

cartnumcoins.ZVariables = {numcoins = 3}

 

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

buildervar = {}

buildervar.numcoins = {}

buildervar.numcoins.Id ="818f7ce7-d9b5-48b3-bcf3-96b61bb0e71e"

buildervar.numcoins.Name = "numcoins"

buildervar.numcoins.Type = "Number"

buildervar.numcoins.Data=[[3]]

buildervar.numcoins.Description=[[]]

 

-- ZTimers --

 

-- Inputs --

 

--

-- Events/Conditions/Actions --

--

 

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

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

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

 

function zoneAddcoincount:OnEnter()

-- #GroupDescription=enterZone --

-- #Comment=enterZone Comment --

numcoins = numcoins + 1

zitemcoin:MoveTo(Player)

zitemcoin.Description = "You have " .. numcoins .. " coins"

zitemcoin.Name = numcoins .. " coins"

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

-- #End Author Functions# --

-- Nothing after this line --

return cartnumcoins

Edited by Ranger Fox
Link to comment

Hello,

Very nice work. The way to concatenate in lua is the "..". But, when you concatenate something like "You have", you have to use the [[ ]] brackets. It's probably a good general rule to use the brackets, you could do it with quotes, but, the builder encloses things like this with brackets, that way, you can event have line feeds embedded. So, the best way to represent your code is:

 

zitemcoin.Description = [[You have]] .. numcoins .. [[coins]]

zitemcoin.Name = numcoins .. [[coins]]

 

This can be entered in the builder this way and I've opened it, saved it and reopened and it is preserved.

 

Hope this helps.

 

David.

Link to comment

Thank you both, Ranger Fox and David. I had it working another way, but it wasn't pleasant at all. You had to click several times to see the number of 'coins'. But I followed Ranger's advice, and got it working on the emulator, and then got a lot of errors when I tried to upload it. But after following David's suggestion, it uploaded fine, and is now working.

 

Again, thanks!

Link to comment

I'm posting the same code, but with David's changes to it. This way others will save on the copy and paste and edit. I was confused when I opened the Lua script and saw quotes instead of the square brackets, which led to my use of quotes instead of brackets. It's good to know.

 

==============================================================

==============================================================

==============================================================

 

require "Wherigo"

ZonePoint = Wherigo.ZonePoint

Distance = Wherigo.Distance

Player = Wherigo.Player

 

-- #Author Directives Go Here# --

-- #End Author Directives# --

 

cartnumcoins = Wherigo.ZCartridge()

 

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

cartnumcoins.MsgBoxCBFuncs = {}

 

-- Cartridge Info --

cartnumcoins.Id="0740bc64-26dd-4983-a9c4-da9e45006193"

cartnumcoins.Name="numcoins"

cartnumcoins.Description=[[Demonstrate the numcoins code.

 

Every time you enter the zone, you will add a coin to your inventory. The first time you enter the zone, you will begin with four coins.]]

cartnumcoins.Visible=true

cartnumcoins.Activity="TourGuide"

cartnumcoins.StartingLocationDescription=[[]]

cartnumcoins.StartingLocation = ZonePoint(37,-80,0)

cartnumcoins.Version="0.01"

cartnumcoins.Company="Ranger Fox Adventures, Ltd."

cartnumcoins.Author="Ranger Fox"

cartnumcoins.BuilderVersion="2.0.4715.3441"

cartnumcoins.CreateDate="1/18/2008 12:54:38 PM"

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

cartnumcoins.UpdateDate="1/18/2008 1:06:18 PM"

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

cartnumcoins.TargetDevice="PocketPC"

cartnumcoins.TargetDeviceVersion="0"

cartnumcoins.StateId="1"

cartnumcoins.CountryId="2"

cartnumcoins.Complete=false

cartnumcoins.UseLogging=false

 

-- Zones --

zoneAddcoincount = Wherigo.Zone(cartnumcoins)

zoneAddcoincount.Id="6e7d9dd5-329b-42b8-a466-496c554c2886"

zoneAddcoincount.Name="Add coin count"

zoneAddcoincount.Description=[[When you enter this zone, you add a coin to your inventory.]]

zoneAddcoincount.Visible=true

zoneAddcoincount.DistanceRange = Distance(1500, "feet")

zoneAddcoincount.ShowObjects="OnEnter"

zoneAddcoincount.ProximityRange = Distance(200, "feet")

zoneAddcoincount.AllowSetPositionTo=false

zoneAddcoincount.Active=true

zoneAddcoincount.Points = {

ZonePoint(36.99981,-80.00034,0),

ZonePoint(36.99975,-79.998,0),

ZonePoint(36.99911,-79.99792,0),

ZonePoint(36.99908,-80.00034,0)

}

zoneAddcoincount.OriginalPoint = ZonePoint(36.99981,-80.00034,0)

zoneAddcoincount.DistanceRangeUOM = "Feet"

zoneAddcoincount.ProximityRangeUOM = "Feet"

zoneAddcoincount.OutOfRangeName = ""

zoneAddcoincount.InRangeName = ""

 

-- Characters --

 

-- Items --

zitemcoin = Wherigo.ZItem(cartnumcoins)

zitemcoin.Id="4501462b-9431-428c-8d29-bde68faeae31"

zitemcoin.Name="coin"

zitemcoin.Description=[[]]

zitemcoin.Visible=true

zitemcoin.ObjectLocation = Wherigo.INVALID_ZONEPOINT

zitemcoin.Locked = false

zitemcoin.Opened = false

zitemcoin.Commands = {

}

 

-- Tasks --

 

-- Cartridge Variables --

numcoins = 3

cartnumcoins.ZVariables = {numcoins = 3}

 

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

buildervar = {}

buildervar.numcoins = {}

buildervar.numcoins.Id ="818f7ce7-d9b5-48b3-bcf3-96b61bb0e71e"

buildervar.numcoins.Name = "numcoins"

buildervar.numcoins.Type = "Number"

buildervar.numcoins.Data=[[3]]

buildervar.numcoins.Description=[[]]

 

-- ZTimers --

 

-- Inputs --

 

--

-- Events/Conditions/Actions --

--

 

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

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

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

 

function zoneAddcoincount:OnEnter()

-- #GroupDescription=enterZone --

-- #Comment=enterZone Comment --

numcoins = numcoins + 1

zitemcoin:MoveTo(Player)

zitemcoin.Description = [[You have ]] .. numcoins .. [[ coins]]

zitemcoin.Name = numcoins .. [[ coins]]

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

-- #End Author Functions# --

-- Nothing after this line --

return cartnumcoins

Link to comment

attempt to concatenate global 'numcoins' (a nil value)

 

These are some tips for this topic. Because I had a same error message when I built my cartridge.

The error seems not to find the variable.

 

- When the variable is written in "description", it may be error.

For examble, when you put "[[You have ]] .. numcoins .. [[ coins]]" in Description in item property, you find the same error. Maybe because the item definition is earlier than variable definition in Lua file.

It is OK, when the Description is defined in script.

 

- Typo even capital and small letter cause error.

When you write "[[You have ]] .. Numcoins .. [[ coins]]", it cause error.

 

I made these error many times....

Link to comment

- Typo even capital and small letter cause error.

When you write "[[You have ]] .. Numcoins .. [[ coins]]", it cause error.

 

Yes, it has been reported that Lua is case sensitive. Please do be careful. The trick is to find a way to name everything that works for you. If it makes more sense, just use all lowercase.

 

If you have any more questions, TOSY, you know where to ask them. There are a lot of people around who are eager to help.

Link to comment

- Typo even capital and small letter cause error.

When you write "[[You have ]] .. Numcoins .. [[ coins]]", it cause error.

 

Yes, it has been reported that Lua is case sensitive. Please do be careful. The trick is to find a way to name everything that works for you. If it makes more sense, just use all lowercase.

 

If you have any more questions, TOSY, you know where to ask them. There are a lot of people around who are eager to help.

 

Thanks everyone for the great work and comments. Here is a link to the Lua Language online:

 

Programming in Lua

 

David

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