Some scripting help

I have no clue how to upload a txt file, so I’ll just paste the scripts here. There’s three of them that were made, two being the same, with slightly different requirements. I just want to know if they would work, or if there’s any changes that would be needed for them.

– Define the character and room profile changes
local targetCharacter = “Alice” – Character name to trigger the change
local enterProfile = “WelcomeProfile”
local exitProfile = “DefaultProfile”

– Event handler for character entering a room
function onEnterRoom(character, room)
if character.name == targetCharacter then
– Change the room profile when the target character enters
room:setProfile(enterProfile)
print(character.name .. " entered. Room profile set to " .. enterProfile)
end
end

– Event handler for character leaving a room
function onLeaveRoom(character, room)
if character.name == targetCharacter then
– Revert or change the room profile when the target character leaves
room:setProfile(exitProfile)
print(character.name .. " left. Room profile set to " .. exitProfile)
end
end

– Register event handlers
registerEvent(“enterRoom”, onEnterRoom)
registerEvent(“leaveRoom”, onLeaveRoom)

– Configuration
local TARGET_CHARACTER = “Alice” – Character that triggers the change
local ENTER_PROFILE = “WelcomeProfile” – Profile applied when entering
local EXIT_PROFILE = “DefaultProfile” – Profile applied when leaving

– Function to handle character entering a room
function handleEnterRoom(event)
local character = event.character
local room = event.room

-- Check if the character is the one we want
if character.name == TARGET_CHARACTER then
    -- Change the room profile
    if room.setProfile then
        room:setProfile(ENTER_PROFILE)
        print("[" .. character.name .. "] entered the room. Profile set to: " .. ENTER_PROFILE)
    else
        print("Error: room:setProfile function not found.")
    end
end

end

– Function to handle character leaving a room
function handleLeaveRoom(event)
local character = event.character
local room = event.room

-- Check if the character is the one we want
if character.name == TARGET_CHARACTER then
    -- Revert or change the room profile
    if room.setProfile then
        room:setProfile(EXIT_PROFILE)
        print("[" .. character.name .. "] left the room. Profile set to: " .. EXIT_PROFILE)
    else
        print("Error: room:setProfile function not found.")
    end
end

end

– Register the events with Mucklet
registerEvent(“enterRoom”, handleEnterRoom)
registerEvent(“leaveRoom”, handleLeaveRoom)

– Configuration
local WELCOME_MESSAGE = “Welcome to the room! Enjoy your stay.”

– Function triggered when a character enters a room
function handleEnterRoom(event)
local character = event.character
local room = event.room

-- Send a message to the character
if character.sendMessage then
    character:sendMessage(WELCOME_MESSAGE)
    print("Sent welcome message to " .. character.name .. " upon entering " .. room.name)
else
    print("Error: character:sendMessage function not available.")
end

end

– Register the event with Mucklet
registerEvent(“enterRoom”, handleEnterRoom)

If this was the incorrect way to post this, please let me know. I have no clue how to set this.

i’d suggest adding three backticks ` at the start and at the end of each script on lines by themselves which will format it better, e.g.

-- Function triggered when a character enters a room
function handleEnterRoom(event)
local character = event.character
local room = event.room

Or put them in a GitHub Gist or the like.

Hello! Good to see you trying to script. Check out the documentation at mucklet-script/docs/documentation.md at master · mucklet/mucklet-script · GitHub. Have you tried adding the script to a room and getting it to work? I would recommend using the cli tool in order to give you a starting script project. Otherwise, the bootstrapped script is at mucklet-script/scripts/index.ts at develop · mucklet/mucklet-script · GitHub

Couple notes – Mucklet script is written in a variant of typescript called assemblyscript. Variables are defined with let or const rather than local. Room events are registered with a Room listener. You would want to use the character events with Room.listenCharEvent() in the onActivate function! Character events would be handled in the onCharEvent function. Should look something like this:

export function onCharEvent(
	addr: string,
	charId: string,
	after: string | null,
	before: string | null,
): void {
	if (after == null && before != null) {
		// If after is null, the character left
		const char = JSON.parse<Room.Char>(before);
		if (char.name == TARGET_CHARACTER) {
			Room.useProfile(DEFAULT_PROFILE)
		}
	}
	if (before == null && after != null) {
		// If before is null, the character arrived
		const char = JSON.parse<Room.Char>(after);
		if(char.name == TARGET_CHARACTER) {
			Room.useProfile(WELCOME_PROFILE)
		}
	}
}

Happy scripting. :slight_smile:

To be clear, this script itself wasn’t made by me, but ChatGPT. I asked it to show me a few examples of mucklet scripting for specific types of actions. I’m still working on understanding the script coding myself at the moment. For this, I wanted to know if the script it gave me was accurate enough I could test with it.

Also, to be clear, again, I have been looking over the github page for mucklet scripting. I’m just… having a hard time understanding a lot of it.

No, not at all. It seems to be written in pseudocode rather then typescript. Although chatgpt is good at writing this sort of stuff with a little context of a GitHub page it helps to have something built and deployed first. I tried to explain a little. ^^"

I’ve been poking at the script you revised, and the one thing I’m having the most trouble figuring out, is how to set the specific character for which the script will trigger. That, and how to set the right room profile to switch between. One other thing I’m having trouble with, is a script that was written for the shrine, back before Fox and Shinyuu left Wolfery, for the entrance. I don’t know any of the other types of arrival events, other than what was set, which is teleport. I wanted to co-opt that script for a separate room, but without knowing what arrival method I need, I’m having difficulty. The co-opted one from Fox compiles at least. It just doesn’t trigger, so what I’ve tried so far isn’t the right event method.

export function onActivate(): void {
  Room.listen();
}

export function onRoomEvent(addr: string, ev: string): void {
  if (Event.getType(ev) != "arrive") {
    return;
  }

  const e = JSON.parse<Event.Arrive>(ev);

  if (e.method != "teleport") {
    return;
  }

  Room.describe(
    `Hi! Welcome to the shrine. Please make sure to observe the \`Area Rules\`, ` +
    `and please, don't try to go into the \`Living Quarters\` as those are **private** rooms. ` +
    `Thank you, and please enjoy your visit.`);
}

