Need help with lua? - LunaLua General Help

This is the place for discussion and support for LunaLua and related modifications and libraries.

Moderator: Userbase Moderators

Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Sun Apr 24, 2016 10:15 am

http://hastebin.com/zenofukayi.lua
Mind you the Text.print line was to check to see how the random generation worked. Still nothing different.

S1eth
Bot
Bot
Posts: 54
Joined: Sat Apr 23, 2016 10:44 am

Re: Need help with lua? - LunaLua General Help

Postby S1eth » Sun Apr 24, 2016 11:12 am

pal4 wrote:http://hastebin.com/zenofukayi.lua
Mind you the Text.print line was to check to see how the random generation worked. Still nothing different.
if(tonumber(tableOfBirdo[1]:mem(0xF0, FIELD_DFLOAT)) = 1)

You may want to use "==" here instead of assigning 1 to the left hand value.

math.random(0, 2);

Just letting you know that the first time you call math.random, you will always receive the same result.
math.random has caused trouble for some people and I suggest you use the rng.lua API instead.

I wish people would stop putting raw hex values in their code without an explanation anywhere what it means
Last edited by S1eth on Sun Apr 24, 2016 12:58 pm, edited 1 time in total.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun Apr 24, 2016 11:28 am

S1eth wrote:
pal4 wrote:http://hastebin.com/zenofukayi.lua
Mind you the Text.print line was to check to see how the random generation worked. Still nothing different.
if(tonumber(tableOfBirdo[1]:mem(0xF0, FIELD_DFLOAT)) = 1)

You may want to use "==" here instead of an assigning 1 to the left hand value.

math.random(0, 2);

Just letting you know that the first time you call math.random, you will always receive the same result.
math.random has caused trouble for some people and I suggest you use the rng.lua API instead.

I wish people would stop putting raw hex values in their code wihtout explanation anywhere what it means
Not to mention 0xF0 is just tableOfBirdo[1].ai1

RhysOwens
Nipper
Nipper
Posts: 423
Joined: Fri Apr 22, 2016 2:53 am

Re: Need help with lua? - LunaLua General Help

Postby RhysOwens » Sun Apr 24, 2016 11:44 am

PixelPest wrote:
RhysOwens wrote:I heard you had to define the variable first.
You are by doing local Health = API.load "HealthPoint";
I removed the local Health = 0 and my code still works.

But like I said all NPCs I try still take multiple hits which I don't want.
And when they have lost their normal health the game still thinks the NPC is dead and give you points when the NPC isn't dead.
This bit of a screenshot below shows what's happening:
Image

pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Sun Apr 24, 2016 2:07 pm

Enjl wrote:
S1eth wrote:
pal4 wrote:http://hastebin.com/zenofukayi.lua
Mind you the Text.print line was to check to see how the random generation worked. Still nothing different.
if(tonumber(tableOfBirdo[1]:mem(0xF0, FIELD_DFLOAT)) = 1)

You may want to use "==" here instead of an assigning 1 to the left hand value.

math.random(0, 2);

Just letting you know that the first time you call math.random, you will always receive the same result.
math.random has caused trouble for some people and I suggest you use the rng.lua API instead.

I wish people would stop putting raw hex values in their code wihtout explanation anywhere what it means
Not to mention 0xF0 is just tableOfBirdo[1].ai1
This was how it was set up on the wiki (except the Text.print line, see above). You're saying I remove (0xF0, FIELD_DFLOAT) and replace math.random with rng.lua or whatever you would type that as?

S1eth
Bot
Bot
Posts: 54
Joined: Sat Apr 23, 2016 10:44 am

Re: Need help with lua? - LunaLua General Help

Postby S1eth » Sun Apr 24, 2016 2:57 pm

pal4 wrote:
Enjl wrote:
S1eth wrote:
if(tonumber(tableOfBirdo[1]:mem(0xF0, FIELD_DFLOAT)) = 1)

You may want to use "==" here instead of an assigning 1 to the left hand value.

math.random(0, 2);

Just letting you know that the first time you call math.random, you will always receive the same result.
math.random has caused trouble for some people and I suggest you use the rng.lua API instead.

I wish people would stop putting raw hex values in their code wihtout explanation anywhere what it means
Not to mention 0xF0 is just tableOfBirdo[1].ai1
This was how it was set up on the wiki (except the Text.print line, see above). You're saying I remove (0xF0, FIELD_DFLOAT) and replace math.random with rng.lua or whatever you would type that as?
Replace tableOfBirdo[1]:mem(0xF0, FIELD_DFLOAT)) with tableOfBirdo[1].ai1
and tableOfBirdo[1]:mem(0xF8, FIELD_DFLOAT)) with tableOfBirdo[1].ai2

This is purely for readability and will not change how your code works.

