Hi everyone. I’m working on a small grid based combat system and am coding the player movement. The code works, as in, I can move the player. Except after half a dozen moves or so (the number changes) the player starts jumping around into different parts of the grid randomly with each move. I don’t understand why.
I’ll try to only post the code that seems relevant though it might be a lot.
First I make the grid in StoryInit:
<<for _y = 0; _y < $gridSize[1]; _y++>>
<<for _x = 0; _x < $gridSize[0]; _x++>>
<<set $grid["" + _x + "," + _y] = {pos: [_x,_y], type: "floor", status: [], contains: ""}>>
<</for>>
<</for>>
<<set $grid["1,1"].contains = "Player">>
All good. I couldn’t figure out a better way to do the object creation because setting the $grid[objects] as just numbers without quotes made some issues.
Then the grid gets displayed in the “Battle” passage, calling a widget repeatedly.
<<for _y = 0; _y < $gridSize[1]; _y++>>\
<tr>\
<<for _x = 0; _x < $gridSize[0]; _x++>>\
<td><<DisplayGrid _x _y>></td>\
<</for>>\
</tr>\
<</for>>\
<<widget "DisplayGrid">>\
<<set _cell = "" + _args[0] + "," + _args[1]>>\
DisplayGrid sets the _cell temp var which I pass along to other widgets in this process.
Later in DisplayGrid these get called in each cell:
<td><<GetMove _cell "up">></td>\
<td></td></tr>\
<tr><td><<GetMove _cell "left">></td>\
<td><<GetMove _cell "right">></td>\
</tr><tr><td>\
<td><<GetMove _cell "down">></td>\
<td></td></tr>\
And I’ll put the whole GetMove code in because I think this must be where something bad is happening.
<<widget "GetMove">>\
<<set _cell = _args[0]>>\
<<capture _cell>>\
<<if $grid[_cell].contains == "Player">>\
<<if _args[1] == "up">>\
<<link ⬆️ $passage>><<Move "Player" "up" _cell>><</link>>\
<<elseif _args[1] == "right">>\
<<link ➡️ $passage>><<Move "Player" "right" _cell>><</link>>\
<<elseif _args[1] == "left">>\
<<link ⬅️ $passage>><<Move "Player" "left" _cell>><</link>>\
<<elseif _args[1] == "down">>\
<<link ⬇️ $passage>><<Move "Player" "down" _cell>><</link>>\
<</if>>\
<</if>>\
<</capture>>\
<</widget>>\
The $passage link refreshes the page.
Finally, the Move widget.
<<widget "Move">>\
<<set _pos = $grid[_args[2]].pos>>\
<<if _args[1] == "up">>\
<<set $grid[_args[2]].contains = "">>\
<<set _pos[1] -= 1>>\
<<set _cell = "" + _pos[0] + "," + _pos[1]>>\
<<set $grid[_cell].contains = "Player">>\
<<elseif _args[1] == "right">>\
<<set $grid[_args[2]].contains = "">>\
<<set _pos[0] += 1>>\
<<set _cell = "" + _pos[0] + "," + _pos[1]>>\
<<set $grid[_cell].contains = "Player">>\
<<elseif _args[1] == "left">>\
<<set $grid[_args[2]].contains = "">>\
<<set _pos[0] -= 1>>\
<<set _cell = "" + _pos[0] + "," + _pos[1]>>\
<<set $grid[_cell].contains = "Player">>\
<<elseif _args[1] == "down">>\
<<set $grid[_args[2]].contains = "">>\
<<set _pos[1] += 1>>\
<<set _cell = "" + _pos[0] + "," + _pos[1]>>\
<<set $grid[_cell].contains = "Player">>\
<</if>>\
<</widget>>\
So, the player moves from cell to cell with each arrow pressed, but then begins to jump around, not move at all, or skip cells.
I separated the process into individual widgets for my own sake because I like having everything segmented like that, but could that be what’s messing with it? I tried changing _cell to _cell2 in the Move widget in case the temps having the same name was messing with it somehow, but that didn’t fix it. Also, if you see a better way to do any of my other code I’m all for it.
Also, something else I was wondering since I have a thread. If you make a lot of objects in sugarcube, does that slow things down? Is there a way to clear some objects completely, like, after a combat? I generate new enemies and want to remove them after each combat.