Can someone make a code for X amount of stars triggers an event?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Wed Oct 20, 2021 5:32 am

I don't know how to code anything and I didn't find any results searching for this. I'm wanting an event to play in my hub world after collecting a certain amount of stars. I don't know if it's possible, but I look forward to waking up tomorrow and seeing what replies I get. :)

DrMekar
Eerie
Eerie
Posts: 778
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby DrMekar » Wed Oct 20, 2021 6:48 am

You could probably do this via Save Data, so I'm not an Expert at this.

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Can someone make a code for X amount of stars triggers an event?

Postby deice » Wed Oct 20, 2021 7:22 am

CerealCat wrote:
Wed Oct 20, 2021 5:32 am
I don't know how to code anything and I didn't find any results searching for this. I'm wanting an event to play in my hub world after collecting a certain amount of stars. I don't know if it's possible, but I look forward to waking up tomorrow and seeing what replies I get. :)
while i assume you mean the overworld by "hub world", i'll offer up a solution to both interpretations just to be sure.

if you mean performing an action when the player enters the overworld for the first time after collecting a set amount of stars, first you have to ensure it can't happen more than once. so, inside onStart() within your "map.lua", you'll want to set a custom field inside SaveData that dictates whether the action has already been done, something like:

Code: Select all

if (SaveData.xStarsEventDone == nil) then SaveData.xStarsEventDone = false end
then, if it hasn't happened (i.e. said field is equal to false), simply compare the player's star count (global memory 0x00B251E0) to your desired value, and if it's greater than or equal to it, perform the desired action.

if you mean performing an event inside a hub-styled level after a certain amount of stars has been collected, that's also doable. use onPostNPCKill and check if the killed NPC was a star. then, once again just compare the star counter to your desired value, but this time only trigger the event if it's exactly equal to it (otherwise, the event would trigger with each subsequent collected star)

Marioman2007
Lakitu
Lakitu
Posts: 466
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Can someone make a code for X amount of stars triggers an event?

Postby Marioman2007 » Wed Oct 20, 2021 7:34 am

you could check for the total star count with 0x00B251E0 (global memory for the star count) and trigger the event.

CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Wed Oct 20, 2021 3:27 pm

Seems a little complicated, but I'll try it!

Added in 14 minutes 17 seconds:
deice wrote:
Wed Oct 20, 2021 7:22 am
CerealCat wrote:
Wed Oct 20, 2021 5:32 am
I don't know how to code anything and I didn't find any results searching for this. I'm wanting an event to play in my hub world after collecting a certain amount of stars. I don't know if it's possible, but I look forward to waking up tomorrow and seeing what replies I get. :)
while i assume you mean the overworld by "hub world", i'll offer up a solution to both interpretations just to be sure.

if you mean performing an action when the player enters the overworld for the first time after collecting a set amount of stars, first you have to ensure it can't happen more than once. so, inside onStart() within your "map.lua", you'll want to set a custom field inside SaveData that dictates whether the action has already been done, something like:

Code: Select all

if (SaveData.xStarsEventDone == nil) then SaveData.xStarsEventDone = false end
then, if it hasn't happened (i.e. said field is equal to false), simply compare the player's star count (global memory 0x00B251E0) to your desired value, and if it's greater than or equal to it, perform the desired action.

if you mean performing an event inside a hub-styled level after a certain amount of stars has been collected, that's also doable. use onPostNPCKill and check if the killed NPC was a star. then, once again just compare the star counter to your desired value, but this time only trigger the event if it's exactly equal to it (otherwise, the event would trigger with each subsequent collected star)
Like I described before, I don't know how to code anything. I understand your explanation slightly, but it would be easier if you did all the coding. Also, I want it on a hub-styled level. It's okay if you refuse to do so, I could probably figure it out myself if I needed to. Thanks in advance!

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Can someone make a code for X amount of stars triggers an event?

Postby deice » Wed Oct 20, 2021 6:01 pm

