Log In  

Hi everyone.

Playing around with the new mouse control feature of PICO8 I got the idea of a "1869" style game.

Remember "1869" on Amiga? It's a trading game. You ship around and buy and sell goods.

This is a very early stage but somehow I got stuck on my way using tables.

Perhaps someone can give me a clue?

Cart #anno1869-0 | 2021-03-03 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

P#88482 2021-03-03 20:57

The game runs, so can you say what is the problem?

P#88485 2021-03-03 21:13

Click on a country. Then click on "verkauf" or "Einkauf".
Repeat that with different countries.
At one game will break with an error.

P#88492 2021-03-03 21:48 ( Edited 2021-03-03 21:49)

You're defining goods in two places, and the number in both playes is different - at one "H" "Holz" is missing. (Keep definitions like these in one place and make them unique.)

At the moment in the tables you are using numeric keys (1: Code="W", Name="Weizen"; ...; 5: Code="H", Name="Holz"). My knowledge of Lua is rudimentary, but tables in Lua are associative. This means, you can use the "Code" directly as the key ("W": Name="Weizen", ..., "H": Name="Holz"); in for-loops you will then have to iterate with the pairs()-function (instead of all()) and functions like find_goods() aren't necessary.

P#88511 2021-03-04 03:11 ( Edited 2021-03-04 03:31)

Agreed that using codes as table keys instead of numbers would make things easier to use and remove the need for the find_* functions! Here’s an example:

goods={
 holz={name="Holz", cost=10},
 weizen={name="Weizen", cost=20},
}

countries={
 fr={name="Frankreich", color=1, goods={"holz"}},
 de={name="Deutschland", color=9, goods={"weizen", "holz"}},
}

--gc means code for good
for gc in all(countries.fr.goods) do
 local good=goods[gc]
 print(good.name..": "..good.cost)
end
P#88538 2021-03-04 17:11

merwok, thank you very much! These tables in lua are very flexible in their use, I am still not getting used to it completly. Your way - thx for the exampe - seems a good approach. I will change code and report here ;-)

P#88539 2021-03-04 17:51

can I replace a part of the table pointer with a variable?
like this:

a="fr"
for gc in all(countries.a.goods) do
local good=goods[gc]
print(good.name..": "..good.cost)
end

this example doesn't work.

P#88547 2021-03-04 20:09
1

Yes but in this way:

countries.fr  -- shortcut for line below
coutries["fr"]

a="fr"
countries[a]  -- key is "fr"!

goods[1]  -- number indices cannot be written things.1
b=2
goods[b]  -- key is 2
P#88549 2021-03-04 21:22 ( Edited 2021-06-21 23:38)

Hi! I am struggling again ...

How to iterate through the table for countries to compare mouse coordinates with the map boxes?
I tried like that but it doesn't seem to work, as well as there comes no error...

instead of using the key (as country["fr"]) I can not use numbers (country[1] as country["fr"]), did I understand that right?

country={
 fr={name="frankreich",code="fr",verkauf=create_goods(),einkauf=create_goods(),box={20,74,47,102}},
 uk={name="england",code="uk",verkauf=create_goods(),einkauf=create_goods(),box={19,45,36,73}}
}

ct=0
for c in all(country) do
 ct+=1
 if (mx>=c[ct].box[1] and my>=c[ct].box[2]) and (mx<=c[ct].box[3] and my<=c[ct].box[4]) then 
  if (mb) then  
   player.ziel=c
  else
   msg={}
  end
 end
end

goal shoud be that if the coordinates match, the destination country is put to player.ziel

P#88616 2021-03-06 17:21 ( Edited 2021-03-06 17:26)

Ah there’s a slight issue! Your code doesn’t work because your data needs to be accessed like 'country["fr"]', not 'country[1]'!

Lua tables serve two different use cases: key-value mappings, like 'france={code="fr",col="blue"}', but also sequences, where keys are numbers, like 'countries={france,germany}'.

A for loop like 'for i=1,#countries' will give you a number 1, 2, 3, etc., that you can use like 'countries[i]' to get the item. 'all' makes this more convenient: 'for c in all(countries)' gives you the first thing in the table, then the second thing.

But 'all' doesn’t work for your country table, because it’s not a sequence table but a key-value table! So you need to use pairs:

for code,c in pairs(country) do
 -- c is now a country table, you can check c.name or c.box etc
end

Read this: https://pico-8.fandom.com/wiki/Lua#for_loops

Also good to keep handy: https://www.lexaloffle.com/pico-8.php?page=manual

P#88619 2021-03-06 18:21

Oh, merwok, you enlighten me ;-) No I understand the difference between pairs() and all()! thank you!
Somehow I didn't get that by reading the manual for pico.

P#88620 2021-03-06 18:44

So many thanks to your hints I got it working.
Using tabels in this way is kind of cool now I understand them a bit more.
And I translated the game to english ;-)
Ok, "the game" means this alpha version. It still needs a lot of game logic but I wanted to do the basic things first: map navigation, sell and buy functions.

Cart #fukikehake-0 | 2021-03-08 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#88680 2021-03-08 17:29

Looking forward to when it is finished!

P#88758 2021-03-09 23:06

Cart #hehhosuzo-0 | 2021-03-10 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA


Thanks to your help I got my tables now working and I think I understand the background of tables in lua now much better.
So here is the prototype, map navigation and trade menus are working fine.
Try and click around, suggestions are welcome!

P#88798 2021-03-10 17:55 ( Edited 2021-03-10 17:56)

So here is the next step of my trading game "Anno 1869"
You can play around and buy or sell goods. So the basic game function work ;-)
(Not all, new goods you buy are not yet added to your kontor, but that will happen next release.
Next step is some more game play...

Try it out!

Cart #anno1869-1 | 2021-03-25 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#89563 2021-03-25 22:20 ( Edited 2021-03-25 22:21)

Very nice! Great job! Will the prices be influenced by the buying/selling of goods? Or just random fluctuation?

P#89596 2021-03-26 07:21

I am not decided about the prices calculation.
There is a lot of game logic to be done:
Riots, expand kontor capacities, jobs to be done...

P#89600 2021-03-26 07:51

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 13:27:03 | 0.030s | Q:42