This is the code that Fox wrote up for the shrine entrance. The one spot I don’t know is the method of arrival, in this case, teleport. If I knew which method was for just arriving in a room by simple room to room travel, I could set the script for other rooms.

It does help to tell ChatGPT you’re using mucklet-script specifically. That’s how I was able to make the seasonal profile script.

1 Like

It’s right here: the onRoomEvent()
Once the ‘ev’ (event) has been declared as arrive, you parse the JSON data for that event, which is Event.Arrive.

You can read about onRoomEvent function here: mucklet-script/docs/documentation.md at master · mucklet/mucklet-script · GitHub
Event.Arrive is here: mucklet-script/docs/documentation.md at master · mucklet/mucklet-script · GitHub

So in Fox’s code there he uses Room.listen() This puts a little dot in the top right corner alerting people that a script is active and listening to all room events. You can do it this way. I wrote a bit of code here that checks for leave and arrive events and changes the profile accordingly. You can also handle say and other events. The detail of Events in the api is here

const targetCharacter = "Alice"
const welcomeProfile = "Alice"
const defaultProfile = "Default"

export function onActivate(): void {
	Room.listen();
}

export function onRoomEvent(
	addr: string,
	ev: string,
): void {
	const eventType = Event.getType(ev);
	if (eventType == 'arrive') {
		const arrive = JSON.parse<Event.Arrive>(ev);
		if (arrive.char.name == targetCharacter) {
			Room.useProfile(welcomeProfile);
		}
	}
	if (eventType == 'leave') {
		const leave = JSON.parse<Event.Leave>(ev);
		if (leave.char.name == targetCharacter) {
			Room.useProfile(defaultProfile);
		}
	}
}

If you wanted to be more conscious about not handling all room events. You can use the Room.listenCharEvents listener like I detailed above.

const targetCharacter = "Alice"
const welcomeProfile = "Alice"
const defaultProfile = "Default"

export function onActivate(): void {
	Room.listenCharEvent();
}

export function onCharEvent(
	addr: string,
	charId: string,
	after: string | null,
	before: string | null,
): void {
	if (after == null && before != null) {
		// If after is null, the character left
		const char = JSON.parse<Room.Char>(before);
		if (char.name == targetCharacter) {
			Room.useProfile(defaultProfile)
		}
	}
	if (before == null && after != null) {
		// If before is null, the character arrived
		const char = JSON.parse<Room.Char>(after);
		if(char.name == targetCharacter) {
			Room.useProfile(welcomeProfile)
		}
	}
}

I would recommend doing it this way anyway. With commands too now I don’t see much reason to handle say and pose events. Maybe puts me at ease when I know they’re not being handled at all.

const targetCharacter = ""
const welcomeProfile = "forging"
const defaultProfile = "not-forging"

export function onActivate(): void {
	Room.listen();
}

export function onRoomEvent(
	addr: string,
	ev: string,
): void {
	const eventType = Event.getType(ev);
	if (eventType == 'arrive') {
		const arrive = JSON.parse<Event.Arrive>(ev);
		if (arrive.char.id == targetCharacter) {
			Room.useProfile(welcomeProfile);
		}
	}
	if (eventType == 'leave') {
		const leave = JSON.parse<Event.Leave>(ev);
		if (leave.char.id == targetCharacter) {
			Room.useProfile(defaultProfile);
		}
	}
}

After numerous attempts, changing the Target parameters, and some advice from Raeth, this is what we got to work for what I needed. Thank you to Raeth, Waku, and everyone else who assisted. I’ll probably have more scripts to try eventually.

1 Like

From a moderation perspective it’s a best practice to have the room.listen dot active on any script that could be construed as ‘listening’. This is a longstanding idea/compromise to let us have scripting while letting people know when something ‘might be listening’. (Similar to how the blue character names are an indication that you’re talking to a script.)

1 Like