CerealCat wrote:
Wed Oct 20, 2021 3:41 pm
Like I described before, I don't know how to code anything. I understand your explanation slightly, but it would be easier if you did all the coding. Also, I want it on a hub-styled level. It's okay if you refuse to do so, I could probably figure it out myself if I needed to. Thanks in advance!
it's no problem, there isn't that much code (though it is a bit different from my explanation as i forgot to account for the fact you can still collect stars which were previously collected)

Code: Select all

local lastStars = 0

local STARS_FOR_EVENT = 2 --change this according to how many stars are needed to trigger the event
local EVENT_NAME = "test" --change this according to the name of the event

function onStart()
	lastStars = mem(0x00B251E0, FIELD_WORD)
end

function onPostNPCKill(killedNPC, harmType)
	if(killedNPC.id == 196 or killedNPC.id == 97) then
		if(mem(0x00B251E0, FIELD_WORD) > lastStars and mem(0x00B251E0, FIELD_WORD) == STARS_FOR_EVENT) then
			triggerEvent(EVENT_NAME)
		end
		lastStars = mem(0x00B251E0, FIELD_WORD)
	end
end
if the level in question already has lua code that has either of these functions defined, just take the stuff between the function declaration and the "end" keyword and paste it inside the respective function bodies.

CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Wed Oct 20, 2021 6:21 pm

deice wrote:
Wed Oct 20, 2021 6:01 pm
CerealCat wrote:
Wed Oct 20, 2021 3:41 pm
Like I described before, I don't know how to code anything. I understand your explanation slightly, but it would be easier if you did all the coding. Also, I want it on a hub-styled level. It's okay if you refuse to do so, I could probably figure it out myself if I needed to. Thanks in advance!
it's no problem, there isn't that much code (though it is a bit different from my explanation as i forgot to account for the fact you can still collect stars which were previously collected)

Code: Select all

local lastStars = 0

local STARS_FOR_EVENT = 2 --change this according to how many stars are needed to trigger the event
local EVENT_NAME = "test" --change this according to the name of the event

function onStart()
	lastStars = mem(0x00B251E0, FIELD_WORD)
end

function onPostNPCKill(killedNPC, harmType)
	if(killedNPC.id == 196 or killedNPC.id == 97) then
		if(mem(0x00B251E0, FIELD_WORD) > lastStars and mem(0x00B251E0, FIELD_WORD) == STARS_FOR_EVENT) then
			triggerEvent(EVENT_NAME)
		end
		lastStars = mem(0x00B251E0, FIELD_WORD)
	end
end
if the level in question already has lua code that has either of these functions defined, just take the stuff between the function declaration and the "end" keyword and paste it inside the respective function bodies.
Thank you! I'll let you know if anything goes wrong somehow, but it should go smoothly!

Added in 1 hour 44 minutes 6 seconds:
deice wrote:
Wed Oct 20, 2021 6:01 pm
CerealCat wrote:
Wed Oct 20, 2021 3:41 pm
Like I described before, I don't know how to code anything. I understand your explanation slightly, but it would be easier if you did all the coding. Also, I want it on a hub-styled level. It's okay if you refuse to do so, I could probably figure it out myself if I needed to. Thanks in advance!
it's no problem, there isn't that much code (though it is a bit different from my explanation as i forgot to account for the fact you can still collect stars which were previously collected)

Code: Select all

local lastStars = 0

local STARS_FOR_EVENT = 2 --change this according to how many stars are needed to trigger the event
local EVENT_NAME = "test" --change this according to the name of the event

function onStart()
	lastStars = mem(0x00B251E0, FIELD_WORD)
end

function onPostNPCKill(killedNPC, harmType)
	if(killedNPC.id == 196 or killedNPC.id == 97) then
		if(mem(0x00B251E0, FIELD_WORD) > lastStars and mem(0x00B251E0, FIELD_WORD) == STARS_FOR_EVENT) then
			triggerEvent(EVENT_NAME)
		end
		lastStars = mem(0x00B251E0, FIELD_WORD)
	end
