Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×

Relearning API, What Are the getObj Lines Doing Here and How to Handle getObj Error?

Hi folks, I'm trying to relearn the javascript in Roll20 APIs. I had made some progress in 2020-2021 but have forgotten it all. I'm trying right now to run a script to pull a selected character token's name. I based the following on a google inquiry. It works just fine when I've selected a player character token or a token that is not associated with a character. However, it gives me TypeError: Cannot read properties of undefined (reading '0') right after the log "Starting ______ New"  and  before  the let chatcommand line. I don't understand why it happens there. So why does the first getObj line snuff up? And how can I handle or avoid such an error? Code is simple, I think, I have a lot of Logs to it: on("chat:message", function(msg) { log("Starting ______ New"); //Convert what is typed to lowercase and trim. let chatcommand = msg.content.trim().toLowerCase(); log("After convert msg to lower case"); if(msg.type == "api" && chatcommand.startsWith("!test")) { log("immediately after if statement"); let token = getObj("graphic", msg.selected[0]._id); log("token defined"); let char = getObj("character", token.get("represents")); log("character defined"); if(char) { log("if(char) part"); let charName = char.get("name"); log(charName); } else { log("else part"); // Token is null (not found) log("Not a character token"); } } }); Thanks for any help on this. -- Tim
1778034057
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Make sure you are on the Experimental sandbox. It gives the exact name and line where an error occurs now. My guess is this line: let token = getObj("graphic", msg.selected[0]._id); If you don't have an object selected then getObj is going to return undefined and kill the script at: let char = getObj("character", token.get("represents")); As to why you are not seeing the log lines like: log("After convert msg to lower case"); I'm not sure. It shouldn't crash by that point. Perhaps it's a race condition?
keithcurtis said: Make sure you are on the Experimental sandbox. It gives the exact name and line where an error occurs now. My guess is this line: let token = getObj("graphic", msg.selected[0]._id); If you don't have an object selected then getObj is going to return undefined and kill the script at: let char = getObj("character", token.get("represents")); As to why you are not seeing the log lines like: log("After convert msg to lower case"); I'm not sure. It shouldn't crash by that point. Perhaps it's a race condition? Thank  you Keith, Oh cool, an experimental sandbox? I will need to google that, thank you and the rest of your reply!
1778047279
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Tim M said: Oh cool, an experimental sandbox? I will need to google that, thank you and the rest of your reply! It's in a drop down on your mods page. It has better capabilities, and is the only option that can effectively work with beacon-based sheets.
keithcurtis said: Tim M said: Oh cool, an experimental sandbox? I will need to google that, thank you and the rest of your reply! It's in a drop down on your mods page. It has better capabilities, and is the only option that can effectively work with beacon-based sheets. Bacon based sheets? YUMMY.  Sorry, I'm sure I'm not the first person to make sch a lame joke.... :)  Not sure what beacon-based is, but at this stage I think  I'm OK without learning it for now. Thanks Keith.
1778084641
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Quick answer for anyone who does want to know: Beacon is a newer underlying framework or toolset for the construction of character sheets on Roll20. It allows for better management of data and improved capabilities in the user interface. Unfortunately, the way it was implemented makes it categorically more difficult to write scripts for, and many older scripts built to interact with the original architecture just plain won't work. The Experimental sandbox contains a number of special get and set functions that work with Beacon. The 2024 /5.5e DnD sheet is the most well-known examoople of a Beacon-based sheet.
1778096862
GiGs
Pro
Sheet Author
API Scripter
Beacon is the new way of creating character sheets. Unfortunately, I think it is a dead end because it creates extra challenges to creating sheets and the reason roll20 has so many sheets supporting so many systems is the ease (relatively speaking) of creating character sheets. You can use existing html, css, and (optionally) javascript, and dont need further setup to create your sheet. To create a Beacon sheet is much harder and that pretty much locks them to developers, which is why we've seen no-one developing them outside the roll20 team.