Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: identifier 'ui' undefined #113

Closed
mgovea opened this issue Mar 30, 2021 · 14 comments
Closed

ReferenceError: identifier 'ui' undefined #113

mgovea opened this issue Mar 30, 2021 · 14 comments
Labels
bug Something isn't working

Comments

@mgovea
Copy link
Owner

mgovea commented Mar 30, 2021

Investigate & maybe fix.

Mentioned by @kscheel in #37 (comment)_

@mgovea
Copy link
Owner Author

mgovea commented Mar 30, 2021

He did say

it throws an error on my headless server

so I'm betting that this will only show up in that context. No UI -> no ui

@mgovea mgovea changed the title [Ride Price Manager] ReferenceError: identifier 'ui' undefined ReferenceError: identifier 'ui' undefined Mar 30, 2021
@mgovea mgovea added the bug Something isn't working label Mar 30, 2021
@mgovea
Copy link
Owner Author

mgovea commented Mar 30, 2021

This gets me to thinking... I may add documentation for server owners so they can change the configuration file by hand, since they might not have access to the UI.

@KuestenKeks
Copy link

KuestenKeks commented Mar 31, 2021

so I'm betting that this will only show up in that context. No UI -> no ui

exactly :) The scripting Reference also has some info on this:


Can I run code only if the user interface is available?

Yes, it is good practice, particularly if writing scripts for servers to check if the game is running in headless mode before attempting to use any UI APIs. The ui namespace is not available in headless mode, so make sure you check it, otherwise an error will be thrown.

if (typeof ui !== 'undefined') {
    console.log("OpenRCT2 is not running in headless, UI is available!");
    ui.registerMenuItem('My window', function() {
        // ...
    });
}

This gets me to thinking... I may add documentation for server owners so they can change the configuration file by hand, since they might not have access to the UI.

The UI is actually available to other players. Every client that connects can access the UI. Again from the Scripting Reference:


Can servers add additional user interface elements to players?

Yes, remote scripts are uploaded to every client and run as-is. Even if the server is running in headless mode, the script can still contain UI calls for the benefit of the players that are not running in headless mode. Be sure to check if the UI is available first before executing UI calls.


kind regards :)

PS: it just ocurred to me, that the plugin settings are maybe not shared between server and clients 🤔 I'll check later if changes from one Client also appear for other clients :) I also wonder, if every client will try to update the price ride settings individiually 🙈

@mgovea
Copy link
Owner Author

mgovea commented Mar 31, 2021

PS: it just ocurred to me, that the plugin settings are maybe not shared between server and clients 🤔 I'll check later if changes from one Client also appear for other clients :) I also wonder, if every client will try to update the price ride settings individiually 🙈

Yeah, every client (with the plugin) will individually update the price every day, so the one who sets it last, wins. (Or that's probably how it works. Again, I'm too lazy to test 😜)

Edit: and they absolutely do not sync settings

@KuestenKeks
Copy link

Yeah, every client (with the plugin) will individually update the price every day, so the one who sets it last, wins. (Or that's probably how it works. Again, I'm too lazy to test 😜)

Edit: and they absolutely do not sync settings

yeah, I can confirm that they do not sync settings, this was easy enough to test.

It wouldn't be too hard to disable the Plug In for clients in network games 🤔 Then the server would be the only one doing the daily price updates. In this case having some documentation about changing the configuration manually, like you suggested, would really be handy.

Anyway, these things should probably better have their own issue, getting a little carried away here from the original topic ^^

@mgovea
Copy link
Owner Author

mgovea commented Apr 2, 2021

https://github.com/mgovea/openrct2-ride-price-manager/releases/tag/v1.3.1

I did a lot of the bonus stuff we talked about. @kscheel would you mind double-checking the "set the server config" instructions in the readme?

https://github.com/mgovea/openrct2-ride-price-manager/tree/v1.3.1#multiplayer