rng.lua replaces the random number generator, since the default generator has some problems, and doesn't work at all on some PCs (for example, on raocow's during the MAGLX2 stream)
How to use: http://wohlsoft.ru/pgewiki/Rng.lua

Quantumenace
Chain Chomp
Chain Chomp
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Sun Apr 24, 2016 5:08 pm

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.
My mistake, it looks more like 0x140 is actually part of a FIELD_DFLOAT at 0x13C that corresponds to the X- or Y-coordinate of the generator (depending on direction). I had thought it was some kind of NPC reference to the generator NPC itself.

pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Mon Apr 25, 2016 10:04 am

There is a second LunaSaved Vars0 file in my world folder. I'm pretty sure that's the last thing keeping my game from reading the code correctly. Except every time I delete it, it reappears when I try to run my level... :shock:

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Mon Apr 25, 2016 10:18 am

pal4 wrote:There is a second LunaSaved Vars0 file in my world folder. I'm pretty sure that's the last thing keeping my game from reading the code correctly. Except every time I delete it, it reappears when I try to run my level... :shock:
LunaSavedVars0 saves variables like the lunalua version number for the current save slot. There's no way it breaks anything.

Kevsoft
Ripper II
Ripper II
Posts: 375
Joined: Sun Jul 27, 2014 8:03 am

Re: Need help with lua? - LunaLua General Help

Postby Kevsoft » Mon Apr 25, 2016 12:18 pm

Enjl wrote:
pal4 wrote:There is a second LunaSaved Vars0 file in my world folder. I'm pretty sure that's the last thing keeping my game from reading the code correctly. Except every time I delete it, it reappears when I try to run my level... :shock:
LunaSavedVars0 saves variables like the lunalua version number for the current save slot. There's no way it breaks anything.
It is from the old LunaDLL saved vars system. It doesn't save any data from the Data-Class.

pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Mon Apr 25, 2016 4:11 pm

http://hastebin.com/wasilejise.lua
Any last flaws? Or is my code doomed to never work no matter what?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Mon Apr 25, 2016 4:21 pm

Shouldn't the line

Code: Select all

        if(table.getn(tableOfBirdoEggs) > 0) then
say

Code: Select all

        if(#tableOfBirdoEggs > 0) then
That aside, the way your code is set up is very suboptimal, as it only works for one birdo at a time and there's probably more things wrong with it. To fix it you need to make "ran" and "hasGenerated" specific for each birdo (and probably do some more things which I'm overlooking).

I'm only barely gona touch on that here, though, by mentioning "in pairs". Basically it's a cleaner way to get the contents of a table.

Code: Select all

for k,v in pairs(NPC.get(39, -1)) do
runs for all elements in the table NPC.get(39, -1). The index in the table (1,2,3,4,5,6, etc) is stored in k and the object itself is stored in v.
You can then do v.ai1 == x rather than tableOfBirdo[1].ai1 == x.

pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Mon Apr 25, 2016 4:47 pm

Only one Birdo exists, near the end of the level. Each world has one such encounter.

Quantumenace
Chain Chomp
Chain Chomp
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Tue Apr 26, 2016 2:33 am

It would help if you described any errors, or whether nothing happens at all. Are you certain the script is even being run?

S1eth
Bot
Bot
Posts: 54
Joined: Sat Apr 23, 2016 10:44 am

Re: Need help with lua? - LunaLua General Help

Postby S1eth » Tue Apr 26, 2016 10:22 am

pal4 wrote:http://hastebin.com/wasilejise.lua
Any last flaws? Or is my code doomed to never work no matter what?
What's the problem? Your code works.

An issue I see with it is that it only works if there is only a single birdo egg in the world at one time.
You are selecting the last index from tableOfBirdoEggs, which does not guarantee that you will get the most recently spawned egg.
For example, if you pick up an egg, your code will turn that egg in your hand into a fireball.
If you want to keep track of bird eggs' livetime, I suggest using pnpc and giving them a tick counter or something.

Another issue is that you're playing two sound effects at the same time, but I haven't played with lua Sound functions enough to know if there is a way to disable the Birdo's sound effect, or swap it out for the fireball sound effect for that one tick only.

Try this:

http://hastebin.com/lujebotuku.lua

This code ignores birdos completely. It only checks if a new birdo egg has been spawned, and 33% of the time, it turns that egg into a Ludwig fireball.
In other words, it even works with multiple birdos at the same time.

RhysOwens
Nipper
Nipper
Posts: 423
Joined: Fri Apr 22, 2016 2:53 am

Re: Need help with lua? - LunaLua General Help

Postby RhysOwens » Tue Apr 26, 2016 10:55 am

Has anyone forgotten about me?
I'm not really making levels with LunaLua right now.
I'm messing around with it and experimenting with it so I can get used to it.

The NPCs all take more hits even when I've set the ID to 1 NPC with HealthPoint.lua.

