Jump to content

Help: getGridSquare() returns nil?


TheNicestGuy

Recommended Posts

I'm working on my first mod, as an experienced programmer but Lua newbie. My first milestone is to have a boat appear in the boathouse. This is my mockup with an awful-looking placeholder boat:

boat1.png

 

So I've got PNGs of the boat chopped up into ten iso squares, each 64x128. Using both the SprayPaint mod by Thuztor and Peanuts and turbotutone's World Object tutorial as a guide, I threw together the following code to try to get one piece of the boat to appear.

gtfo = {};gtfo.loadTextures = function()	getTexture('mods/GTFO/media/textures/boat_n_01.png');endgtfo.spawnVehicles = function()	local cell = getWorld():getCell();	print('cell: ');	print(cell);	local wX, wY = ISCoordConversion.ToWorld(11303, 6587, 0);	wX = math.floor(wX);	wY = math.floor(wY);	print('wX: ');	print(wX);	print('wY: ');	print(wY);	local gridSquare = cell:getGridSquare(wX, wY, 0);	print('gridSquare: ');	print(gridSquare);	local boatTile01 = IsoObject.new(gridSquare, 'mods/GTFO/media/textures/boat_n_01.png', "boat01");	print('boatTile01: ');	print(boatTile01);	gridSquare:AddTileObject(boatTile01);endEvents.OnGameBoot.Add(gtfo.loadTextures);Events.OnGameStart.Add(gtfo.spawnVehicles);

 

The coords 11303 x 6587 are what RoboMat's Coordinate Viewer shows as the mouse coordinates when I click the square inside the boathouse I want to use. When this runs, I get this output:

STATE: enter zombie.gameStates.IngameStatecell:zombie.iso.IsoCell@40ec97c6wX:11640wY:6613gridSquare:nilboatTile01:zombie.iso.IsoObject@6ce06976-------------------------------------------------------------attempted index: AddTileObject of non-table: null-----------------------------------------STACK TRACE-----------------------------------------function: spawnVehicles -- file: gtfo.lua line # 33java.lang.RuntimeException: attempted index: AddTileObject of non-table: null

 

So it seems clear that the problem is that cell:getGridSquare() is returning nil unexpectedly, but I'm little at sea as to why. My main question is obviously if anyone can spot what I'm doing wrong or offer further troubleshooting leads. But since I'm sure this is some rookie mistake because I really don't know what I'm doing yet, here are some other questions whose answers would edify me greatly:

  • What are the various coordinate systems, in which contexts are they used, and how do they differ?
  • What exactly is the IsoCell that getWorld():getCell() returns? It doesn't seem to be any particular piece of the map, so what's its point?

Also one other interesting, relevant point: The World Object tutorial itself does not work. I assume this is just because it got outmoded by a new release, but in any case its error is not quite the same as mine. It seems to be caused by a failure to populate a sprite into an object.

Link to comment
Share on other sites

Hola,

 

first of all, to save you some headaches:

 

I'm working on my first mod, as an experienced programmer but Lua newbie.

 

PZ doesn't use "pure" Lua but rather Kahlua. It also seems like the require statement is broken, so you can't use classic "modules" like you could with normal Lua - There are workarounds though since TurboTuTone managed to finish his Erosion Mod nonetheless :)

 

 

So it seems clear that the problem is that cell:getGridSquare() is returning nil unexpectedly, but I'm little at sea as to why. My main question is obviously if anyone can spot what I'm doing wrong or offer further troubleshooting leads. But since I'm sure this is some rookie mistake because I really don't know what I'm doing yet, here are some other questions whose answers would edify me greatly:

 

I haven't touched any PZ related code in months so this is just a guess. I think the problem is that you access the cell with the absolute coordinates, but you need to access it with the coordinates relative to the cell instead.

 

 

-- If % doesn't work try math.modlocal localX = wX % 300;local localY = wY % 300;
 

This should give you the correct coordinates on the cell:

 

 

local gridSquare = cell:getGridSquare(localX, localY, 0);
 

As I said, it's been a while for me ... so no guarantee ;)

Link to comment
Share on other sites

RoboMat, that did not fix the error, but it does raise questions that will probably get me there. But first, I discovered something fascinating. Having the Coordinate Viewer mod enabled makes the numbers involved very, very different.

With Coordinate Viewer

wX = 11640

wY = 6611

Without Coordinate Viewer, same code, player beginning in the exact same spot

wX = 370

wY = 18

 

Could I possibly be running into some name collision between the mods, or does this mean Coordinate Viewer changes the behaviour of ISCoordConversion.ToWorld()? Anyway, even though the modulo-by-300 of those numbers is quite different, the result is the same: gridSquare is still nil.

 

So your answer implies that an IsoCell is a section of the map which is 300 x 300 IsoGridSquares. If so, then calling getWorld():getCell() during OnGameStart does what, gets the cell that the player is currently in? This wouldn't explain why getGridSquare() still wouldn't work with coordinates that are less than 300, but it does mean I'm probably going about this the wrong way.

 

A later milestone is to randomly choose, from a list of say five or six fixed locations, two or three at the beginning of a game. A boat will appear at each of those. Rather than directly planting boats in OnGameStart, should I be doing the choosing and the math in some one-time event (OnInitWorld, maybe?), storing the results, then testing each time the player enters a new cell (OnPostMapLoad, maybe?) whether that cell contains a boat and plant it at the correct cell-relative coordinates?

Link to comment
Share on other sites

Where did the coordinates come from? The overlay of the coordinate viewer doesn't use the world coordinates, but rather the player's coordinates.

I can't speak for the "on-click"-extension that TurboTuTone wrote. It's possible that something changed in the 1 year since I wrote the mod. Maybe the conversion is done automatically now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...