end
if the level in question already has lua code that has either of these functions defined, just take the stuff between the function declaration and the "end" keyword and paste it inside the respective function bodies.
It works when I collect a star in the hub world, but not when I collect a star in a different level. Would there be a way to trigger it by collecting a star in a different level, or is that impossible?

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Can someone make a code for X amount of stars triggers an event?

Postby deice » Thu Oct 21, 2021 3:09 am

CerealCat wrote:
Wed Oct 20, 2021 8:05 pm
It works when I collect a star in the hub world, but not when I collect a star in a different level. Would there be a way to trigger it by collecting a star in a different level, or is that impossible?
you can just copy and paste it into the lua file of any level you need it to work for, or make it into a library and load it into each level you need it to work for. (however, it's obviously not possible to trigger an event that only exists in a different level, and you'd have to mess about with SaveData and GameData in order to make changes that affect the entire game happen)

CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Thu Oct 21, 2021 3:35 am

deice wrote: you can just copy and paste it into the lua file of any level you need it to work for, or make it into a library and load it into each level you need it to work for. (however, it's obviously not possible to trigger an event that only exists in a different level, and you'd have to mess about with SaveData and GameData in order to make changes that affect the entire game happen)
I'll try that.

CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Thu Oct 21, 2021 3:59 am

deice wrote:
Thu Oct 21, 2021 3:09 am
you can just copy and paste it into the lua file of any level you need it to work for, or make it into a library and load it into each level you need it to work for. (however, it's obviously not possible to trigger an event that only exists in a different level, and you'd have to mess about with SaveData and GameData in order to make changes that affect the entire game happen)
Would something like this work? I have no idea what I'm doing, so sorry if this is completely wrong.

Code: Select all


local EVENT_NAME = "test"

SaveData.hudInfo = SaveData.hudInfo or {
	
	if stars = 1 then triggerEvent(EVENT_NAME)
	end
	

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Can someone make a code for X amount of stars triggers an event?

Postby deice » Thu Oct 21, 2021 4:19 am

CerealCat wrote:
Thu Oct 21, 2021 3:59 am
Would something like this work? I have no idea what I'm doing, so sorry if this is completely wrong.

Code: Select all


local EVENT_NAME = "test"

SaveData.hudInfo = SaveData.hudInfo or {
	
	if stars = 1 then triggerEvent(EVENT_NAME)
	end
	
barring the fact that the syntax there is invalid, if you want to (for example) make sure an event is triggered in your hub level after a certain amount of stars is collected anywhere in the game, you'd have to not only check for the event condition within the hub, but also set certain values within SaveData when stars are collected in other levels, and then in the hub's onStart() you'd once again trigger events and perform more code depending on said values.

since this isn't a trivial amount of work and there are many small tweaks that need to be made depending on the exact circumstances and conditions you want certain events to trigger under, i recommend finding a dedicated coder to help you with your episode when it comes to things like implementing these complex systems of progression.

CerealCat
Rex
Rex
Posts: 30
Joined: Thu Jul 11, 2019 5:31 pm
Flair: twitch.tv/mscerealcat
Pronouns: she/they
Contact:

Re: Can someone make a code for X amount of stars triggers an event?

Postby CerealCat » Thu Oct 21, 2021 4:25 am

deice wrote: barring the fact that the syntax there is invalid, if you want to (for example) make sure an event is triggered in your hub level after a certain amount of stars is collected anywhere in the game, you'd have to not only check for the event condition within the hub, but also set certain values within SaveData when stars are collected in other levels, and then in the hub's onStart() you'd once again trigger events and perform more code depending on said values.

since this isn't a trivial amount of work and there are many small tweaks that need to be made depending on the exact circumstances and conditions you want certain events to trigger under, i recommend finding a dedicated coder to help you with your episode when it comes to things like implementing these complex systems of progression.
Ok, thank you for all the help!


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari