Losing variable in functions

Hello, i have a cycle for with variable ‘lv’ this variable is used to call a function like this

this.createMap(lv)

createMap: function(lv) { this.generateMap(lv); }

generateMap: function(lv) { do something with lv}

but the value was lost so i thought of insert inside the for cycle this.lv=lv and use this.lv inside other functions as well…with no results neither if i use var self=this; and self.lv

can someone point me in the right direction? https://playcanvas.com/editor/project/352037

the problem show off when you enter the church in level.js script

The only way lv might not be defined, is that you call this method somewhere else, or redefine lv just before passing it through.

Best way to find out, is to use Breakpoints. Open Dev Tools, in Sources hit Ctrl + O and find by name your script file. Then find first line of generateMap function body, and click on line number to set breakpoint, then go to church ( :smiley: ). It will trigger this method, and on right hand side you will see call stack, you can inspect this callstack and see the flow of execution, as well as hover on variables to see their values.

Here is good manual how to do this: https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints

I have done all that @max but still missing some point, today will make new tries…hope will be successfull

hi again, seems all variables all lost when reach this point

           var mapCallback = function(x, y, value) {
                if (value) { return; }

                var key = x+","+y;
                this.map[this.lv][key] = this.keyFloor;
                this.freeCells[this.lv].push(key);
            };

In the Chrome tools, enabling this box:

This makes the debugger stop where an exception is thrown by the code (e.g when trying to call a function on an undefined object) so you can check values and the callstack at the point of the crash.

From here, I saw this.freecells is virtually empty.

Now the next step is to work out where this.freecells gets populated.

So somewhere from this code onwards, this.freecells becomes empty.

Stepping through the code a few lines from here, you should be able to see where. I leave that as an exercise for you.

Yes @yaustar yesterday i reached that point too…but i had to stop my research because of some family issues. Tomorrow i will look into it again hoping to find the culprit. When you have eliminated the impossible, whatever remains, however improbable, must be the truth. lol

Ok seems i found where the freeCells got erased, now i have another error but i think is a concept error due to the change in creation system. Will try to adjust it.

Got the code working, not the game, i have no errors but the player fall endless down lol …will have to check all the code again… i think that will make the 50th time in a week

Lost in code…the floor don’t want to appear…while everything else is fine…sigh

You need to be patient! When you are inexperienced it’s very likely that you will get lost in your own code at some point… With practice and experience you will gradually be able to organize your code better and separate it in meaningful parts.

For now try to understand why something is happening first without necessarily looking at the code. If the player falls it means there is nothing for the player to collide underneath. I haven’t looked at your code but try to figure out why the floor isn’t there first.

Hi @vaios i know i need more patience,also because i’m changing a code that isn’t developed by me, so i am trying to bend it to my needs, i may have found the bad knot, still investigating…it’s frustrating sometime to code, but never nerve craking as my actual work :slight_smile: I will FIGHT!!! Lol

The problem seems here

if(existingFloor.length > 0){
                    existingFloor[0].setPosition(x, 0, y);
                    existingFloor[0].enabled = false;
                    existingFloor[0].rigidbody.syncEntityToBody();
                    this.game.floorLocations[this.lv][x+','+y] = existingFloor[0];
                    existingFloor[0].script.Floor.setMat();
                    existingFloor.shift();
                }
                else {
                    var floor = floorTemplate.clone();
                    floor.setName('Floor');
                    floor.setPosition(x, 0, y);
                    floor.rigidbody.syncEntityToBody();
                    app.root.addChild(floor);
                    floor.enabled = false;
                    this.game.floorLocations[this.lv][x+','+y] = floor;
                }

at this line this.game.floorLocations[this.lv][x+’,’+y] = existingFloor[0]; the floorLocations don’t get valorized.
https://playcanvas.com/editor/code/352037/Level.js lines 408 and follows.

Seems that part works now, but still no result…this has became the biggest headache

Everything works, walls, doors, stairs…but the floor still missing…so frustrating

Is existingFloor[0].enabled = false; meant to actually be false?

Yes coz it’s enabled later

if(!this.game.floorLocations[this.lv][visibleTiles[this.lv][i]].enabled){
                        this.game.floorLocations[this.lv][visibleTiles[this.lv][i]].enabled = true;

in updateVisibleFloor function

I think i may have found the the reason, con you help me confirm it? I have made a for loop for 10 levels that go through every step of a floor creation, the last one is drawMap, there the code made this steps, it checked it there was existing floor tiles var existingFloor = app.root.find(“getName”, “Floor”); if not it created 1/3 of the map total tiles and after it allocated them in the x,y position, since it’s now repeated for 10 times, once the first level is created the second time the drawMap is launched it find all the floor tiles of first level and re-allocate them…and so on for each level, so when the code try to enable the first level floor tiles they are no more there.
EDIT: as i was writing the above i thougth of a way to test so i narrowed down the for loop to 1 level and it works prefectly! …awww…so tiring…now just have to remove the part of code that pre-create the tiles and reallocate them. Sorry everybody for have bothered you with my troubles.