# Why is this "for" loop returning \[ \]void?

**URL:** <https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [May 31, 2023, 8:30pm UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922 "2023-05-31T20:30:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![OB1\_canbeno](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OB1\_canbeno](https://forums.unrealengine.com/u/OB1_canbeno)\
**Post date:** [May 31, 2023, 8:30pm UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/1 "2023-05-31T20:30:12Z")

</div>

I wanted a []creative\_prop, but somehow I made a []void from the following snippet

```auto
# Let's try to get an array of 4 creative_props (from 4 spawns)
PropsList: []creative_prop = for (N:=0..3):
            MaybeProp := SpawnProp(PropAssetRef, vector3{X:=N, Y:=N, Z:=0.0}, IdentityRotation())(0)
            if (Prop := MaybeProp?): # MaybeProp: ?creative_prop
                Prop # Prop: creative_prop

```

Anyway, the error message is:

This variable expects to be initialized with a value of type []creative\_prop, but this initializer is an incompatible value of type []void.(3509)

My question is what am I missing/doing wrong?

Andre

---

<div class="post-metadata">

**Author:** ![Mapshot\_Agency](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mapshot_agency/32/700660_2.png) [@Mapshot\_Agency](https://forums.unrealengine.com/u/Mapshot_Agency)\
**Post date:** [June 1, 2023, 8:55am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/2 "2023-06-01T08:55:31Z")

</div>

Hi,

You should have gotten errors with this code. A vector3 takes float values as the X, Y, Z values but you use the integer N value.

Try changing the SpawnProp line to `MaybeProp := SpawnProp(PropAssetRef, vector3{X:=N * 1.0, Y:=N * 1.0, Z:=0.0}, IdentityRotation())` and see if that works.

The way the type option MaybeProp? works is that if the prop fails to spawn, you can’t assign it to the `Prop` value and the if fails. That way, it returns void instead of creative\_prop. Since your spawnprop is probably failing due to the int values instead of float values, the if-statement fails and returns void.

I also have a question, what does the `(0)` do at the end of your SpawnProp line?

Hope it works!

---

<div class="post-metadata">

**Author:** ![OB1\_canbeno](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OB1\_canbeno](https://forums.unrealengine.com/u/OB1_canbeno)\
**Post date:** [June 1, 2023, 9:25am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/3 "2023-06-01T09:25:33Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/b/f4b9049dedd30ed4390ac2e27c8634fb50be67db.png)

Thanks for you’re help.  
Sorry about the “int” issue. That came about due to me trying to make a small snippet to illustrate the problem.

I’ve used float constants instead.

You will notice I put a call to SpawnProp before the offending snippet to illustrate that is does seem to be a valid call.

The (0) is what I believe to be how one indexes into a tuple, wihch is returned from SpawnProp.

With the example “int” issue resolved, the problem of not getting a list of creative\_props still remains.

The following workaround does work though: Props does become an array of creative\_props.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/e/27ef0cdd597d60314c50a755ddae67f7bb5f8a92.png)

So why is the “for” loop producing []void and not []creative\_prop

Andre

---

<div class="post-metadata">

**Author:** ![Mapshot\_Agency](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mapshot_agency/32/700660_2.png) [@Mapshot\_Agency](https://forums.unrealengine.com/u/Mapshot_Agency)\
**Post date:** [June 1, 2023, 9:38am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/4 "2023-06-01T09:38:41Z")

</div>

> [@OB1\_canbeno](#):
>
> The (0) is what I believe to be how one indexes into a tuple, wihch is returned from SpawnProp.

Ah yes, I forgot the tuple 😅

As to your issue, I haven’t initialized variables that way enough to see the issue. My guess would be that because of the option type creative\_prop? if-statement, it doesn’t work.

For example, if you create a function that returns a creative\_prop like this

```auto
Test():creative_prop=
        MaybeProp := SpawnProp(PropAssetRef, vector3{X:=N, Y:=N, Z:=0.0}, IdentityRotation())(0)
            if (Prop := MaybeProp?): # MaybeProp: ?creative_prop
                return Prop # Prop: creative_prop

```

That also doesn’t work because the function isn’t sure that it ALWAYS returns a creative\_prop. The same principle applies to your issue. Since it’s not all paths that lead to a creative\_prop being returned, it fails and sets the return type to void as a failsafe.

---

<div class="post-metadata">

**Author:** ![t.tutiya](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/t.tutiya/32/924637_2.png) [@t.tutiya](https://forums.unrealengine.com/u/t.tutiya)\
**Post date:** [June 1, 2023, 10:01am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/5 "2023-06-01T10:01:40Z")

</div>

Hi. I haven’t tested it, but how about the following code?

```auto
# Let's try to get an array of 4 creative_props (from 4 spawns)
PropsList: []creative_prop = for:
    N:=0..3
    MaybeProp := SpawnProp(PropAssetRef, vector3{X:=N, Y:=N, Z:=0.0}, IdentityRotation())(0)
    Prop := MaybeProp?
do:
    Prop # Prop: creative_prop

```

I think the original code would not work because it would return a tuple with a mix of creative\_prop and void.

---

<div class="post-metadata">

**Author:** ![OB1\_canbeno](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OB1\_canbeno](https://forums.unrealengine.com/u/OB1_canbeno)\
**Post date:** [June 1, 2023, 11:20am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/6 "2023-06-01T11:20:13Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/c/e/8cea5ab951800b77c49bf2496c4e133a57a2c787.jpeg)

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/6/b/16b34a4c19d37f22bd3a332ce3a8f493065ec1f0.png)

Your code worked perfectly. A single asset is spawned on a 3 by 3 grid, creating a creative\_prop array in the process.

Nice one! Thank you very much t.tutiya,  
Kind regards,  
Andre

---

<div class="post-metadata">

**Author:** ![OB1\_canbeno](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OB1\_canbeno](https://forums.unrealengine.com/u/OB1_canbeno)\
**Post date:** [June 1, 2023, 11:30am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/7 "2023-06-01T11:30:45Z")

</div>

P.S. I’d still like to know why the original “for” loop was returning []void. Knowing why will go towards a better understanding of Verse.

---

<div class="post-metadata">

**Author:** ![t.tutiya](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/t.tutiya/32/924637_2.png) [@t.tutiya](https://forums.unrealengine.com/u/t.tutiya)\
**Post date:** [June 1, 2023, 12:52pm UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/8 "2023-06-01T12:52:36Z")

</div>

My thoughts are as follows.  
The following code is as same as the original code.

```auto
PropsList: []creative_prop = for (N:=0..3):
            MaybeProp := SpawnProp(PropAssetRef, vector3{X:=N, Y:=N, Z:=0.0}, IdentityRotation())(0)
            if (Prop := MaybeProp?): # MaybeProp: ?creative_prop
                Prop # add creative_prop to []creative_prop
            else:
                #no op. add void to []creative_prop (<--ERROR!)

```

Even if, “if” expression is fail, but “for” loop is living. so else claues is evaluted, then return “void”.

---

<div class="post-metadata">

**Author:** ![AndrewScheidecker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andrewscheidecker/32/926976_2.png) [@AndrewScheidecker](https://forums.unrealengine.com/u/AndrewScheidecker)\
**Post date:** [June 2, 2023, 12:21am UTC](https://forums.unrealengine.com/t/why-is-this-for-loop-returning-void/1184922/9 "2023-06-02T00:21:48Z")

</div>

The problem with your original code is that `if` without an `else` does not yield a useful value. `if(a) then b else c` yields `b` or `c`, but `if(a) then b` is only useful for its side effects.

The `for` is just evaluating the body expression for each possible value in the domain, and creating an array containing the yielded values. The body expression in this case was a block with multiple subexpressions, so it uses the value yielded by the final subexpression in the block. Your `if`-without-`else` does not yield a useful value, so it creates an array of useless values.

As others have pointed out, if don’t want some iteration of the `for` to produce an element in the output array, then you need to make the domain of the `for` (the part in parentheses) fail for that iteration.