Let me know if you have more ideas :)

@mgovea mgovea closed this as completed Apr 2, 2021
@KuestenKeks
Copy link

KuestenKeks commented Apr 5, 2021

Really nice, thanks for the update! :)

Unfortunately, the error still occurs. But I see you added a UI check for headless server homies ;D I noticed that your check if (ui) doesn't use typeof like the the example does. Maybe it should be if (typeof ui !== 'undefined')? But idk if that's correct typescript syntax, as the official example doesn't use typescript ;)

And somehow the plug-in isn't working properly at all on my server. The daily price updates simply do not occur. But using "Force recalculate now" from a client works.

Maybe the ReferenceError stops the plug-in execution on the server? Otherwise I could try to build the plug-in myself and add some debug output to the console to figure out what's going on.


Regarding the readme instructions for multiplayer: I think it's only necessary to edit the configuration file when you're hosting a headless server? When I host game and also play on the host machine, I assume I could use the plug-in UI as usual? This could be clarified in the readme.

And one more idea :)
Maybe the UI elements a client can't use could be disabled for clients? Then only the two buttons "Force recalculate now" and "Make rides free" would be enabled. (And as an optional bonus an info text box for this situation could be added, e.g.: "Plug-In Settings are managed by the host" ;)

@mgovea mgovea reopened this Apr 6, 2021
@mgovea
Copy link
Owner Author

mgovea commented Apr 6, 2021

I bet the error prevents the daily calculation from registering.

if (ui) is a "truthy" check, and undefined should always be "falsey". So I'll do what the man says, but I don't know why it wouldn't work the way I wrote it.

Maybe the UI elements a client can't use could be disabled for clients?

Yeah, I want to have a UI library that is more dynamic, and I ran into a ton of trouble when trying to use https://github.com/oli414/OliUI
I want to make a React-based UI library, but that is more work than I was willing to do last time I looked into it. But having a different UI for MP clients would be nice. I also kinda want to have admins (players with the kick_player permission) have their config changes sync to the server, but that seems like a lot, haha.

Oooooh, I could disable the UI elements and have the state read from the server's config instead of from client config. Spicy, haha.

@mgovea
Copy link
Owner Author

mgovea commented Apr 6, 2021

Regarding the readme instructions for multiplayer: I think it's only necessary to edit the configuration file when you're hosting a headless server? When I host game and also play on the host machine, I assume I could use the plug-in UI as usual? This could be clarified in the readme.

Oh yeah, will do. Dunno why my eyes skipped over this part.

@KuestenKeks
Copy link

KuestenKeks commented Apr 6, 2021

if (ui) is a "truthy" check, and undefined should always be "falsey". So I'll do what the man says, but I don't know why it wouldn't work the way I wrote it.

I tried to do some reading, it seems if (ui) throws the reference error because ui isn't just undefined, it was never declared. Therefore trying to access ui just throws the ReferenceError exception instead of evaluating to true or false 🤔 But using typeof is safe :)

Oooooh, I could disable the UI elements and have the state read from the server's config instead of from client config. Spicy, haha.

uuuh, that would be fancy 😋
And yeah, having the UI of admin clients fully functional / syncing with the server would also be fancy, but I'm happy with editing the config file for now :)

cheers!

@mgovea
Copy link
Owner Author

mgovea commented Apr 7, 2021

if (ui) throws the reference error

damn u right

See for yourself: if you hit F12 & open the browser console, you can test js stuff. !!undefined returns false, while !!a throws.

I hope you know that I'm adding you to the list of authors.

@mgovea
Copy link
Owner Author

mgovea commented Apr 7, 2021

@KuestenKeks
Copy link

Looking good mate! We played for a few hours and everything seemed to work fine :) Squeezing all that sweet cash out of our guests 💸

Thanks for adding me as an author :3

@mgovea
Copy link
Owner Author

mgovea commented Apr 11, 2021

Yee thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants