Log In  

Hello all, again.
I'm trying to get my head around tables.
I thought it would be a good idea to store a table of the possible modes and enemy can be in, chase, pause, detect etc.
So the enemy could only be in one mode at a time without all the if statements.

I tried this:

function add_new_enemy(x,y,type)
    add(enemy,{
        mode={},
        add(mode,{1,2,3}),
        x=x,
        y=y.... etc.

Adding a table called 'mode' with values 1,2,3 (All in the name of experimentation.)

Similar to the last problem I asked about, I'm trying to just print the first value of 'mode'.
In the _draw I have this:

print(enemy[1].mode[1])

But that print NIL

What am I doing wrong? is my table all wrong are is my print statement wrong?

Many thanks
Peej

P#95007 2021-07-17 22:16

you could try doing

function add_new_enemy(x,y,type)
    add(enemy,{
        mode={1,2,3},
        x=x,
        y=y.... etc.

which seems to work when i tested it. i think the issue here is that the 'add(mode,{1,2,3})' isn't actually adding to the table associated with the key 'mode'

this is probably happening because the add function expects a variable holding a table rather than a key associated with a table

also, if the add function did work in this case, then it would result in the mode table looking like {{1,2,3}} which is likely not intended here

i hope this helps!

P#95011 2021-07-17 23:14

Thanks so much, Numzyx.
The code you sent gave me an 'unclosed' error, BUT the line about the function expected a variable holding table inspired me to try:

function add_new_enemy(x,y,type)
    add(enemy,{
        mode={1,2,3},
        add(mode),
        x=x,
        y=y,
        scanx=x,
        scany=y,

A slight tweak to what you'd put which now works as intended.
Thanks again.
Peej

P#95013 2021-07-17 23:28

So presumably 'enemy' is a collection of enemies in your game? You're adding a new one with the function and you want a table called 'mode' to be part of each enemy? Is that right?

Anyway, the problem is in this line:

add(mode,{1,2,3}),

You can't really call functions from inside table definitions like you're trying to do here. Well, I mean you can obviously because you have but it won't work the way you might expect.

There's a couple things going wrong there. First, when you call a function inside the table definition like you're doing, you're actually adding the result of the function call to the table. I don't know what 'add' returns (probably nil?) but whatever it is, that line will try to add that value to your table. That's not the end of the world or anything but it's also not what you're trying to do.

The other problem is that since you're trying to define 'mode' as part of the table and the table definition isn't done yet, 'mode' doesn't actually exist yet when you're calling 'add'. Also, even if 'mode' did exist already, it wouldn't be called 'mode' it would be called 'enemy[some_index].mode'. So, essentially, you're trying to add values to a table that doesn't exist.

Hopefully that made sense.

The fix is easy though. You don't need to use 'add' at all. You can just define the 'mode' table directly.

function add_new_enemy(x,y,type)
    add(enemy,{
        mode={1, 2, 3},
        x=x,
        y=y.... etc.

Or you could add the values in later. Something like this maybe:

function add_new_enemy(x,y,type)
    add(enemy,{
        mode={},
        x=x,
        y=y.... etc.
end

add_new_enemy(1, 2, 'some_type')
add(enemy[1].mode, {1, 2, 3})

I wouldn't do that if the idea is to assign the mode when you create an enemy but you can use that basic idea to add to or modify the mode later if you need to.

Hope that helps!

P#95012 2021-07-17 23:29

Cheers, jasondelaat. A brilliant reply again there.

I'm kinda in awe at this bbs at the moment. IS there always such great answers to problems?

P#95022 2021-07-18 09:02

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 04:39:35 | 0.007s | Q:15