Mario_and_Luigi_55
Spike
Spike
Posts: 270
Joined: Sat Feb 27, 2016 12:01 pm

Re: Need help with lua? - LunaLua General Help

Postby Mario_and_Luigi_55 » Tue Apr 26, 2016 10:58 am

RhysOwens wrote:Has anyone forgotten about me?
I'm not really making levels with LunaLua right now.
I'm messing around with it and experimenting with it so I can get used to it.

The NPCs all take more hits even when I've set the ID to 1 NPC with HealthPoint.lua.


I have same problem.

pal4
Swooper
Swooper
Posts: 63
Joined: Wed Dec 23, 2015 7:57 pm

Re: Need help with lua? - LunaLua General Help

Postby pal4 » Tue Apr 26, 2016 12:13 pm

S1eth wrote:
pal4 wrote:http://hastebin.com/wasilejise.lua
Any last flaws? Or is my code doomed to never work no matter what?
What's the problem? Your code works.

An issue I see with it is that it only works if there is only a single birdo egg in the world at one time.
You are selecting the last index from tableOfBirdoEggs, which does not guarantee that you will get the most recently spawned egg.
For example, if you pick up an egg, your code will turn that egg in your hand into a fireball.
If you want to keep track of bird eggs' livetime, I suggest using pnpc and giving them a tick counter or something.

Another issue is that you're playing two sound effects at the same time, but I haven't played with lua Sound functions enough to know if there is a way to disable the Birdo's sound effect, or swap it out for the fireball sound effect for that one tick only.

Try this:

http://hastebin.com/lujebotuku.lua

This code ignores birdos completely. It only checks if a new birdo egg has been spawned, and 33% of the time, it turns that egg into a Ludwig fireball.
In other words, it even works with multiple birdos at the same time.
Tried it. no luck.
I'm pretty sure that's not how you get an npc id.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Tue Apr 26, 2016 12:30 pm

pal4 wrote:
S1eth wrote:
pal4 wrote:http://hastebin.com/wasilejise.lua
Any last flaws? Or is my code doomed to never work no matter what?
What's the problem? Your code works.

An issue I see with it is that it only works if there is only a single birdo egg in the world at one time.
You are selecting the last index from tableOfBirdoEggs, which does not guarantee that you will get the most recently spawned egg.
For example, if you pick up an egg, your code will turn that egg in your hand into a fireball.
If you want to keep track of bird eggs' livetime, I suggest using pnpc and giving them a tick counter or something.

Another issue is that you're playing two sound effects at the same time, but I haven't played with lua Sound functions enough to know if there is a way to disable the Birdo's sound effect, or swap it out for the fireball sound effect for that one tick only.

Try this:

http://hastebin.com/lujebotuku.lua

This code ignores birdos completely. It only checks if a new birdo egg has been spawned, and 33% of the time, it turns that egg into a Ludwig fireball.
In other words, it even works with multiple birdos at the same time.
Tried it. no luck.
I'm pretty sure that's not how you get an npc id.
npcID.lua includes a bunch of constants for easy use.
NPCID.BIRDOEGG = 40
I'm starting to think your lunadll.lua file is in the wrong directory. Is it one folder down from where the level file is located?

S1eth
Bot
Bot
Posts: 54
Joined: Sat Apr 23, 2016 10:44 am

Re: Need help with lua? - LunaLua General Help

Postby S1eth » Tue Apr 26, 2016 1:37 pm

Mario_and_Luigi_55 wrote:
RhysOwens wrote:Has anyone forgotten about me?
I'm not really making levels with LunaLua right now.
I'm messing around with it and experimenting with it so I can get used to it.

The NPCs all take more hits even when I've set the ID to 1 NPC with HealthPoint.lua.


I have same problem.
It's not you. HealthPoint is bugged.

First of all, when you load the API, it automatically sets all NPCs to 3 health. (the creator apparently thought that was a good idea...)
If you don't want that, you'd want to iterate over all NPC ids and call healthPoint.makeNPCNormal(id), but that doesn't work either.

healthPoint.makeNPCNormal is supposed to remove the entry for a specific NPC id from a table, but instead, it removes the value with the index of the npc id, and then pushes all other indexes in the table forward to fill in the gap.
It's pretty broken.

Here is a fix: http://hastebin.com/anerequxef.lua

Create a new text file inside your level folder. Copy and paste the hastebin text in there. Rename the file to HealthPoint.lua (remove the .txt extension)

Test it by having the following code in your lunadll.lua file for your level:

Code: Select all

local healthPoint = API.load("HealthPoint");

--healthPoint.healthbar = true; -- optional, use to activate healthbars above NPCs

for _,id in pairs(healthPoint.allNPCs) do
	healthPoint.makeNPCNormal(id);
end

healthPoint.setNPCHealth(1, 3);
This makes a goomba (ID 1) take 3 hits to kill. All other enemies are unaffected.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari