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?
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.
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 |
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
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
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.
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!
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!
[Please log in to post a comment]