Page 1 of 1

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

Posted: Wed Oct 20, 2021 5:32 am
by CerealCat
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. :)

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

Posted: Wed Oct 20, 2021 6:48 am
by DrMekar
You could probably do this via Save Data, so I'm not an Expert at this.

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

Posted: Wed Oct 20, 2021 7:22 am
by deice
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)

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

Posted: Wed Oct 20, 2021 7:34 am
by Marioman2007
you could check for the total star count with 0x00B251E0 (global memory for the star count) and trigger the event.

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

Posted: Wed Oct 20, 2021 3:27 pm
by CerealCat
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!

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

Posted: Wed Oct 20, 2021 6:01 pm
by deice
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.

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

Posted: Wed Oct 20, 2021 6:21 pm
by CerealCat
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?

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

Posted: Thu Oct 21, 2021 3:09 am
by deice
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)

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

Posted: Thu Oct 21, 2021 3:35 am
by CerealCat
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.

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

Posted: Thu Oct 21, 2021 3:59 am
by CerealCat
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
	

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

Posted: Thu Oct 21, 2021 4:19 am
by deice
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.

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

Posted: Thu Oct 21, 2021 4:25 am
by CerealCat
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!