Page 16 of 76
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 5:36 am
by Mario_and_Luigi_55
RhysOwens wrote:Hoeloe wrote:RhysOwens wrote:
I was copying the functions in the lua file near the beginning.
But you didn't understand enough of what you were doing to know that you need to define variables before you use them.
Do I call the variable HealthPoint?
I've abandoned the HealthPoint thing for now.
Yes. You can call it whatever you want for example:
Code: Select all
local something = API.load "HealthPoint"
But then you don't use
Code: Select all
HealthPoint.setNPCHealth(npcID, Health)
, but
Code: Select all
something.setNPCHealth(npcID, Health)
Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 7:57 am
by PixelPest
Mario_and_Luigi_55 wrote:Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I would recommend using rng.lua for sure. Also, do you mean after the player eats npc-263? That's the ?-mushroom. If so, you would start by loading rng.lua. Next, start with the onNPCKill() event and inside test to see if npc-263 has been killed. Then, if npc-263 was killed use the parameter player.reservePowerup and make that equal a random number (using rng)
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 8:58 am
by Mario_and_Luigi_55
PixelPest wrote:Mario_and_Luigi_55 wrote:Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I would recommend using rng.lua for sure. Also, do you mean after the player eats npc-263? That's the ?-mushroom. If so, you would start by loading rng.lua. Next, start with the onNPCKill() event and inside test to see if npc-263 has been killed. Then, if npc-263 was killed use the parameter player.reservePowerup and make that equal a random number (using rng)
This code works, but it's the shortest longest code in the world Can I somehow make it shorter?
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 9:10 am
by PixelPest
Mario_and_Luigi_55 wrote:PixelPest wrote:Mario_and_Luigi_55 wrote:Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I would recommend using rng.lua for sure. Also, do you mean after the player eats npc-263? That's the ?-mushroom. If so, you would start by loading rng.lua. Next, start with the onNPCKill() event and inside test to see if npc-263 has been killed. Then, if npc-263 was killed use the parameter player.reservePowerup and make that equal a random number (using rng)
This code works, but it's the shortest longest code in the world Can I somehow make it shorter?
Make a table with all of those numbers and replace all of that stuff with the name of the table, like: switchTable = {numbers here}
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 9:41 am
by RhysOwens
Mario_and_Luigi_55 wrote:
Yes. You can call it whatever you want for example:
Code: Select all
local something = API.load "HealthPoint"
But then you don't use
Code: Select all
HealthPoint.setNPCHealth(npcID, Health)
, but
Code: Select all
something.setNPCHealth(npcID, Health)
Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I've done what you've done and this is my new code:
Code: Select all
Health = 0
local Health = API.load "HealthPoint"
function Health.setNPCHealth(54, 5)
end
And the error I'm now getting says: <name> or '...' expected near '54'
From 54 I mean NPC 54 if that helps.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 9:47 am
by PixelPest
RhysOwens wrote:Mario_and_Luigi_55 wrote:
Yes. You can call it whatever you want for example:
Code: Select all
local something = API.load "HealthPoint"
But then you don't use
Code: Select all
HealthPoint.setNPCHealth(npcID, Health)
, but
Code: Select all
something.setNPCHealth(npcID, Health)
Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I've done what you've done and this is my new code:
Code: Select all
Health = 0
local Health = API.load "HealthPoint"
function Health.setNPCHealth(54, 5)
end
And the error I'm now getting says: <name> or '...' expected near '54'
From 54 I mean NPC 54 if that helps.
No no no. Use function onTick() and then set the variable inside that event
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 10:48 am
by S1eth
RhysOwens wrote:Mario_and_Luigi_55 wrote:
Yes. You can call it whatever you want for example:
Code: Select all
local something = API.load "HealthPoint"
But then you don't use
Code: Select all
HealthPoint.setNPCHealth(npcID, Health)
, but
Code: Select all
something.setNPCHealth(npcID, Health)
Also, I want to improve the ? mushroom (npc-273). I want to make it give player completely random NPC to the powerup box, after he grabs it. How can I do that? I'll need to use math.random or rng.lua (that's what I know, but here my knowledge ends)
I've done what you've done and this is my new code:
Code: Select all
Health = 0
local Health = API.load "HealthPoint"
function Health.setNPCHealth(54, 5)
end
And the error I'm now getting says: <name> or '...' expected near '54'
From 54 I mean NPC 54 if that helps.
This will answer all your questions:
http://www.lua.org/pil/contents.html
And after you've read all that, if you need help with SMBX specifically, watch:
https://www.youtube.com/watch?v=E69ti1Q ... wd5hW2KwOw
and
http://wohlsoft.ru/pgewiki/How_To:_Make ... custom_API
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 12:02 pm
by Mario_and_Luigi_55
And can I make lua do this code until end of table? Right now this code only checks the first position of table.
http://www.hastebin.com/ejedeyawuz.lua
also
this didn't work
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 12:46 pm
by underFlo
That 2nd code doesn't work because using or like this only works with booleans.
For the 1st code, you're only decreasing TablePosition once every frame, so it won't check for all the table positions. What you should do is add a loop, like this:
http://www.hastebin.com/ijinenebam.lua
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 12:48 pm
by S1eth
You need to iterate over the table and check for every index if the table value equals the new powerup value.
Here, I made a "contains" function, which does that for you. It checks if a given table contains a given value, and returns true if it does, and false if it doesn't.
http://www.hastebin.com/teviyaxumo.lua
BTW, you misspelled TableLength. But you don't need that. There is #tableName or table.getn(tableName) for that.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 1:07 pm
by Mario_and_Luigi_55
Thank you! I wouldn't probably guess how to do that and I would go back to old code.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 6:19 pm
by pal4
Enjl wrote:It's so your code is actually formatted well. You can also use the
Code: Select all
bbcode but that's limited to like 6 lines at a time. Just pasting it to the page makes it unreadable.[/quote]
What is the [code] bbcode,? Also you didn't tell me how I'm supposed to actually use hastebin.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 6:21 pm
by PixelPest
pal4 wrote:Enjl wrote:It's so your code is actually formatted well. You can also use the
Code: Select all
bbcode but that's limited to like 6 lines at a time. Just pasting it to the page makes it unreadable.[/quote]
What is the [code] bbcode,? Also you didn't tell me how I'm supposed to actually use hastebin.[/quote]
It's not that hard. You just press paste. Then you press save. Then you copy the url and paste it here
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 7:24 pm
by Quantumenace
Is there a way to convert an NPC pointer variable into something I can use in NPC:mem() ?
I want to try to put a pointer to an NPC in 0x140, because otherwise an NPC i'm using in a simulated warp-type generator assumes its generator is to its right and immediately thinks it's "finished" if it's moving left.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 7:41 pm
by Hoeloe
Quantumenace wrote:Is there a way to convert an NPC pointer variable into something I can use in NPC:mem() ?
I want to try to put a pointer to an NPC in 0x140, because otherwise an NPC i'm using in a simulated warp-type generator assumes its generator is to its right and immediately thinks it's "finished" if it's moving left.
If you just want to store references attached to the NPC, the best way is to wrap it with pnpc and using the PNPC.data table that is attached to all pnpc objects.
Re: Need help with lua? - LunaLua General Help
Posted: Sat Apr 23, 2016 8:01 pm
by Quantumenace
Hoeloe wrote:Quantumenace wrote:Is there a way to convert an NPC pointer variable into something I can use in NPC:mem() ?
I want to try to put a pointer to an NPC in 0x140, because otherwise an NPC i'm using in a simulated warp-type generator assumes its generator is to its right and immediately thinks it's "finished" if it's moving left.
If you just want to store references attached to the NPC, the best way is to wrap it with pnpc and using the PNPC.data table that is attached to all pnpc objects.
Yes, I do that for other stuff but it won't work for what I want here because it's part of the SMBX programming. I can make an NPC act like it's being spawned from a warp-type generator by setting its 0x138 to 4 and its 0x144 to the generator direction. 0x140 holds a value that is set for NPCs spawned by a generator so I think it's some kind of reference.
If it's moving right or down, it keeps moving until I set those to something else and stop it. But if it's going up or left, it "drops out" immediately. I can force it to keep going by setting those every tick but it appears in front of blocks instead of behind.
...But I found that setting those values again at onTickEnd fixes the priority. So maybe I don't need to bother, but I'd still like to know if it's possible.
Re: Need help with lua? - LunaLua General Help
Posted: Sun Apr 24, 2016 6:32 am
by RhysOwens
Finally the Fighterfly has more health.
This was my final code:
Code: Select all
local Health = 0
local Health = API.load "HealthPoint";
function onStart()
Health.setNPCHealth(54, 12)
end
But it seems that all NPC take more hits and some still give you points when their not dead,
Re: Need help with lua? - LunaLua General Help
Posted: Sun Apr 24, 2016 7:08 am
by Hoeloe
Quantumenace wrote:I'd still like to know if it's possible.
I'm not really sure what you're asking for here. All the memory locations in the NPC struct have specific types and uses, you can't just change it by trying to store an NPC object there, which won't work anyway. The closest you can get is storing the index into the NPC array, which is what SMBX usually does when it needs to keep track on an NPC.
RhysOwens wrote:
This was my final code:
Code: Select all
local Health = 0
local Health = API.load "HealthPoint";
function onStart()
Health.setNPCHealth(54, 12)
end
Why are you setting your variable to 0 and then to the API you want? There's no reason to set it to 0 first.
Re: Need help with lua? - LunaLua General Help
Posted: Sun Apr 24, 2016 10:04 am
by RhysOwens
Hoeloe wrote:Quantumenace wrote:I'd still like to know if it's possible.
I'm not really sure what you're asking for here. All the memory locations in the NPC struct have specific types and uses, you can't just change it by trying to store an NPC object there, which won't work anyway. The closest you can get is storing the index into the NPC array, which is what SMBX usually does when it needs to keep track on an NPC.
RhysOwens wrote:
This was my final code:
Code: Select all
local Health = 0
local Health = API.load "HealthPoint";
function onStart()
Health.setNPCHealth(54, 12)
end
Why are you setting your variable to 0 and then to the API you want? There's no reason to set it to 0 first.
I heard you had to define the variable first.
Re: Need help with lua? - LunaLua General Help
Posted: Sun Apr 24, 2016 10:10 am
by PixelPest
RhysOwens wrote:I heard you had to define the variable first.
You are by doing local Health = API.load "HealthPoint";